diff --git a/test/dotcom/system_status/subway_test.exs b/test/dotcom/system_status/subway_test.exs index b22e09d053..1ff65f65a3 100644 --- a/test/dotcom/system_status/subway_test.exs +++ b/test/dotcom/system_status/subway_test.exs @@ -3,7 +3,7 @@ defmodule Dotcom.SystemStatus.SubwayTest do doctest Dotcom.SystemStatus.Subway alias Dotcom.SystemStatus.Subway - alias Test.Support.Factories.Alerts.Alert + import Test.Support.Factories.Alerts.Alert alias Test.Support.Factories.Alerts.InformedEntity alias Test.Support.Factories.Alerts.InformedEntitySet @@ -33,7 +33,10 @@ defmodule Dotcom.SystemStatus.SubwayTest do affected_route_id = Faker.Util.pick(@lines_without_branches) time = time_today() effect = Faker.Util.pick(@effects) - alerts = [current_alert(route_id: affected_route_id, time: time, effect: effect)] + + alerts = [ + build(:alert, effect: effect) |> active_during(time) |> for_route(affected_route_id) + ] # Exercise groups = Subway.subway_status(alerts, time) @@ -565,7 +568,7 @@ defmodule Dotcom.SystemStatus.SubwayTest do effect = opts[:effect] || Faker.Util.pick(@effects) active_period = opts |> Keyword.fetch!(:active_period) - Alert.build(:alert, + build(:alert, effect: effect, informed_entity: InformedEntitySet.build(:informed_entity_set, diff --git a/test/support/factories/alerts/alert.ex b/test/support/factories/alerts/alert.ex index 4fc24f3760..ba9af4e453 100644 --- a/test/support/factories/alerts/alert.ex +++ b/test/support/factories/alerts/alert.ex @@ -6,8 +6,11 @@ defmodule Test.Support.Factories.Alerts.Alert do use ExMachina alias Alerts.{Alert, Priority} + alias Test.Support.Factories.Alerts.InformedEntity alias Test.Support.Factories.Alerts.InformedEntitySet + @high_priority_effects [:delay, :shuttle, :station_closure, :suspension] + def alert_factory do %Alert{ id: :rand.uniform(999) |> Integer.to_string(), @@ -28,4 +31,40 @@ defmodule Test.Support.Factories.Alerts.Alert do url: Faker.Internet.url() } end + + def for_route(alert, route_id) do + %{ + alert + | informed_entity: + InformedEntitySet.build(:informed_entity_set, + route: route_id, + entities: [ + InformedEntity.build(:informed_entity, route: route_id) + ] + ) + } + end + + def with_high_priority_effect(alert) do + %{alert | effect: Faker.Util.pick(@high_priority_effects)} + end + + def active_during(alert, time) do + %{alert | active_period: [{time_before(time), time_after(time)}]} + end + + # Returns a random time during the day today before the time provided. + defp time_before(time) do + between(Timex.beginning_of_day(time), time) + end + + # Returns a random time during the day today after the time provided. + defp time_after(time) do + between(time, Timex.end_of_day(time)) + end + + # Returns a random time between the times provided in the Eastern time zone. + defp between(time1, time2) do + Faker.DateTime.between(time1, time2) |> Timex.to_datetime("America/New_York") + end end