Skip to content

Commit

Permalink
tests: add tests for updating shuttle route, shuttle route shape id
Browse files Browse the repository at this point in the history
  • Loading branch information
meagharty committed Oct 24, 2024
1 parent d2244c8 commit 29a9e60
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 7 deletions.
24 changes: 18 additions & 6 deletions lib/arrow/shuttles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,15 @@ defmodule Arrow.Shuttles do
"""
def create_shuttle(attrs \\ %{}) do
%Shuttle{}
|> Shuttle.changeset(attrs)
|> Repo.insert()
created_shuttle =
%Shuttle{}
|> Shuttle.changeset(attrs)
|> Repo.insert()

case created_shuttle do
{:ok, shuttle} -> {:ok, Repo.preload(shuttle, routes: [:shape])}
err -> err
end
end

@doc """
Expand All @@ -297,9 +303,15 @@ defmodule Arrow.Shuttles do
"""
def update_shuttle(%Shuttle{} = shuttle, attrs) do
shuttle
|> Shuttle.changeset(attrs)
|> Repo.update()
updated_shuttle =
shuttle
|> Shuttle.changeset(attrs)
|> Repo.update()

case updated_shuttle do
{:ok, shuttle} -> {:ok, Repo.preload(shuttle, routes: [:shape])}
err -> err
end
end

@doc """
Expand Down
46 changes: 46 additions & 0 deletions test/arrow/shuttles_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,52 @@ defmodule Arrow.ShuttlesTest do
assert shuttle.shuttle_name == "some updated shuttle_name"
end

test "update_shuttle/2 with valid route data updates the shuttle route" do
shuttle = shuttle_fixture()
[route1, route2] = shuttle.routes
destination = unique_shuttle_route_destination()
updated_route1 = Map.merge(route1, %{destination: destination})

update_attrs =
Map.from_struct(%Shuttle{
shuttle
| routes: [Map.from_struct(updated_route1), Map.from_struct(route2)]
})

assert {:ok, %Shuttle{} = shuttle} = Shuttles.update_shuttle(shuttle, update_attrs)
assert List.first(shuttle.routes).id == route1.id
assert List.first(shuttle.routes).destination == destination
end

test "update_shuttle/2 with valid shape_id updates the shuttle route shape" do
shuttle = shuttle_fixture()
routes = shuttle.routes
first_route = List.first(routes)
new_shape = shape_fixture()
# Updated shape is set by shape_id param
updated_route1 = Map.merge(List.first(routes), %{shape_id: new_shape.id})
existing_route2 = Enum.at(routes, 1)

update_attrs =
Map.from_struct(%Shuttle{
shuttle
| routes: [Map.from_struct(updated_route1), Map.from_struct(existing_route2)]
})

assert {:ok, %Shuttle{} = updated_shuttle} = Shuttles.update_shuttle(shuttle, update_attrs)
# Shuttle id is the same
assert updated_shuttle.id == shuttle.id
# Existing route is unchanged
assert Enum.at(updated_shuttle.routes, 1) == existing_route2
# Route definition id is the same
updated_shuttle_route = List.first(updated_shuttle.routes)
assert updated_shuttle_route.id == first_route.id
# Shape reference is updated
refute updated_shuttle_route.shape == first_route.shape
assert updated_shuttle_route.shape.id == updated_route1.shape_id
assert updated_shuttle_route.shape == new_shape
end

test "update_shuttle/2 with invalid data updates for status active returns error changeset" do
shuttle = shuttle_fixture()
update_attrs = %{status: :active, shuttle_name: "some updated shuttle_name"}
Expand Down
34 changes: 33 additions & 1 deletion test/support/fixtures/shuttles_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,38 @@ defmodule Arrow.ShuttlesFixtures do
"""
def unique_shuttle_shuttle_name, do: "some shuttle_name#{System.unique_integer([:positive])}"

@doc """
Generate a unique shuttle route destination.
"""
def unique_shuttle_route_destination,
do: "some shuttle_route_destination#{System.unique_integer([:positive])}"

defp shuttle_routes do
shape1 = shape_fixture()
shape2 = shape_fixture()

[
%{
shape_id: shape1.id,
shape: shape1,
destination: "Harvard",
direction_id: :"0",
direction_desc: "Southbound",
suffix: nil,
waypoint: "Brattle"
},
%{
shape_id: shape2.id,
shape: shape2,
destination: "Alewife",
direction_id: :"1",
direction_desc: "Northbound",
suffix: nil,
waypoint: "Brattle"
}
]
end

@doc """
Generate a shuttle.
"""
Expand All @@ -70,7 +102,7 @@ defmodule Arrow.ShuttlesFixtures do
|> Enum.into(%{
shuttle_name: unique_shuttle_shuttle_name(),
status: :draft,
routes: []
routes: shuttle_routes()
})
|> Arrow.Shuttles.create_shuttle()

Expand Down

0 comments on commit 29a9e60

Please sign in to comment.