From 18605d4603eaeaff3547942a12ca98a6d813470b Mon Sep 17 00:00:00 2001 From: Hannah Purcell Date: Mon, 6 Jan 2025 11:45:48 -0500 Subject: [PATCH] fix: don't mix-match route name and id! --- lib/skate/detours/detours.ex | 12 +++++------ .../channels/detours_channel_test.exs | 4 ++-- .../controllers/detours_controller_test.exs | 6 ++++++ test/support/factories/detour_factory.ex | 20 +++++++++++++++---- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/lib/skate/detours/detours.ex b/lib/skate/detours/detours.ex index 9582c97a1..f95c22290 100644 --- a/lib/skate/detours/detours.ex +++ b/lib/skate/detours/detours.ex @@ -40,7 +40,7 @@ defmodule Skate.Detours.Detours do def active_detours_by_route(route_id) do list_detours() |> Enum.filter(fn detour -> - categorize_detour(detour) == :active and get_detour_route(detour) == route_id + categorize_detour(detour) == :active and get_detour_route_id(detour) == route_id end) |> Enum.map(fn detour -> db_detour_to_detour(detour) end) end @@ -131,9 +131,9 @@ defmodule Skate.Detours.Detours do def categorize_detour(_detour_context), do: :draft - @spec get_detour_route(detour :: map()) :: String.t() - defp get_detour_route(%{state: %{"context" => %{"route" => %{"name" => route_name}}}}), - do: route_name + @spec get_detour_route_id(detour :: map()) :: String.t() + defp get_detour_route_id(%{state: %{"context" => %{"route" => %{"id" => route_id}}}}), + do: route_id @doc """ Gets a single detour. @@ -264,7 +264,7 @@ defmodule Skate.Detours.Detours do |> User.get_by_id!() |> Map.get(:uuid) - route_id = get_detour_route(detour) + route_id = get_detour_route_id(detour) Phoenix.PubSub.broadcast( Skate.PubSub, @@ -286,7 +286,7 @@ defmodule Skate.Detours.Detours do end defp broadcast_detour(:past, detour, _author_id) do - route_id = get_detour_route(detour) + route_id = get_detour_route_id(detour) Phoenix.PubSub.broadcast( Skate.PubSub, diff --git a/test/skate_web/channels/detours_channel_test.exs b/test/skate_web/channels/detours_channel_test.exs index d0086002f..7f523de76 100644 --- a/test/skate_web/channels/detours_channel_test.exs +++ b/test/skate_web/channels/detours_channel_test.exs @@ -30,10 +30,10 @@ defmodule SkateWeb.DetoursChannelTest do draft_detour = :detour_snapshot |> build() |> with_id(1) active_detour_one = - :detour_snapshot |> build() |> activated |> with_id(2) |> with_route_name("57") + :detour_snapshot |> build() |> activated |> with_id(2) |> with_route("57") active_detour_two = - :detour_snapshot |> build() |> activated |> with_id(3) |> with_route_name("66") + :detour_snapshot |> build() |> activated |> with_id(3) |> with_route("66") past_detour = :detour_snapshot |> build() |> deactivated |> with_id(4) diff --git a/test/skate_web/controllers/detours_controller_test.exs b/test/skate_web/controllers/detours_controller_test.exs index 9a8ba03ca..996ce393f 100644 --- a/test/skate_web/controllers/detours_controller_test.exs +++ b/test/skate_web/controllers/detours_controller_test.exs @@ -132,6 +132,7 @@ defmodule SkateWeb.DetoursControllerTest do "snapshot" => %{ "context" => %{ "route" => %{ + "id" => "23", "name" => "23", "directionNames" => %{ "0" => "Outbound", @@ -154,6 +155,7 @@ defmodule SkateWeb.DetoursControllerTest do "snapshot" => %{ "context" => %{ "route" => %{ + "id" => "47", "name" => "47", "directionNames" => %{ "0" => "Outbound", @@ -176,6 +178,7 @@ defmodule SkateWeb.DetoursControllerTest do "snapshot" => %{ "context" => %{ "route" => %{ + "id" => "75", "name" => "75", "directionNames" => %{ "0" => "Outbound", @@ -358,6 +361,7 @@ defmodule SkateWeb.DetoursControllerTest do Detours.upsert_from_snapshot(other_user.id, %{ "context" => %{ "route" => %{ + # "id" => "23", "name" => "23", "directionNames" => %{ "0" => "Outbound", @@ -423,6 +427,7 @@ defmodule SkateWeb.DetoursControllerTest do "snapshot" => %{ "context" => %{ "route" => %{ + "id" => "23", "name" => "23", "directionNames" => %{ "0" => "Outbound", @@ -444,6 +449,7 @@ defmodule SkateWeb.DetoursControllerTest do "snapshot" => %{ "context" => %{ "route" => %{ + "id" => "23", "name" => "23" }, "routePattern" => %{ diff --git a/test/support/factories/detour_factory.ex b/test/support/factories/detour_factory.ex index 132563a47..ff1e06911 100644 --- a/test/support/factories/detour_factory.ex +++ b/test/support/factories/detour_factory.ex @@ -26,6 +26,7 @@ defmodule Skate.DetourFactory do "context" => %{ "uuid" => nil, "route" => %{ + "id" => sequence("detour_route_id:"), "name" => sequence("detour_route_name:"), "directionNames" => %{ "0" => "Outbound", @@ -74,12 +75,23 @@ defmodule Skate.DetourFactory do put_in(state["value"], %{"Detour Drawing" => "Past"}) end - def with_route_name(%Skate.Detours.Db.Detour{} = detour, route_name) do - %{detour | state: with_route_name(detour.state, route_name)} + def with_route(%Skate.Detours.Db.Detour{} = detour, route) do + %{detour | state: with_route(detour.state, route)} end - def with_route_name(%{"context" => %{"route" => %{"name" => _}}} = state, route_name) do - put_in(state["context"]["route"]["name"], route_name) + def with_route( + %{ + "context" => %{ + "route" => %{"name" => _, "id" => _, "directionNames" => direction_names} + } + } = state, + route + ) do + put_in(state["context"]["route"], %{ + "name" => route, + "id" => route, + "directionNames" => direction_names + }) end def with_direction(%Skate.Detours.Db.Detour{} = detour, direction) do