Skip to content

Commit

Permalink
Added test pattern to trip_info
Browse files Browse the repository at this point in the history
  • Loading branch information
kotva006 committed May 9, 2024
1 parent fed8c3f commit c7156f6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
7 changes: 4 additions & 3 deletions lib/dotcom_web/controllers/schedule/trip_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ defmodule DotcomWeb.ScheduleController.TripInfo do
alias Routes.Route
alias DotcomWeb.ScheduleController.VehicleLocations

@predictions_repo Application.compile_env!(:dotcom, :repo_modules)[:predictions]

@default_opts [
trip_fn: &Schedules.Repo.schedule_for_trip/2,
vehicle_fn: &Vehicles.Repo.trip/1,
prediction_fn: &Predictions.Repo.all/1
vehicle_fn: &Vehicles.Repo.trip/1
]

@impl true
Expand Down Expand Up @@ -96,7 +97,7 @@ defmodule DotcomWeb.ScheduleController.TripInfo do
case opts[:trip_fn].(trip_id, date: conn.assigns.date) do
trips when is_list(trips) ->
trips
|> build_trip_times(conn.assigns, trip_id, opts[:prediction_fn])
|> build_trip_times(conn.assigns, trip_id, Function.capture(@predictions_repo, :all, 1))
|> TripInfo.from_list(
vehicle: opts[:vehicle_fn].(trip_id),
vehicle_stop_name: active_stop,
Expand Down
20 changes: 16 additions & 4 deletions test/dotcom_web/controllers/schedule/trip_info_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule DotcomWeb.ScheduleController.TripInfoTest do
use DotcomWeb.ConnCase, async: true
import DotcomWeb.ScheduleController.TripInfo
import Mox
alias DotcomWeb.ScheduleController.TripInfo
alias Schedules.{Schedule, Trip}
alias Stops.Stop
Expand Down Expand Up @@ -218,7 +219,7 @@ defmodule DotcomWeb.ScheduleController.TripInfoTest do
end

defp conn_builder(conn, schedules, params \\ []) do
init = init(trip_fn: &trip_fn/2, vehicle_fn: &vehicle_fn/1, prediction_fn: &prediction_fn/1)
init = init(trip_fn: &trip_fn/2, vehicle_fn: &vehicle_fn/1)
query_params = Map.new(params, fn {key, val} -> {Atom.to_string(key), val} end)
params = put_in(query_params["route"], "1")

Expand Down Expand Up @@ -250,6 +251,7 @@ defmodule DotcomWeb.ScheduleController.TripInfoTest do
end

test "assigns trip_info when origin/destination are selected", %{conn: conn} do
expect(Predictions.Repo.Mock, :all, &prediction_fn/1)
expected_stops = ["after_first", "1", "2", "3", "new_last"]

conn =
Expand All @@ -263,6 +265,7 @@ defmodule DotcomWeb.ScheduleController.TripInfoTest do
end

test "assigns the total number of stops", %{conn: conn} do
expect(Predictions.Repo.Mock, :all, 2, &prediction_fn/1)
conn = conn_builder(conn, [], trip: "long_trip")
assert conn.assigns[:trip_info].stop_count == 7

Expand All @@ -276,6 +279,8 @@ defmodule DotcomWeb.ScheduleController.TripInfoTest do
end

test "returns nil if we can't generate a trip info", %{conn: conn} do
expect(Predictions.Repo.Mock, :all, &prediction_fn/1)

conn =
conn_builder(
conn,
Expand All @@ -290,6 +295,7 @@ defmodule DotcomWeb.ScheduleController.TripInfoTest do
end

test "does not redirect if we didn't have a trip already", %{conn: conn} do
expect(Predictions.Repo.Mock, :all, &prediction_fn/1)
conn = conn_builder(conn, @schedules, origin: "fake", destination: "fake")
refute conn.halted
refute conn.assigns.trip_info
Expand Down Expand Up @@ -324,6 +330,8 @@ defmodule DotcomWeb.ScheduleController.TripInfoTest do
end

test "Trip predictions are fetched if date is service day", %{conn: conn} do
expect(Predictions.Repo.Mock, :all, &prediction_fn/1)

conn =
conn
|> conn_builder([], trip: "long_trip")
Expand Down Expand Up @@ -372,6 +380,8 @@ defmodule DotcomWeb.ScheduleController.TripInfoTest do
end

test "Default Trip id is taken from journeys if one is not provided", %{conn: conn} do
expect(Predictions.Repo.Mock, :all, &prediction_fn/1)

schedules = [
%Schedule{
trip: %Trip{id: "32893585"},
Expand All @@ -393,7 +403,7 @@ defmodule DotcomWeb.ScheduleController.TripInfoTest do
}
]

init = init(trip_fn: &trip_fn/2, vehicle_fn: &vehicle_fn/1, prediction_fn: &prediction_fn/1)
init = init(trip_fn: &trip_fn/2, vehicle_fn: &vehicle_fn/1)

conn =
%{conn | request_path: schedule_path(conn, :show, "66"), query_params: nil}
Expand All @@ -407,8 +417,9 @@ defmodule DotcomWeb.ScheduleController.TripInfoTest do
end
end

@tag :external
test "does assign trips for the subway if the date is today", %{conn: conn} do
expect(Predictions.Repo.Mock, :all, &prediction_fn/1)

schedules = [
%Schedule{
trip: %Trip{id: "32893585"},
Expand Down Expand Up @@ -578,7 +589,8 @@ defmodule DotcomWeb.ScheduleController.TripInfoTest do

describe "test that wollaston station is properly inserted when expected" do
test "Does not add Wollaston to non Red line routes", %{conn: conn} do
init = init(trip_fn: &trip_fn/2, vehicle_fn: &vehicle_fn/1, prediction_fn: &prediction_fn/1)
expect(Predictions.Repo.Mock, :all, &prediction_fn/1)
init = init(trip_fn: &trip_fn/2, vehicle_fn: &vehicle_fn/1)
route = %{id: "Not-Red"}

times = [
Expand Down

0 comments on commit c7156f6

Please sign in to comment.