Skip to content
Open
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
6 changes: 5 additions & 1 deletion lib/dotcom/schedule_finder/upcoming_departures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ defmodule Dotcom.ScheduleFinder.UpcomingDepartures do
defp arrival_substatus(%{
predicted_schedule: %PredictedSchedule{prediction: %Prediction{status: status}}
})
when status != nil,
when status != nil and status != "Delayed",
do: {:status, status |> String.split(" ") |> Enum.map_join(" ", &String.capitalize/1)}

defp arrival_substatus(%{
Expand All @@ -473,6 +473,7 @@ defmodule Dotcom.ScheduleFinder.UpcomingDepartures do
}) do
scheduled_time = schedule.departure_time
predicted_time = prediction.departure_time
status = prediction.status

cond do
predicted_time == nil ->
Expand All @@ -484,6 +485,9 @@ defmodule Dotcom.ScheduleFinder.UpcomingDepartures do
DateTime.diff(predicted_time, scheduled_time, :second) >= 60 ->
{:delayed_from, scheduled_time}

status != nil ->
{:status, status}

true ->
:on_time
end
Expand Down
124 changes: 124 additions & 0 deletions test/dotcom/schedule_finder/upcoming_departures_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1858,6 +1858,130 @@ defmodule Dotcom.ScheduleFinder.UpcomingDeparturesTest do
assert departure.arrival_substatus == {:status, display_status}
end

test "shows {:status, 'Delayed'} for commuter rail if the status is 'Delayed', but the predicted time is less than a minute late" do
# Setup
now = Dotcom.Utils.DateTime.now()

route = Factories.Routes.Route.build(:commuter_rail_route)
route_id = route.id
stop_id = FactoryHelpers.build(:id)

trip = Factories.Schedules.Trip.build(:trip)
direction_id = Faker.Util.pick([0, 1])

scheduled_departure_time =
Generators.DateTime.random_time_range_date_time(
{now, ServiceDateTime.end_of_service_day(now)}
)

predicted_departure_time =
scheduled_departure_time
|> DateTime.shift(second: Faker.random_between(0, 59))

expect(Predictions.Repo.Mock, :all, fn [
route: ^route_id,
direction_id: ^direction_id,
include_terminals: true
] ->
[
Factories.Predictions.Prediction.build(:prediction,
departure_time: predicted_departure_time,
stop: Factories.Stops.Stop.build(:stop, id: stop_id),
status: "Delayed",
trip: trip
)
]
end)

expect(Schedules.Repo.Mock, :by_route_ids, fn
_, _ ->
[
Factories.Schedules.Schedule.build(:schedule,
departure_time: scheduled_departure_time,
time: scheduled_departure_time,
stop: Factories.Stops.Stop.build(:stop, id: stop_id),
trip: trip
)
]
end)

# Exercise
departures =
UpcomingDepartures.upcoming_departures(%{
direction_id: direction_id,
now: now,
route: route,
stop_id: stop_id
})

# Verify
assert [departure] = departures
assert departure.arrival_status == {:time, predicted_departure_time}
assert departure.arrival_substatus == {:status, "Delayed"}
end

test "shows {:delayed_from, scheduled_time} for commuter rail if predicted time is more than a minute late even if the status is 'Delayed'" do
# Setup
now = Dotcom.Utils.DateTime.now()

route = Factories.Routes.Route.build(:commuter_rail_route)
route_id = route.id
stop_id = FactoryHelpers.build(:id)

trip = Factories.Schedules.Trip.build(:trip)
direction_id = Faker.Util.pick([0, 1])

scheduled_departure_time =
Generators.DateTime.random_time_range_date_time(
{now, ServiceDateTime.end_of_service_day(now)}
)

predicted_departure_time =
scheduled_departure_time
|> DateTime.shift(second: Faker.random_between(60, 3600))

expect(Predictions.Repo.Mock, :all, fn [
route: ^route_id,
direction_id: ^direction_id,
include_terminals: true
] ->
[
Factories.Predictions.Prediction.build(:prediction,
departure_time: predicted_departure_time,
stop: Factories.Stops.Stop.build(:stop, id: stop_id),
status: "Delayed",
trip: trip
)
]
end)

expect(Schedules.Repo.Mock, :by_route_ids, fn
_, _ ->
[
Factories.Schedules.Schedule.build(:schedule,
departure_time: scheduled_departure_time,
time: scheduled_departure_time,
stop: Factories.Stops.Stop.build(:stop, id: stop_id),
trip: trip
)
]
end)

# Exercise
departures =
UpcomingDepartures.upcoming_departures(%{
direction_id: direction_id,
now: now,
route: route,
stop_id: stop_id
})

# Verify
assert [departure] = departures
assert departure.arrival_status == {:time, predicted_departure_time}
assert departure.arrival_substatus == {:delayed_from, scheduled_departure_time}
end

test "shows :cancelled for commuter rail or bus if the schedule_relationship is :cancelled or :skipped" do
# Setup
now = Dotcom.Utils.DateTime.now()
Expand Down