Skip to content

Commit

Permalink
Added more factory usage for predictions and some random data
Browse files Browse the repository at this point in the history
  • Loading branch information
kotva006 committed May 13, 2024
1 parent dfe967f commit b3be441
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 120 deletions.
12 changes: 6 additions & 6 deletions lib/dotcom_web/controllers/schedule/trip_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
87 changes: 46 additions & 41 deletions test/dotcom/transit_near_me_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule Dotcom.TransitNearMeTest do
use ExUnit.Case

import Mox
import Test.Support.Factory.Prediction

alias LocationService.Address
alias Predictions.Prediction
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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 _ ->
Expand All @@ -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
Expand All @@ -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,
Expand Down
98 changes: 41 additions & 57 deletions test/dotcom_web/controllers/schedule/trip_info_test.exs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 =
Expand All @@ -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

Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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 =
%{
Expand All @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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"}

Expand Down
Loading

0 comments on commit b3be441

Please sign in to comment.