Skip to content

Commit

Permalink
fix: prediction ids should be unique by route, for multi-route trips
Browse files Browse the repository at this point in the history
  • Loading branch information
meagharty committed Sep 30, 2024
1 parent 11dfa42 commit 0cc1d78
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions apps/api_web/lib/api_web/views/prediction_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ defmodule ApiWeb.PredictionView do
optional_relationship("stop", stop_id, &State.Stop.by_id/1, conn)
end

def id(%{trip_id: trip_id, stop_id: stop_id, stop_sequence: seq}, _conn) do
"prediction-#{trip_id}-#{stop_id}-#{seq}"
def id(%{trip_id: trip_id, stop_id: stop_id, stop_sequence: seq, route_id: route_id}, _conn) do
"prediction-#{trip_id}-#{stop_id}-#{seq}-#{route_id}"
end

def schedule(%{schedule: schedule}, _conn), do: schedule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ defmodule ApiWeb.TripControllerTest do
"id" => "trip",
"relationships" => %{
"predictions" => %{
"data" => [%{"id" => "prediction-trip-stop-", "type" => "prediction"}]
"data" => [%{"id" => "prediction-trip-stop--", "type" => "prediction"}]
}
}
} = json_response(conn, 200)["data"]
Expand Down
6 changes: 6 additions & 0 deletions apps/api_web/test/api_web/views/prediction_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ defmodule ApiWeb.PredictionViewTest do
}
end

test "includes a unique record identifier by trip, stop, stop seq, and route", %{conn: conn} do
rendered = render(ApiWeb.PredictionView, "index.json-api", data: @prediction, conn: conn)

assert rendered["data"]["id"] == "prediction-trip-North Station-02-5-CR-Lowell"
end

test "includes trip/stop/route/vehicle relationships by default", %{conn: conn} do
rendered = render(ApiWeb.PredictionView, "index.json-api", data: @prediction, conn: conn)

Expand Down
2 changes: 1 addition & 1 deletion apps/api_web/test/api_web/views/schedule_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ defmodule ApiWeb.ScheduleViewTest do
|> render("index.json-api", data: @schedule, conn: conn, opts: %{include: "prediction"})
|> get_in(["data", "relationships", "prediction", "data", "id"])

assert prediction_id == "prediction-trip-stop-1"
assert prediction_id == "prediction-trip-stop-1-route"
end

test "does not return a prediction if not explicitly requested", %{conn: conn} do
Expand Down

0 comments on commit 0cc1d78

Please sign in to comment.