diff --git a/apps/api_web/test/api_web/controllers/line_controller_test.exs b/apps/api_web/test/api_web/controllers/line_controller_test.exs index 7a4c108d3..4e2b84120 100644 --- a/apps/api_web/test/api_web/controllers/line_controller_test.exs +++ b/apps/api_web/test/api_web/controllers/line_controller_test.exs @@ -29,13 +29,15 @@ defmodule ApiWeb.LineControllerTest do id: "1-1", agency_id: "1", type: 1, - line_id: "1" + line_id: "1", + sort_order: 1 }, %Route{ id: "1-2", agency_id: "1", type: 1, - line_id: "1" + line_id: "1", + sort_order: 2 } ]) diff --git a/apps/api_web/test/api_web/controllers/prediction_controller_test.exs b/apps/api_web/test/api_web/controllers/prediction_controller_test.exs index 323469f29..20a378c6a 100644 --- a/apps/api_web/test/api_web/controllers/prediction_controller_test.exs +++ b/apps/api_web/test/api_web/controllers/prediction_controller_test.exs @@ -138,7 +138,7 @@ defmodule ApiWeb.PredictionControllerTest do {%{"stop" => "1", "route_type" => "1,2"}, [prediction1, prediction2]} ] do conn = get(conn, "/predictions", params) - assert conn.assigns.data == expected + assert Enum.sort(conn.assigns.data) == Enum.sort(expected) end end diff --git a/apps/api_web/test/api_web/controllers/route_controller_test.exs b/apps/api_web/test/api_web/controllers/route_controller_test.exs index 2dddbc989..1dfa4830a 100644 --- a/apps/api_web/test/api_web/controllers/route_controller_test.exs +++ b/apps/api_web/test/api_web/controllers/route_controller_test.exs @@ -52,12 +52,14 @@ defmodule ApiWeb.RouteControllerTest do @route_pattern1 %Model.RoutePattern{ id: "1", route_id: "1", - name: "1-0-1" + name: "1-0-1", + sort_order: 1 } @route_pattern2 %Model.RoutePattern{ id: "2", route_id: "1", - name: "1-1-1" + name: "1-1-1", + sort_order: 2 } setup %{conn: conn} do diff --git a/apps/api_web/test/api_web/controllers/schedule_controller_test.exs b/apps/api_web/test/api_web/controllers/schedule_controller_test.exs index ed9cb1ac4..8f0bbf23c 100644 --- a/apps/api_web/test/api_web/controllers/schedule_controller_test.exs +++ b/apps/api_web/test/api_web/controllers/schedule_controller_test.exs @@ -219,13 +219,13 @@ defmodule ApiWeb.SchedulerControllerTest do State.Schedule.new_state([schedule1, schedule2]) conn = get(conn, "/schedules", %{"stop" => "stop"}) - assert conn.assigns.data == [schedule1, schedule2] + assert Enum.sort(conn.assigns.data) == [schedule1, schedule2] conn = get(conn, "/schedules", %{"stop" => "stop", "route_type" => "2"}) assert conn.assigns.data == [schedule2] conn = get(conn, "/schedules", %{"stop" => "stop", "route_type" => "1,2"}) - assert conn.assigns.data == [schedule1, schedule2] + assert Enum.sort(conn.assigns.data) == [schedule1, schedule2] end test "versions before 2019-02-12 include new Kenmore stops", %{conn: conn} do diff --git a/apps/api_web/test/api_web/controllers/vehicle_controller_test.exs b/apps/api_web/test/api_web/controllers/vehicle_controller_test.exs index 56c086114..b769c2cda 100644 --- a/apps/api_web/test/api_web/controllers/vehicle_controller_test.exs +++ b/apps/api_web/test/api_web/controllers/vehicle_controller_test.exs @@ -91,8 +91,11 @@ defmodule ApiWeb.VehicleControllerTest do test "can filter by trip", %{conn: conn} do for vehicle <- @vehicles do - assert index_data(conn, %{"trip" => vehicle.trip_id}) == [vehicle] - assert index_data(conn, %{"trip" => "#{vehicle.trip_id},not_a_trip"}) == [vehicle] + vehicle_id = vehicle.id + assert [%Vehicle{id: ^vehicle_id}] = index_data(conn, %{"trip" => vehicle.trip_id}) + + assert [%Vehicle{id: ^vehicle_id}] = + index_data(conn, %{"trip" => "#{vehicle.trip_id},not_a_trip"}) end assert index_data(conn, %{"trip" => "not_a_trip"}) == [] diff --git a/apps/state/lib/state/route.ex b/apps/state/lib/state/route.ex index 4b69b1130..e49afdf97 100644 --- a/apps/state/lib/state/route.ex +++ b/apps/state/lib/state/route.ex @@ -28,6 +28,11 @@ defmodule State.Route do {:noreply, state, :hibernate} end + @impl State.Server + def post_load_hook(routes) do + Enum.sort_by(routes, & &1.sort_order) + end + def do_gather(%{ {:fetch, "directions.txt"} => directions_blob, {:fetch, "routes.txt"} => routes_blob @@ -83,12 +88,6 @@ defmodule State.Route do end end - def by_ids(ids) do - ids - |> super() - |> Enum.sort_by(& &1.sort_order) - end - def match(matcher, index, opts \\ []) do opts = Keyword.put_new(opts, :order_by, {:sort_order, :asc}) super(matcher, index, opts) diff --git a/apps/state/lib/state/route_pattern.ex b/apps/state/lib/state/route_pattern.ex index 824e4e864..f021d3679 100644 --- a/apps/state/lib/state/route_pattern.ex +++ b/apps/state/lib/state/route_pattern.ex @@ -21,6 +21,11 @@ defmodule State.RoutePattern do optional(:stop_ids) => [Stop.id()] } + @impl State.Server + def post_load_hook(route_patterns) do + Enum.sort_by(route_patterns, & &1.sort_order) + end + @spec by_id(String.t()) :: RoutePattern.t() | nil def by_id(id) do case super(id) do diff --git a/apps/state/test/state/prediction_test.exs b/apps/state/test/state/prediction_test.exs index 98d33b844..c91e6290b 100644 --- a/apps/state/test/state/prediction_test.exs +++ b/apps/state/test/state/prediction_test.exs @@ -62,8 +62,8 @@ defmodule State.PredictionTest do test "returns all predictions if no filters set" do new_state([@prediction, @prediction2]) by_stops = by_stop_id("stop") - assert filter_by_route_type(by_stops, nil) == [@prediction, @prediction2] - assert filter_by_route_type(by_stops, []) == [@prediction, @prediction2] + assert Enum.sort(filter_by_route_type(by_stops, nil)) == [@prediction, @prediction2] + assert Enum.sort(filter_by_route_type(by_stops, [])) == [@prediction, @prediction2] end test "filters by route_type" do @@ -76,7 +76,7 @@ defmodule State.PredictionTest do assert filter_by_route_type(by_stops, [0]) == [@prediction] assert filter_by_route_type(by_stops, [1]) == [@prediction2] - assert filter_by_route_type(by_stops, [0, 1]) == [@prediction, @prediction2] + assert Enum.sort(filter_by_route_type(by_stops, [0, 1])) == [@prediction, @prediction2] assert filter_by_route_type(by_stops, [2]) == [] end end diff --git a/apps/state/test/state/stop_test.exs b/apps/state/test/state/stop_test.exs index 4e6eb676a..ef9fcd71d 100644 --- a/apps/state/test/state/stop_test.exs +++ b/apps/state/test/state/stop_test.exs @@ -42,12 +42,18 @@ defmodule State.StopTest do other = %Stop{id: "7"} State.Stop.new_state([parent, child, other_child, other]) - assert State.Stop.by_family_ids([parent.id]) == [parent, child, other_child] + assert Enum.sort(State.Stop.by_family_ids([parent.id])) == [parent, child, other_child] assert State.Stop.by_family_ids([child.id]) == [child] - assert State.Stop.by_family_ids([parent.id, child.id]) == [parent, child, other_child] + + assert Enum.sort(State.Stop.by_family_ids([parent.id, child.id])) == [ + parent, + child, + other_child + ] + assert State.Stop.by_family_ids([other.id]) == [other] - assert State.Stop.by_parent_station(parent.id) == [child, other_child] - assert State.Stop.siblings(child.id) == [child, other_child] + assert Enum.sort(State.Stop.by_parent_station(parent.id)) == [child, other_child] + assert Enum.sort(State.Stop.siblings(child.id)) == [child, other_child] end describe "by_parent_station/1" do