Skip to content

Commit

Permalink
Handle nil values in vehicle/ghost property matching (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadyan authored Mar 16, 2020
1 parent 3c20c53 commit 6e2114c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/realtime/ghost.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Realtime.Ghost do
trip_id: Trip.id(),
headsign: String.t(),
block_id: Block.id(),
run_id: Run.id(),
run_id: Run.id() | nil,
via_variant: RoutePattern.via_variant() | nil,
layover_departure_time: Util.Time.timestamp() | nil,
scheduled_timepoint_status: TimepointStatus.timepoint_status(),
Expand Down
4 changes: 3 additions & 1 deletion lib/realtime/vehicle_or_ghost.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ defmodule Realtime.VehicleOrGhost do
|> matches?(text)
end

@spec matches?(String.t(), String.t()) :: boolean()
@spec matches?(String.t() | nil, String.t()) :: boolean()
defp matches?(nil, _text), do: false

defp matches?("999-0" <> shuttle_run, text) do
# special case to match shuttle runs even without the leading 0
matches?("999" <> shuttle_run, text) || matches?("9990" <> shuttle_run, text)
Expand Down
6 changes: 6 additions & 0 deletions test/realtime/vehicle_or_ghost_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,11 @@ defmodule Realtime.VehicleOrGhostTest do
test "short circuits to an empty result if trying to match on the empty string" do
assert VehicleOrGhost.find_by(@vehicles, %{text: "", property: :all}) == []
end

test "handles nil values" do
ghost_with_nil_run_id = %{@ghost | run_id: nil}

assert VehicleOrGhost.find_by([ghost_with_nil_run_id], %{text: "710", property: :all}) == []
end
end
end

0 comments on commit 6e2114c

Please sign in to comment.