Skip to content

Commit

Permalink
fix: address test failures caused by ordering changes
Browse files Browse the repository at this point in the history
  • Loading branch information
paulswartz committed Sep 20, 2023
1 parent 7635810 commit c80435b
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"}) == []
Expand Down
11 changes: 5 additions & 6 deletions apps/state/lib/state/route.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions apps/state/lib/state/route_pattern.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions apps/state/test/state/prediction_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
14 changes: 10 additions & 4 deletions apps/state/test/state/stop_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c80435b

Please sign in to comment.