Skip to content

Commit

Permalink
fixup! cleanup: Add more alert factories for use in SystemStatus.Subw…
Browse files Browse the repository at this point in the history
…ayTests
  • Loading branch information
joshlarson committed Feb 12, 2025
1 parent d6e7b70 commit 4a97d25
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 89 deletions.
155 changes: 66 additions & 89 deletions test/dotcom/system_status/subway_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ defmodule Dotcom.SystemStatus.SubwayTest do
alias Dotcom.SystemStatus.Alerts
alias Dotcom.SystemStatus.Subway
alias Test.Support.Factories.Alerts.Alert
alias Test.Support.Factories.Alerts.InformedEntity
alias Test.Support.Factories.Alerts.InformedEntitySet

@lines_without_branches List.delete(Subway.lines(), "Green")

Expand Down Expand Up @@ -54,7 +52,11 @@ defmodule Dotcom.SystemStatus.SubwayTest do
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 = [
Alert.build(:alert_for_route, route_id: affected_route_id, effect: effect)
|> Alert.active_during(time)
]

# Exercise
groups = Subway.subway_status(alerts, time)
Expand All @@ -72,7 +74,11 @@ defmodule Dotcom.SystemStatus.SubwayTest do
# Setup
affected_route_id = Faker.Util.pick(@lines_without_branches)
time = time_today()
alerts = [current_alert(route_id: affected_route_id, time: time)]

alerts = [
Alert.build(:alert_for_route, route_id: affected_route_id)
|> Alert.active_during(time)
]

# Exercise
groups = Subway.subway_status(alerts, time)
Expand All @@ -91,7 +97,10 @@ defmodule Dotcom.SystemStatus.SubwayTest do
affected_route_id = Faker.Util.pick(@lines_without_branches)
time = time_today()

alerts = [current_alert(route_id: affected_route_id, time: time)]
alerts = [
Alert.build(:alert_for_route, route_id: affected_route_id)
|> Alert.active_during(time)
]

# Exercise
groups = Subway.subway_status(alerts, time)
Expand All @@ -116,7 +125,10 @@ defmodule Dotcom.SystemStatus.SubwayTest do
time = time_today()
alert_start_time = time_after(time)

alerts = [future_alert(route_id: affected_route_id, start_time: alert_start_time)]
alerts = [
Alert.build(:alert_for_route, route_id: affected_route_id)
|> Alert.active_starting_at(alert_start_time)
]

# Exercise
groups = Subway.subway_status(alerts, time)
Expand All @@ -139,7 +151,7 @@ defmodule Dotcom.SystemStatus.SubwayTest do
alert_start_time = time_before(time)

