Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(TimetableController): update via-Fairmount trains #1765

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,45 @@ defmodule SiteWeb.ScheduleController.TimetableController do
"""
@spec trip_messages(Routes.Route.t(), 0 | 1) :: %{{String.t(), String.t()} => String.t()}
def trip_messages(%Routes.Route{id: "CR-Franklin"}, 0) do
train = "731"
kotva006 marked this conversation as resolved.
Show resolved Hide resolved
["741", "757", "759", "735"]
|> Enum.flat_map(&franklin_via_fairmount(&1, 0))
|> Enum.into(%{})
end

def trip_messages(%Routes.Route{id: "CR-Franklin"}, 1) do
["740", "728", "758", "732", "760"]
|> Enum.flat_map(&franklin_via_fairmount(&1, 1))
|> Enum.into(%{})
end

def trip_messages(_, _) do
%{}
end

defp franklin_via_fairmount(train, direction) do
stops = stops_for_fairmount(direction)

[
List.duplicate(train, 4),
["place-bbsta", "place-rugg", "place-NEC-2203", "place-DB-0095"],
["Via", "Fair-", "mount", "Line"]
List.duplicate(train, length(stops)),
stops,
["Via", "Fair-", "mount", "Line", "-"]
]
|> make_via_list()
|> Map.put({train}, "Via Fairmount Line")
|> Enum.concat([{{train}, "Via Fairmount Line"}])
end

def trip_messages(_, _) do
%{}
defp stops_for_fairmount(1) do
["place-DB-0095", "place-forhl", "place-rugg", "place-bbsta"]
end

defp stops_for_fairmount(0) do
["place-bbsta", "place-rugg", "place-forhl", "place-NEC-2203", "place-DB-0095"]
end

def make_via_list(list) do
list
|> List.zip()
|> Map.new(fn {train, stop, value} -> {{train, stop}, value} end)
|> Enum.map(fn {train, stop, value} -> {{train, stop}, value} end)
end

defp all_stops(conn, _) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,20 @@ defmodule SiteWeb.ScheduleController.TimetableControllerTest do
end

describe "trip_messages/2" do
test "returns proper messages for CR Franklin" do
assert trip_messages(%Routes.Route{id: "CR-Franklin"}, 1) == %{}

assert [
"731"
] ==
test "returns proper messages for a CR Franklin train running via Fairmount" do
assert Enum.member?(
%Routes.Route{id: "CR-Franklin"}
|> trip_messages(0)
|> Map.keys()
|> Enum.map(&elem(&1, 0))
|> Enum.uniq()
|> Enum.sort()
|> Enum.sort(),
"735"
)
end

test "returns proper messages for CR Fairmount" do
assert trip_messages(%Routes.Route{id: "CR-Fairmount"}, 1) == %{}
test "returns proper messages for others" do
assert trip_messages(%Routes.Route{id: "CR-Worcester"}, 1) == %{}
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ defmodule SiteWeb.ScheduleControllerTest do
assert conn.assigns.trip_messages
end

@doc """
FIXME: The map_size will change whenever the schedule changes the number of trains needing these messages.
"""
test "assigns trip messages for a few route/directions", %{conn: conn} do
for {route_id, direction_id, expected_size} <- [
{"CR-Franklin", 0, 5}
{"CR-Franklin", 0, 24},
{"CR-Franklin", 1, 25}
] do
path =
timetable_path(conn, :show, route_id, schedule_direction: %{direction_id: direction_id})
Expand Down