From b3be441325c8c0ecfb9976f79ed96d33f8926cb9 Mon Sep 17 00:00:00 2001 From: kotva006 Date: Mon, 13 May 2024 13:53:51 -0500 Subject: [PATCH] Added more factory usage for predictions and some random data --- .../controllers/schedule/trip_info.ex | 12 +-- test/dotcom/transit_near_me_test.exs | 87 ++++++++-------- .../controllers/schedule/trip_info_test.exs | 98 ++++++++----------- test/predicted_schedule_test.exs | 35 ++++--- 4 files changed, 112 insertions(+), 120 deletions(-) diff --git a/lib/dotcom_web/controllers/schedule/trip_info.ex b/lib/dotcom_web/controllers/schedule/trip_info.ex index ab27fdd5d9..309edbf747 100644 --- a/lib/dotcom_web/controllers/schedule/trip_info.ex +++ b/lib/dotcom_web/controllers/schedule/trip_info.ex @@ -97,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, Function.capture(@predictions_repo, :all, 1)) + |> build_trip_times(conn.assigns, trip_id) |> TripInfo.from_list( vehicle: opts[:vehicle_fn].(trip_id), vehicle_stop_name: active_stop, @@ -149,19 +149,19 @@ defmodule DotcomWeb.ScheduleController.TripInfo do NaiveDateTime.compare(a, b) end - defp build_trip_times(schedules, %{date_time: date_time} = assigns, trip_id, prediction_fn) do + defp build_trip_times(schedules, %{date_time: date_time} = assigns, trip_id) do assigns - |> get_trip_predictions(Util.service_date(date_time), trip_id, prediction_fn) + |> get_trip_predictions(Util.service_date(date_time), trip_id) |> PredictedSchedule.group(schedules) end - defp get_trip_predictions(%{date: date}, service_date, _, _prediction_fn) + defp get_trip_predictions(%{date: date}, service_date, _trip_id) when date != service_date do [] end - defp get_trip_predictions(_, _, trip_id, prediction_fn) do - prediction_fn.(trip: trip_id) + defp get_trip_predictions(_, _, trip_id) do + @predictions_repo.all(trip: trip_id) end @spec show_trips?(DateTime.t(), DateTime.t(), integer, String.t()) :: boolean diff --git a/test/dotcom/transit_near_me_test.exs b/test/dotcom/transit_near_me_test.exs index 235bdc2b26..baf80ee8f3 100644 --- a/test/dotcom/transit_near_me_test.exs +++ b/test/dotcom/transit_near_me_test.exs @@ -2,6 +2,7 @@ defmodule Dotcom.TransitNearMeTest do use ExUnit.Case import Mox + import Test.Support.Factory.Prediction alias LocationService.Address alias Predictions.Prediction @@ -670,19 +671,23 @@ defmodule Dotcom.TransitNearMeTest do time: @schedule_time1 } - @prediction1 %Prediction{ - departing?: true, - direction_id: 1, - id: "prediction-39783543-70050-60", - route: @route, - schedule_relationship: nil, - status: nil, - stop: @stop, - stop_sequence: 60, - time: @prediction_time1, - track: "2", - trip: @trip1 - } + @track_number "#{Faker.Util.digit()}" + @direction_id Faker.Util.digit() + @stop_sequence Faker.random_between(10, 99) + + @prediction1 build(:prediction, %{ + departing?: true, + direction_id: @direction_id, + id: Faker.Internet.slug(), + route: @route, + schedule_relationship: nil, + status: nil, + stop: @stop, + stop_sequence: @stop_sequence, + time: @prediction_time1, + track: @track_number, + trip: @trip1 + }) @schedule2 %Schedule{ route: @route, @@ -691,19 +696,19 @@ defmodule Dotcom.TransitNearMeTest do time: @schedule_time2 } - @prediction2 %Prediction{ - departing?: true, - direction_id: 1, - id: "prediction-39783543-70050-61", - route: @route, - schedule_relationship: nil, - status: nil, - stop: @stop, - stop_sequence: 60, - time: @prediction_time2, - track: "2", - trip: @trip2 - } + @prediction2 build(:prediction, %{ + departing?: true, + direction_id: @direction_id, + id: Faker.Internet.slug(), + route: @route, + schedule_relationship: nil, + status: nil, + stop: @stop, + stop_sequence: @stop_sequence, + time: @prediction_time2, + track: @track_number, + trip: @trip2 + }) @schedule3 %Schedule{ route: @route, @@ -712,19 +717,19 @@ defmodule Dotcom.TransitNearMeTest do time: @schedule_time3 } - @prediction3 %Prediction{ - departing?: true, - direction_id: 1, - id: "prediction-39783543-70050-61", - route: @route, - schedule_relationship: nil, - status: nil, - stop: @stop, - stop_sequence: 60, - time: @prediction_time3, - track: "2", - trip: @trip3 - } + @prediction3 build(:prediction, %{ + departing?: true, + direction_id: @direction_id, + id: Faker.Internet.slug(), + route: @route, + schedule_relationship: nil, + status: nil, + stop: @stop, + stop_sequence: @stop_sequence, + time: @prediction_time3, + track: @track_number, + trip: @trip3 + }) test "returns time data for the next 2 predictions" do expect(Predictions.Repo.Mock, :all, fn _ -> @@ -747,7 +752,7 @@ defmodule Dotcom.TransitNearMeTest do seconds: 300, status: nil, time: ["5", " ", "min"], - track: "2", + track: @track_number, schedule_relationship: nil }, scheduled_time: nil @@ -770,7 +775,7 @@ defmodule Dotcom.TransitNearMeTest do seconds: 900, status: nil, time: ["15", " ", "min"], - track: "2", + track: @track_number, schedule_relationship: nil }, scheduled_time: nil, diff --git a/test/dotcom_web/controllers/schedule/trip_info_test.exs b/test/dotcom_web/controllers/schedule/trip_info_test.exs index d82f16f662..0f57f52a41 100644 --- a/test/dotcom_web/controllers/schedule/trip_info_test.exs +++ b/test/dotcom_web/controllers/schedule/trip_info_test.exs @@ -1,7 +1,10 @@ defmodule DotcomWeb.ScheduleController.TripInfoTest do use DotcomWeb.ConnCase, async: true + import DotcomWeb.ScheduleController.TripInfo import Mox + import Test.Support.Factory.Prediction + alias DotcomWeb.ScheduleController.TripInfo alias Schedules.{Schedule, Trip} alias Stops.Stop @@ -76,52 +79,27 @@ defmodule DotcomWeb.ScheduleController.TripInfoTest do } ] @predictions [ - %Prediction{ + build(:prediction, %{ trip: %Trip{id: "32893585"}, stop: %Stop{id: "first"} - }, - %Prediction{ + }), + build(:prediction, %{ trip: %Trip{id: "32893585"}, stop: %Stop{id: "last"} - } + }) ] @non_red_predictions [ - %Prediction{ + build(:prediction, %{ direction_id: 0, trip: %Trip{id: "non-red-trip"}, stop: %Stop{id: "id1"} - }, - %Prediction{ + }), + build(:prediction, %{ direction_id: 0, trip: %Trip{id: "non-red-trip"}, stop: %Stop{id: "id2"} - } - ] - - @red_predictions_1 [ - %Prediction{ - direction_id: 1, - trip: %Trip{id: "red-trip-1"}, - stop: %Stop{id: "place-nqncy"} - }, - %Prediction{ - direction_id: 1, - trip: %Trip{id: "red-trip-1"}, - stop: %Stop{id: "place-qnctr"} - } - ] - @red_predictions_0 [ - %Prediction{ - direction_id: 0, - trip: %Trip{id: "red-trip-0"}, - stop: %Stop{id: "place-qnctr"} - }, - %Prediction{ - direction_id: 0, - trip: %Trip{id: "red-trip-0"}, - stop: %Stop{id: "place-nqncy"} - } + }) ] setup %{conn: conn} do @@ -133,21 +111,7 @@ defmodule DotcomWeb.ScheduleController.TripInfoTest do {:ok, %{conn: conn}} end - defp prediction_fn(trip: "non-red-trip") do - Enum.map(@non_red_predictions, &%Prediction{&1 | trip: %Trip{id: "non-red-trip"}}) - end - - defp prediction_fn(trip: "red-trip-0") do - Enum.map(@red_predictions_0, &%Prediction{&1 | trip: %Trip{id: "red-trip-0"}}) - end - - defp prediction_fn(trip: "red-trip-1") do - Enum.map(@red_predictions_1, &%Prediction{&1 | trip: %Trip{id: "red-trip-1"}}) - end - - defp prediction_fn(trip: trip_id) do - Enum.map(@predictions, &%Prediction{&1 | trip: %Trip{id: trip_id}}) - end + setup :verify_on_exit! defp trip_fn("32893585", date: @date) do @trip_schedules @@ -251,7 +215,10 @@ 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) + expect(Predictions.Repo.Mock, :all, fn trip: trip_id -> + Enum.map(@predictions, &%Prediction{&1 | trip: %Trip{id: trip_id}}) + end) + expected_stops = ["after_first", "1", "2", "3", "new_last"] conn = @@ -265,7 +232,10 @@ 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) + expect(Predictions.Repo.Mock, :all, 2, fn trip: trip_id -> + Enum.map(@predictions, &%Prediction{&1 | trip: %Trip{id: trip_id}}) + end) + conn = conn_builder(conn, [], trip: "long_trip") assert conn.assigns[:trip_info].stop_count == 7 @@ -279,7 +249,9 @@ 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) + expect(Predictions.Repo.Mock, :all, fn trip: trip_id -> + Enum.map(@predictions, &%Prediction{&1 | trip: %Trip{id: trip_id}}) + end) conn = conn_builder( @@ -295,7 +267,10 @@ 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) + expect(Predictions.Repo.Mock, :all, fn trip: trip_id -> + Enum.map(@predictions, &%Prediction{&1 | trip: %Trip{id: trip_id}}) + end) + conn = conn_builder(conn, @schedules, origin: "fake", destination: "fake") refute conn.halted refute conn.assigns.trip_info @@ -309,7 +284,7 @@ defmodule DotcomWeb.ScheduleController.TripInfoTest do end init = - init(trip_fn: future_trip_fn, vehicle_fn: &vehicle_fn/1, prediction_fn: &prediction_fn/1) + init(trip_fn: future_trip_fn, vehicle_fn: &vehicle_fn/1) conn = %{ @@ -330,7 +305,9 @@ 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) + expect(Predictions.Repo.Mock, :all, fn trip: trip_id -> + Enum.map(@predictions, &%Prediction{&1 | trip: %Trip{id: trip_id}}) + end) conn = conn @@ -380,7 +357,9 @@ 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) + expect(Predictions.Repo.Mock, :all, fn trip: trip_id -> + Enum.map(@predictions, &%Prediction{&1 | trip: %Trip{id: trip_id}}) + end) schedules = [ %Schedule{ @@ -418,7 +397,9 @@ defmodule DotcomWeb.ScheduleController.TripInfoTest do end test "does assign trips for the subway if the date is today", %{conn: conn} do - expect(Predictions.Repo.Mock, :all, &prediction_fn/1) + expect(Predictions.Repo.Mock, :all, fn trip: trip_id -> + Enum.map(@predictions, &%Prediction{&1 | trip: %Trip{id: trip_id}}) + end) schedules = [ %Schedule{ @@ -589,7 +570,10 @@ 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 - expect(Predictions.Repo.Mock, :all, &prediction_fn/1) + expect(Predictions.Repo.Mock, :all, fn trip: "non-red-trip" -> + Enum.map(@non_red_predictions, &%Prediction{&1 | trip: %Trip{id: "non-red-trip"}}) + end) + init = init(trip_fn: &trip_fn/2, vehicle_fn: &vehicle_fn/1) route = %{id: "Not-Red"} diff --git a/test/predicted_schedule_test.exs b/test/predicted_schedule_test.exs index bd012ac5cc..f09d3753a9 100644 --- a/test/predicted_schedule_test.exs +++ b/test/predicted_schedule_test.exs @@ -1,11 +1,14 @@ defmodule PredictedScheduleTest do use ExUnit.Case, async: false + alias Schedules.{Schedule, ScheduleCondensed, Trip} alias Stops.Stop alias Predictions.Prediction + import PredictedSchedule import Mock import Mox + import Test.Support.Factory.Prediction # set to the end of a month to uncover issues with sorting times as # structs, rather than as integers @@ -62,37 +65,37 @@ defmodule PredictedScheduleTest do ) @predictions [ - %Prediction{ + build(:prediction, %{ stop: %Stop{id: "first"}, trip: %Trip{id: "trip1"}, time: Timex.shift(@base_time, minutes: 12), route: @route - }, - %Prediction{ + }), + build(:prediction, %{ stop: %Stop{id: "second"}, trip: %Trip{id: "trip1"}, time: Timex.shift(@base_time, minutes: 22), route: @route - }, - %Prediction{ + }), + build(:prediction, %{ stop: %Stop{id: "third"}, trip: %Trip{id: "trip1"}, time: Timex.shift(@base_time, minutes: 32), route: @route - } + }) ] @non_matching_predictions [ - %Prediction{ + build(:prediction, %{ stop: %Stop{id: "stop1"}, time: Timex.shift(@base_time, minutes: 12), trip: %Trip{id: "trip6"} - }, - %Prediction{ + }), + build(:prediction, %{ stop: %Stop{id: "stop2"}, time: Timex.shift(@base_time, minutes: 32), trip: %Trip{id: "trip2"} - } + }) ] @trip_schedules [ @@ -117,24 +120,24 @@ defmodule PredictedScheduleTest do ] @trip_predictions [ - %Prediction{ + build(:prediction, %{ trip: %Trip{id: "Trip 1"}, stop: %Stop{id: "stop1"}, time: Timex.shift(@base_time, minutes: 2), route: @route - }, - %Prediction{ + }), + build(:prediction, %{ trip: %Trip{id: "Trip 2"}, stop: %Stop{id: "stop1"}, time: Timex.shift(@base_time, minutes: 12), route: @route - }, - %Prediction{ + }), + build(:prediction, %{ trip: %Trip{id: "Trip 3"}, stop: %Stop{id: "stop1"}, time: Timex.shift(@base_time, minutes: 22), route: @route - } + }) ] describe "get/2" do