Skip to content

Commit

Permalink
cleanup: Simplify module attributes in SystemStatus code and tests (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlarson authored and anthonyshull committed Feb 10, 2025
1 parent fd28ccd commit 7bf6227
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
10 changes: 8 additions & 2 deletions lib/dotcom/system_status/alerts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ defmodule Dotcom.SystemStatus.Alerts do
relying on some specific criteria that are specific enough that they don't
belong in the main `Alerts` module.
"""
@relevant_effects [:delay, :shuttle, :suspension, :station_closure]

@service_effects [:delay, :shuttle, :suspension, :station_closure]
@doc """
Returns a list of the alert effects that are considered
service-impacting.
"""
def service_effects(), do: @service_effects

@doc """
Expand Down Expand Up @@ -86,7 +92,7 @@ defmodule Dotcom.SystemStatus.Alerts do
}
"""
def filter_relevant(alerts) do
alerts |> Enum.filter(fn %{effect: effect} -> effect in @relevant_effects end)
alerts |> Enum.filter(fn %{effect: effect} -> effect in @service_effects end)
end

# Returns true if the alert (as signified by the active_period_start provided)
Expand Down
7 changes: 5 additions & 2 deletions lib/dotcom/system_status/subway.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ defmodule Dotcom.SystemStatus.Subway do
alias Alerts.Alert

@lines ["Blue", "Green", "Orange", "Red"]
@green_line_branches ["Green-B", "Green-C", "Green-D", "Green-E"]
@doc """
Returns the route_id's for the subway lines.
"""
def lines(), do: @lines

@doc """
Translates the given alerts into a map indicating the overall system
Expand Down Expand Up @@ -140,7 +143,7 @@ defmodule Dotcom.SystemStatus.Subway do
%{
route_id: "Green",
branches_with_statuses:
@green_line_branches
GreenLine.branch_ids()
|> Enum.map(&add_statuses_for_route(&1, alerts, time))
|> group_by_statuses()
|> nest_grouped_statuses_under_branches()
Expand Down
52 changes: 26 additions & 26 deletions test/dotcom/system_status/subway_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,21 @@ defmodule Dotcom.SystemStatus.SubwayTest do
use ExUnit.Case, async: true
doctest Dotcom.SystemStatus.Subway

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

@all_rail_lines ["Blue", "Green", "Orange", "Red"]
@green_line_branches ["Green-B", "Green-C", "Green-D", "Green-E"]
@lines_without_branches ["Blue", "Orange", "Red"]

@effects [:delay, :shuttle, :station_closure, :suspension]
@lines_without_branches List.delete(Subway.lines(), "Green")

describe "heavy rail groups" do
test "when there are no alerts, lists each line as normal" do
# Exercise
groups = Subway.subway_status([], time_today())

# Verify
@all_rail_lines
Subway.lines()
|> Enum.each(fn route_id ->
statuses = groups |> status_entries_for(route_id)

Expand All @@ -32,7 +29,7 @@ defmodule Dotcom.SystemStatus.SubwayTest do
# Setup
affected_route_id = Faker.Util.pick(@lines_without_branches)
time = time_today()
effect = Faker.Util.pick(@effects)
effect = Faker.Util.pick(Alerts.service_effects())
alerts = [current_alert(route_id: affected_route_id, time: time, effect: effect)]

# Exercise
Expand All @@ -51,7 +48,7 @@ defmodule Dotcom.SystemStatus.SubwayTest do
# Setup
affected_route_id = Faker.Util.pick(@lines_without_branches)
time = time_today()
effect = Faker.Util.pick(@effects)
effect = Faker.Util.pick(Alerts.service_effects())
alerts = [current_alert(route_id: affected_route_id, time: time, effect: effect)]

# Exercise
Expand Down Expand Up @@ -133,7 +130,7 @@ defmodule Dotcom.SystemStatus.SubwayTest do
affected_route_id = Faker.Util.pick(@lines_without_branches)

time = time_today()
effect = Faker.Util.pick(@effects)
effect = Faker.Util.pick(Alerts.service_effects())
alert_start_time = time_before(time)

alerts = [
Expand Down Expand Up @@ -198,7 +195,8 @@ defmodule Dotcom.SystemStatus.SubwayTest do
# Sorted in reverse order in order to validate that the sorting
# logic works
[effect2, effect1] =
Faker.Util.sample_uniq(2, fn -> Faker.Util.pick(@effects) end) |> Enum.sort(:desc)
Faker.Util.sample_uniq(2, fn -> Faker.Util.pick(Alerts.service_effects()) end)
|> Enum.sort(:desc)

alerts = [
current_alert(route_id: affected_route_id, time: time, effect: effect1),
Expand Down Expand Up @@ -248,7 +246,7 @@ defmodule Dotcom.SystemStatus.SubwayTest do

time = time_today()

effect = Faker.Util.pick(@effects)
effect = Faker.Util.pick(Alerts.service_effects())

alerts = [
current_alert(route_id: affected_route_id, time: time, effect: effect),
Expand All @@ -274,7 +272,7 @@ defmodule Dotcom.SystemStatus.SubwayTest do
time = time_today()
start_time = time_after(time)

effect = Faker.Util.pick(@effects)
effect = Faker.Util.pick(Alerts.service_effects())

alerts = [
future_alert(route_id: affected_route_id, start_time: start_time, effect: effect),
Expand All @@ -299,10 +297,10 @@ defmodule Dotcom.SystemStatus.SubwayTest do
# Setup
time = time_today()

effect = Faker.Util.pick(@effects)
effect = Faker.Util.pick(Alerts.service_effects())

alerts =
@green_line_branches
GreenLine.branch_ids()
|> Enum.map(fn route_id ->
current_alert(route_id: route_id, time: time, effect: effect)
end)
Expand All @@ -321,10 +319,10 @@ defmodule Dotcom.SystemStatus.SubwayTest do

test "splits separate branches of the green line out as sub_routes if some have alerts and others don't" do
# Setup
affected_branch_id = Faker.Util.pick(@green_line_branches)
affected_branch_id = Faker.Util.pick(GreenLine.branch_ids())

time = time_today()
effect = Faker.Util.pick(@effects)
effect = Faker.Util.pick(Alerts.service_effects())

alerts = [current_alert(route_id: affected_branch_id, effect: effect, time: time)]

Expand All @@ -342,18 +340,18 @@ defmodule Dotcom.SystemStatus.SubwayTest do

test "includes an 'Normal Service' entry for non-affected green line branches" do
# Setup
affected_branch_id = Faker.Util.pick(@green_line_branches)
affected_branch_id = Faker.Util.pick(GreenLine.branch_ids())

time = time_today()
effect = Faker.Util.pick(@effects)
effect = Faker.Util.pick(Alerts.service_effects())

alerts = [current_alert(route_id: affected_branch_id, effect: effect, time: time)]

# Exercise
groups = Subway.subway_status(alerts, time)

# Verify
normal_branch_ids = @green_line_branches |> List.delete(affected_branch_id)
normal_branch_ids = GreenLine.branch_ids() |> List.delete(affected_branch_id)

statuses =
groups
Expand All @@ -365,18 +363,18 @@ defmodule Dotcom.SystemStatus.SubwayTest do

test "sorts alerts ahead of 'Normal Service'" do
# Setup
affected_branch_id = Faker.Util.pick(@green_line_branches)
affected_branch_id = Faker.Util.pick(GreenLine.branch_ids())

time = time_today()
effect = Faker.Util.pick(@effects)
effect = Faker.Util.pick(Alerts.service_effects())

alerts = [current_alert(route_id: affected_branch_id, effect: effect, time: time)]

# Exercise
groups = Subway.subway_status(alerts, time)

# Verify
normal_branch_ids = @green_line_branches |> List.delete(affected_branch_id)
normal_branch_ids = GreenLine.branch_ids() |> List.delete(affected_branch_id)

branch_ids =
groups
Expand All @@ -392,10 +390,12 @@ defmodule Dotcom.SystemStatus.SubwayTest do
test "sorts branches that do have alerts lexically by branch ID" do
# Setup
[affected_branch_id1, affected_branch_id2] =
Faker.Util.sample_uniq(2, fn -> Faker.Util.pick(@green_line_branches) end)
Faker.Util.sample_uniq(2, fn -> Faker.Util.pick(GreenLine.branch_ids()) end)

time = time_today()
[effect1, effect2] = Faker.Util.sample_uniq(2, fn -> Faker.Util.pick(@effects) end)

[effect1, effect2] =
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),
Expand Down Expand Up @@ -439,7 +439,7 @@ defmodule Dotcom.SystemStatus.SubwayTest do
test "shows Mattapan as a branch of Red if it has an alert" do
# Setup
time = time_today()
effect = Faker.Util.pick(@effects)
effect = Faker.Util.pick(Alerts.service_effects())

alerts = [current_alert(route_id: "Mattapan", effect: effect, time: time)]

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

Alert.build(:alert,
Expand Down

0 comments on commit 7bf6227

Please sign in to comment.