alerts = [
alert(
Alert.build(:alert_for_route,
route_id: affected_route_id,
effect: effect,
active_period: [{alert_start_time, nil}]
Expand Down Expand Up @@ -170,7 +182,7 @@ defmodule Dotcom.SystemStatus.SubwayTest do
expired_alert_start_time = time_before(expired_alert_end_time)

alerts = [
alert(
Alert.build(:alert_for_route,
route_id: affected_route_id,
active_period: [
{expired_alert_start_time, expired_alert_end_time},
Expand Down Expand Up @@ -204,8 +216,10 @@ defmodule Dotcom.SystemStatus.SubwayTest do
|> Enum.sort(:desc)

alerts = [
current_alert(route_id: affected_route_id, time: time, effect: effect1),
current_alert(route_id: affected_route_id, time: time, effect: effect2)
Alert.build(:alert_for_route, route_id: affected_route_id, effect: effect1)
|> Alert.active_during(time),
Alert.build(:alert_for_route, route_id: affected_route_id, effect: effect2)
|> Alert.active_during(time)
]

# Exercise
Expand All @@ -229,8 +243,10 @@ defmodule Dotcom.SystemStatus.SubwayTest do
future_alert_start_time = time_after(time)

alerts = [
future_alert(route_id: affected_route_id, start_time: future_alert_start_time),
current_alert(route_id: affected_route_id, time: time)
Alert.build(:alert_for_route, route_id: affected_route_id)
|> Alert.active_starting_at(future_alert_start_time),
Alert.build(:alert_for_route, route_id: affected_route_id)
|> Alert.active_during(time)
]

# Exercise
Expand All @@ -254,8 +270,10 @@ defmodule Dotcom.SystemStatus.SubwayTest do
effect = Faker.Util.pick(Alerts.service_effects())

alerts = [
current_alert(route_id: affected_route_id, time: time, effect: effect),
current_alert(route_id: affected_route_id, time: time, effect: effect)
Alert.build(:alert_for_route, route_id: affected_route_id, effect: effect)
|> Alert.active_during(time),
Alert.build(:alert_for_route, route_id: affected_route_id, effect: effect)
|> Alert.active_during(time)
]

# Exercise
Expand All @@ -280,8 +298,10 @@ defmodule Dotcom.SystemStatus.SubwayTest do
effect = Faker.Util.pick(Alerts.service_effects())

alerts = [
future_alert(route_id: affected_route_id, start_time: start_time, effect: effect),
future_alert(route_id: affected_route_id, start_time: start_time, effect: effect)
Alert.build(:alert_for_route, route_id: affected_route_id, effect: effect)
|> Alert.active_starting_at(start_time),
Alert.build(:alert_for_route, route_id: affected_route_id, effect: effect)
|> Alert.active_starting_at(start_time)
]

# Exercise
Expand All @@ -307,7 +327,8 @@ defmodule Dotcom.SystemStatus.SubwayTest do
alerts =
GreenLine.branch_ids()
|> Enum.map(fn route_id ->
current_alert(route_id: route_id, time: time, effect: effect)
Alert.build(:alert_for_route, route_id: route_id, effect: effect)
|> Alert.active_during(time)
end)

# Exercise
Expand All @@ -329,7 +350,10 @@ defmodule Dotcom.SystemStatus.SubwayTest do
time = time_today()
effect = Faker.Util.pick(Alerts.service_effects())

alerts = [current_alert(route_id: affected_branch_id, effect: effect, time: time)]
alerts = [
Alert.build(:alert_for_route, route_id: affected_branch_id, effect: effect)
|> Alert.active_during(time)
]

# Exercise
groups = Subway.subway_status(alerts, time)
Expand All @@ -350,7 +374,10 @@ defmodule Dotcom.SystemStatus.SubwayTest do
time = time_today()
effect = Faker.Util.pick(Alerts.service_effects())

alerts = [current_alert(route_id: affected_branch_id, effect: effect, time: time)]
alerts = [
Alert.build(:alert_for_route, route_id: affected_branch_id, effect: effect)
|> Alert.active_during(time)
]

# Exercise
groups = Subway.subway_status(alerts, time)
Expand All @@ -373,7 +400,10 @@ defmodule Dotcom.SystemStatus.SubwayTest do
time = time_today()
effect = Faker.Util.pick(Alerts.service_effects())

alerts = [current_alert(route_id: affected_branch_id, effect: effect, time: time)]
alerts = [
Alert.build(:alert_for_route, route_id: affected_branch_id, effect: effect)
|> Alert.active_during(time)
]

# Exercise
groups = Subway.subway_status(alerts, time)
Expand Down Expand Up @@ -403,8 +433,10 @@ defmodule Dotcom.SystemStatus.SubwayTest do
Faker.Util.sample_uniq(2, fn -> Faker.Util.pick(Alerts.service_effects()) end)

alerts = [
current_alert(route_id: affected_branch_id1, effect: effect1, time: time),
current_alert(route_id: affected_branch_id2, effect: effect2, time: time)
Alert.build(:alert_for_route, route_id: affected_branch_id1, effect: effect1)
|> Alert.active_during(time),
Alert.build(:alert_for_route, route_id: affected_branch_id2, effect: effect2)
|> Alert.active_during(time)
]

# Exercise
Expand All @@ -426,7 +458,10 @@ defmodule Dotcom.SystemStatus.SubwayTest do
# Setup
time = time_today()

alerts = [current_alert(route_id: "Red", time: time)]
alerts = [
Alert.build(:alert_for_route, route_id: "Red")
|> Alert.active_during(time)
]

# Exercise
groups = Subway.subway_status(alerts, time)
Expand All @@ -446,7 +481,10 @@ defmodule Dotcom.SystemStatus.SubwayTest do
time = time_today()
effect = Faker.Util.pick(Alerts.service_effects())

alerts = [current_alert(route_id: "Mattapan", effect: effect, time: time)]
alerts = [
Alert.build(:alert_for_route, route_id: "Mattapan", effect: effect)
|> Alert.active_during(time)
]

# Exercise
groups = Subway.subway_status(alerts, time)
Expand All @@ -464,7 +502,10 @@ defmodule Dotcom.SystemStatus.SubwayTest do
# Setup
time = time_today()

alerts = [current_alert(route_id: "Mattapan", time: time)]
alerts = [
Alert.build(:alert_for_route, route_id: "Mattapan")
|> Alert.active_during(time)
]

# Exercise
groups = Subway.subway_status(alerts, time)
Expand Down Expand Up @@ -518,68 +559,4 @@ defmodule Dotcom.SystemStatus.SubwayTest do
defp between(time1, time2) do
Faker.DateTime.between(time1, time2) |> Timex.to_datetime("America/New_York")
end

# Returns a random alert that will be active at the time given by
# the required `:time` opt.
#
# Required opts:
# - route_id
# - time
#
# Optional opts:
# - effect (default behavior is to choose an effect at random)
defp current_alert(opts) do
{time, opts} = opts |> Keyword.pop!(:time)

start_time = time_before(time)
end_time = time_after(time)

opts
|> Keyword.put_new(:active_period, [{start_time, end_time}])
|> alert()
end

# Returns a random alert whose active_period starts at the provided
# `:start_time` opt.
#
# Required opts:
# - route_id
# - start_time
#
# Optional opts:
# - effect (default behavior is to choose an effect at random)
defp future_alert(opts) do
{start_time, opts} = opts |> Keyword.pop!(:start_time)

opts
|> Keyword.put_new(:active_period, [{start_time, time_after(start_time)}])
|> alert()
end

# Returns a random alert for the given `:route_id` and
# `:active_period` opts.
#
# Required opts:
# - route_id
# - active_period (Note that this is an array)
#
# Optional opts:
# - effect (default behavior is to choose an effect at random)
defp alert(opts) do
route_id = opts |> Keyword.fetch!(:route_id)
effect = opts[:effect] || Faker.Util.pick(Alerts.service_effects())
active_period = opts |> Keyword.fetch!(:active_period)

Alert.build(:alert,
effect: effect,
informed_entity:
InformedEntitySet.build(:informed_entity_set,
route: route_id,
entities: [
InformedEntity.build(:informed_entity, route: route_id)
]
),
active_period: active_period
)
end
end
4 changes: 4 additions & 0 deletions test/support/factories/alerts/alert.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ defmodule Test.Support.Factories.Alerts.Alert do
%{alert | active_period: [{time_before(time), time_after(time)}]}
end

def active_starting_at(alert, start_time) do
%{alert | active_period: [{start_time, time_after(start_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)
Expand Down

0 comments on commit 4a97d25

Please sign in to comment.