Skip to content

Commit

Permalink
Don't show schedules or predictions outside rating
Browse files Browse the repository at this point in the history
As of 65e97eb, the line page accepts a date query param, which enables
Backstop testing on that page to work properly again. However, it also
introduced a regression, where if the date given has no service
available in the API, rather than just returning live predictions,
TransitNearMe.time_data_for_route_by_stop/3 was choking on the error
struct indicating "no service". Fixed.
  • Loading branch information
phildarnowsky committed Dec 13, 2019
1 parent 6597400 commit 23b28c5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
29 changes: 19 additions & 10 deletions apps/site/lib/site/transit_near_me.ex
Original file line number Diff line number Diff line change
Expand Up @@ -408,16 +408,25 @@ defmodule Site.TransitNearMe do
date = Keyword.get(opts, :date, Util.service_date())
schedules_fn = Keyword.get(opts, :schedules_fn, &Schedules.Repo.by_route_ids/2)

route_id
|> expand_route_id()
|> schedules_fn.(direction_id: direction_id, date: date)
|> get_predicted_schedules([route: route_id, direction_id: direction_id], opts)
|> Enum.group_by(&PredictedSchedule.route(&1).id)
|> Enum.flat_map(&schedules_for_route(&1, %{}, [], opts).stops_with_directions)
|> convert_route_time_data_to_map()
|> Map.new(fn {stop_id, time_data} ->
{stop_id, limit_route_time_data(time_data)}
end)
schedule_data =
route_id
|> expand_route_id()
|> schedules_fn.(direction_id: direction_id, date: date)

case schedule_data do
{:error, [%JsonApi.Error{code: "no_service"}]} ->
%{}

_ ->
schedule_data
|> get_predicted_schedules([route: route_id, direction_id: direction_id], opts)
|> Enum.group_by(&PredictedSchedule.route(&1).id)
|> Enum.flat_map(&schedules_for_route(&1, %{}, [], opts).stops_with_directions)
|> convert_route_time_data_to_map()
|> Map.new(fn {stop_id, time_data} ->
{stop_id, limit_route_time_data(time_data)}
end)
end
end

@spec convert_route_time_data_to_map([stop_with_data]) :: %{Stop.id_t() => [headsign_data]}
Expand Down
29 changes: 29 additions & 0 deletions apps/site/test/site/transit_near_me_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -461,5 +461,34 @@ defmodule Site.TransitNearMeTest do

assert %{"place-wimnl" => [%{}]} = actual
end

test "return neither schedules nor predictions if date is outside rating" do
predictions_fn = fn _ -> [@prediction] end

schedules_fn = fn _, _ ->
{:error,
[
%JsonApi.Error{
code: "no_service",
detail: nil,
meta: %{
"end_date" => "2020-03-14",
"start_date" => "2019-12-06",
"version" => "Winter 2020, 2019-12-13T17:29:50+00:00, version D"
},
source: %{"parameter" => "date"}
}
]}
end

actual =
TransitNearMe.time_data_for_route_by_stop(@route.id, 1,
schedules_fn: schedules_fn,
predictions_fn: predictions_fn,
now: @now
)

assert actual == %{}
end
end
end

0 comments on commit 23b28c5

Please sign in to comment.