Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cancel trips with a boarding status of Canceled #364

Merged
merged 4 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/concentrate/group_filter/cancelled_trip.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Concentrate.GroupFilter.CancelledTrip do
@impl Concentrate.GroupFilter
def filter(trip_group, module \\ CancelledTrips, routes_module \\ Routes)

# credo:disable-for-next-line Credo.Check.Refactor.CyclomaticComplexity
def filter(
{%TripDescriptor{} = td, _vps, [stu | _] = stop_time_updates} = group,
module,
Expand All @@ -26,6 +27,9 @@ defmodule Concentrate.GroupFilter.CancelledTrip do
bus_block_waiver?(stop_time_updates, routes_module.route_type(route_id)) ->
cancel_group(group)

cancelled_cr_trip?(stop_time_updates, routes_module.route_type(route_id)) ->
cancel_group(group)

is_nil(time) ->
group

Expand All @@ -48,6 +52,12 @@ defmodule Concentrate.GroupFilter.CancelledTrip do

defp bus_block_waiver?(_, _), do: false

defp cancelled_cr_trip?(stop_time_updates, 2) do
Enum.all?(stop_time_updates, &StopTimeUpdate.boarding_status_cancelled?/1)
end

defp cancelled_cr_trip?(_, _), do: false

defp cancel_group({td, vps, stus}) do
td = TripDescriptor.cancel(td)
stus = Enum.map(stus, &StopTimeUpdate.skip/1)
Expand Down
5 changes: 5 additions & 0 deletions lib/concentrate/stop_time_update.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ defmodule Concentrate.StopTimeUpdate do
schedule_relationship == :SKIPPED
end

@spec boarding_status_cancelled?(%__MODULE__{}) :: boolean()
def boarding_status_cancelled?(%__MODULE__{status: boarding_status}) do
boarding_status == "Cancelled"
end

defimpl Concentrate.Mergeable do
require Logger

Expand Down
46 changes: 46 additions & 0 deletions test/concentrate/group_filter/cancelled_trip_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,52 @@ defmodule Concentrate.GroupFilter.CancelledTripTest do
assert StopTimeUpdate.schedule_relationship(stu_actual2) == :SCHEDULED
end

test "cancels the group if all stop updates have already been given a cancelled boarding status" do
td =
TripDescriptor.new(
route_id: "CR-Test",
trip_id: "trip",
start_date: {1970, 1, 2}
)

stu =
StopTimeUpdate.new(
trip_id: "trip",
status: "Cancelled",
arrival_time: 87_000,
schedule_relationship: :SCHEDULED
)

group = {td, [], [stu, stu]}
{td_actual, [], [stu_actual1, stu_actual2]} = filter(group, @module, @fake_routes_module)
assert TripDescriptor.schedule_relationship(td_actual) == :CANCELED
assert StopTimeUpdate.schedule_relationship(stu_actual1) == :SKIPPED
assert StopTimeUpdate.schedule_relationship(stu_actual2) == :SKIPPED
end

test "does not cancel the group if all stop updates have already been given a skipped status but route_type is not 2" do
td =
TripDescriptor.new(
route_id: "Red",
trip_id: "red_trip",
start_date: {1970, 1, 2}
)

stu =
StopTimeUpdate.new(
trip_id: "red_trip",
status: "Cancelled",
arrival_time: 87_000,
schedule_relationship: :SCHEDULED
)

group = {td, [], [stu, stu]}
{td_actual, [], [stu_actual1, stu_actual2]} = filter(group, @module, @fake_routes_module)
assert TripDescriptor.schedule_relationship(td_actual) == :SCHEDULED
assert StopTimeUpdate.schedule_relationship(stu_actual1) == :SCHEDULED
assert StopTimeUpdate.schedule_relationship(stu_actual2) == :SCHEDULED
end

test "leaves non-cancelled trips alone" do
td =
TripDescriptor.new(
Expand Down
14 changes: 14 additions & 0 deletions test/concentrate/stop_time_update_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ defmodule Concentrate.StopTimeUpdateTest do
end
end

describe "boarding_status_cancelled?/1" do
test "returns false if stop time update status is not cancelled" do
on_time = update_status(@stu, "On time")

assert boarding_status_cancelled?(on_time) == false
end

test "returns true if stop time update status is 'Cancelled'" do
cancelled = update_status(@stu, "Cancelled")

assert boarding_status_cancelled?(cancelled) == true
end
end

describe "Concentrate.Mergeable" do
test "takes non-nil values, uses arrival/departure times from earliest arrival." do
first = @stu
Expand Down
2 changes: 2 additions & 0 deletions test/support/filter/fakes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ end

defmodule Concentrate.GTFS.FakeRoutes do
@moduledoc "Fake implementation of GTFS.Routes"
def route_type("CR-" <> _cr_line), do: 2

def route_type(route_id) when is_binary(route_id) do
case Integer.parse(route_id) do
:error -> nil
Expand Down
Loading