Skip to content

Commit

Permalink
cleanup: Add more alert factories for use in SystemStatus.SubwayTests
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlarson committed Feb 7, 2025
1 parent 42accb0 commit 8bf0570
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
10 changes: 7 additions & 3 deletions test/dotcom/system_status/subway_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Dotcom.SystemStatus.SubwayTest do

alias Dotcom.SystemStatus.Alerts
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

Expand All @@ -29,8 +29,12 @@ defmodule Dotcom.SystemStatus.SubwayTest do
# Setup
affected_route_id = Faker.Util.pick(@lines_without_branches)
time = time_today()

effect = Faker.Util.pick(Alerts.service_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)
Expand Down Expand Up @@ -565,7 +569,7 @@ defmodule Dotcom.SystemStatus.SubwayTest do
effect = opts[:effect] || Faker.Util.pick(Alerts.service_effects())
active_period = opts |> Keyword.fetch!(:active_period)

Alert.build(:alert,
build(:alert,
effect: effect,
informed_entity:
InformedEntitySet.build(:informed_entity_set,
Expand Down
39 changes: 39 additions & 0 deletions test/support/factories/alerts/alert.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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

0 comments on commit 8bf0570

Please sign in to comment.