Skip to content

Commit

Permalink
test: Property tests for TripUpdate and VehiclePosition modules
Browse files Browse the repository at this point in the history
  • Loading branch information
jzimbel-mbta committed Aug 23, 2024
1 parent ff2a8df commit 6d406db
Show file tree
Hide file tree
Showing 7 changed files with 873 additions and 9 deletions.
46 changes: 37 additions & 9 deletions lib/transit_data/glides_report/trip_update.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ defmodule TransitData.GlidesReport.TripUpdate do

alias TransitData.GlidesReport

@doc """
Cleans up a TripUpdate parsed from raw JSON. (With keys not yet converted to
atoms)
Returns nil if there is no data relevant to Glides terminals in the TripUpdate.
- Canceled TripUpdates are discarded.
- Nonrevenue TripUpdates are discarded.
- `.trip_update.timestamp` is replaced with the given `header_timestamp` if
missing or nil
- `.trip_update.stop_time_update` list is filtered to non-skipped entries with
defined departure times, at Glides terminal stops. If the filtered list is
empty, the entire TripUpdate is discarded.
- All unused fields are removed.
"""
@spec clean_up(map, integer) :: map | nil
def clean_up(tr_upd, header_timestamp)

def clean_up(%{"trip_update" => %{"trip" => %{"schedule_relationship" => "CANCELED"}}}, _) do
Expand Down Expand Up @@ -46,14 +62,16 @@ defmodule TransitData.GlidesReport.TripUpdate do
end

defp clean_up_stop_times(stop_times) do
glides_terminals = GlidesReport.Terminals.all_first_stops()

stop_times
# Ignore stop times that aren't relevant to Glides terminals.
|> Stream.reject(&(&1["stop_id"] not in GlidesReport.Terminals.all_stops()))
# Select stop times that have departure times and aren't skipped.
|> Stream.filter(fn stop_time ->
has_departure_time = not is_nil(stop_time["departure"]["time"])
is_skipped = stop_time["schedule_relationship"] == "SKIPPED"
has_departure_time and not is_skipped
cond do
is_nil(stop_time["departure"]["time"]) -> false
stop_time["schedule_relationship"] == "SKIPPED" -> false
stop_time["stop_id"] not in glides_terminals -> false
:else -> true
end
end)
# Prune all but the relevant fields.
|> Enum.map(fn
Expand Down Expand Up @@ -81,8 +99,18 @@ defmodule TransitData.GlidesReport.TripUpdate do
def filter_by_advance_notice(tr_upd, min_advance_notice_sec) do
time_of_creation = tr_upd.trip_update.timestamp

update_in(tr_upd.trip_update.stop_time_update, fn stop_times ->
Enum.filter(stop_times, &(&1.departure.time - time_of_creation >= min_advance_notice_sec))
end)
filtered_stop_times =
Enum.filter(
tr_upd.trip_update.stop_time_update,
&(&1.departure.time - time_of_creation >= min_advance_notice_sec)
)

case filtered_stop_times do
[] ->
nil

filtered_stop_times ->
put_in(tr_upd.trip_update.stop_time_update, filtered_stop_times)
end
end
end
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ defmodule TransitData.MixProject do
{:dialyxir, "~> 1.4", only: [:dev, :test], runtime: false},
{:lcov_ex, "~> 0.3", only: [:dev, :test], runtime: false},
{:mox, "~> 1.0", only: :test},
{:stream_data, "~> 1.0", only: :test},
# Provided by Mix.install invocation in the notebook.
# We only need to directly get this dep when running tests.
{:tz, "~> 0.26.5", only: [:test]}
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"nimble_ownership": {:hex, :nimble_ownership, "1.0.0", "3f87744d42c21b2042a0aa1d48c83c77e6dd9dd357e425a038dd4b49ba8b79a1", [:mix], [], "hexpm", "7c16cc74f4e952464220a73055b557a273e8b1b7ace8489ec9d86e9ad56cb2cc"},
"parse_trans": {:hex, :parse_trans, "3.4.1", "6e6aa8167cb44cc8f39441d05193be6e6f4e7c2946cb2759f015f8c56b76e5ff", [:rebar3], [], "hexpm", "620a406ce75dada827b82e453c19cf06776be266f5a67cff34e1ef2cbb60e49a"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"},
"stream_data": {:hex, :stream_data, "1.1.1", "fd515ca95619cca83ba08b20f5e814aaf1e5ebff114659dc9731f966c9226246", [:mix], [], "hexpm", "45d0cd46bd06738463fd53f22b70042dbb58c384bb99ef4e7576e7bb7d3b8c8c"},
"stream_gzip": {:hex, :stream_gzip, "0.4.2", "838044dd31dcb15d0a29e1c80b82835c0e7fd5ab81baac328c84e0266c35b9d0", [:mix], [], "hexpm", "3657a5d68c6700b24160b793d74e039074b799abd30ba0727a0c2084a0d3e100"},
"sweet_xml": {:hex, :sweet_xml, "0.7.4", "a8b7e1ce7ecd775c7e8a65d501bc2cd933bff3a9c41ab763f5105688ef485d08", [:mix], [], "hexpm", "e7c4b0bdbf460c928234951def54fe87edf1a170f6896675443279e2dbeba167"},
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
Expand Down
Loading

0 comments on commit 6d406db

Please sign in to comment.