Skip to content
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
12 changes: 9 additions & 3 deletions lib/dotcom/alerts/endpoint_stops.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ defmodule Dotcom.Alerts.EndpointStops do
# stop-range per alert, and returns the non-nil ones.
@impl Behaviour
def endpoint_stops(alerts, route_ids) do
alerts
|> Enum.map(&endpoint_stops_for_alert(&1, route_ids))
|> Enum.reject(&Kernel.is_nil/1)
route_ids = route_ids |> Enum.reject(&Kernel.is_nil/1)

if route_ids |> Enum.empty?() do
[]
else
alerts
|> Enum.map(&endpoint_stops_for_alert(&1, route_ids))
|> Enum.reject(&Kernel.is_nil/1)
end
end

# Returns a single endpoint stop-range for the alert given, or nil
Expand Down
15 changes: 14 additions & 1 deletion test/dotcom/alerts/endpoint_stops_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ defmodule Dotcom.Alerts.EndpointStopsTest do
}

setup do
Routes.Repo.Mock |> stub(:get, fn _ -> Route.build(:route) end)
Routes.Repo.Mock
|> stub(:get, fn route_id when is_binary(route_id) -> Route.build(:route) end)

verify_on_exit!()
:ok
end
Expand All @@ -27,6 +29,17 @@ defmodule Dotcom.Alerts.EndpointStopsTest do
assert EndpointStops.endpoint_stops([alert], [route_id]) == []
end

# [nil] is what you get if you pull the route-informed-entities
# out of an alert that isn't associated with any route. Sometimes
# those route-informed-entities are what get passed back into the
# endpoint_stops/2, which is why [nil] is a relevant input to test
# here.
test "returns empty list if the route ID's provided are [nil]" do
alert = Alert.new(informed_entity: [%InformedEntity{stop: nil}])

assert EndpointStops.endpoint_stops([alert], [nil]) == []
end

test "returns the same stop twice if an alert's informed entities includes precisely one stop" do
# Setup
route_id = FactoryHelpers.build(:id)
Expand Down