diff --git a/lib/dotcom_web/controllers/schedule/trip_info.ex b/lib/dotcom_web/controllers/schedule/trip_info.ex index d06b9bdab6..ab27fdd5d9 100644 --- a/lib/dotcom_web/controllers/schedule/trip_info.ex +++ b/lib/dotcom_web/controllers/schedule/trip_info.ex @@ -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 @@ -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, diff --git a/test/dotcom_web/controllers/schedule/trip_info_test.exs b/test/dotcom_web/controllers/schedule/trip_info_test.exs index 9850382fa8..d82f16f662 100644 --- a/test/dotcom_web/controllers/schedule/trip_info_test.exs +++ b/test/dotcom_web/controllers/schedule/trip_info_test.exs @@ -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 @@ -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") @@ -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 = @@ -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 @@ -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, @@ -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 @@ -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") @@ -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"}, @@ -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} @@ -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"}, @@ -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 = [