Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(Predictions): Added the mox for predictions #2036

Merged
merged 21 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ config :dotcom, :cms_api_module, CMS.Api
config :dotcom, :httpoison, HTTPoison

config :dotcom, :mbta_api_module, MBTA.Api
config :dotcom, :repo_modules, route_patterns: RoutePatterns.Repo
config :dotcom, :repo_modules, route_patterns: RoutePatterns.Repo, predictions: Predictions.Repo

config :dotcom, :redis, Dotcom.Cache.Multilevel.Redis
config :dotcom, :redix, Redix
Expand Down
5 changes: 4 additions & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ config :dotcom, :httpoison, HTTPoison.Mock

config :dotcom, :cms_api_module, CMS.Api.Static
config :dotcom, :mbta_api_module, MBTA.Api.Mock
config :dotcom, :repo_modules, route_patterns: RoutePatterns.Repo.Mock

config :dotcom, :repo_modules,
route_patterns: RoutePatterns.Repo.Mock,
predictions: Predictions.Repo.Mock
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should alphabetize this list and the one above because they are going to be quite long.


config :dotcom, :redis, Dotcom.Redis.Mock
config :dotcom, :redix, Dotcom.Redix.Mock
Expand Down
5 changes: 3 additions & 2 deletions lib/dotcom/realtime_schedule.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ defmodule Dotcom.RealtimeSchedule do
alias Dotcom.JsonHelpers
alias Dotcom.TransitNearMe
alias Predictions.Prediction
alias Predictions.Repo, as: PredictionsRepo
alias RoutePatterns.RoutePattern
alias Routes.Repo, as: RoutesRepo
alias Routes.Route
Expand All @@ -25,10 +24,12 @@ defmodule Dotcom.RealtimeSchedule do

@predicted_schedules_per_stop 2

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

@default_opts [
stops_fn: &StopsRepo.get/1,
routes_fn: &RoutesRepo.by_stop_with_route_pattern/1,
predictions_fn: &PredictionsRepo.all_no_cache/1,
predictions_fn: Function.capture(@predictions_repo, :all_no_cache, 1),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should just remove the dependency injection. It's pretty simple to just replace all instances of predictions_fn.(params) with a call to @predictions_repo.all_no_cache(params).

schedules_fn: &SchedulesRepo.by_route_ids/2,
alerts_fn: &Alerts.Repo.by_route_ids/2
]
Expand Down
6 changes: 5 additions & 1 deletion lib/dotcom/transit_near_me.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ defmodule Dotcom.TransitNearMe do
"place-hsmnl"
]

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

@spec build(Address.t(), Keyword.t()) :: stops_with_distances
def build(%Address{} = location, opts) do
opts = Keyword.merge(@default_opts, opts)
Expand Down Expand Up @@ -382,7 +384,9 @@ defmodule Dotcom.TransitNearMe do
PredictedSchedule.t()
]
defp get_predicted_schedules(schedules, params, opts) do
predictions_fn = Keyword.get(opts, :predictions_fn, &Predictions.Repo.all/1)
predictions_fn =
Keyword.get(opts, :predictions_fn, Function.capture(@predictions_repo, :all, 1))

now = Keyword.fetch!(opts, :now)

predictions = predictions_fn.(params)
Expand Down
4 changes: 3 additions & 1 deletion lib/dotcom_web/channels/vehicle_map_marker_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ defmodule DotcomWeb.VehicleMapMarkerChannel do
alias Leaflet.MapData.Marker
alias Vehicles.Vehicle

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

intercept(["reset", "add", "update", "remove"])

@impl Phoenix.Channel
Expand Down Expand Up @@ -46,7 +48,7 @@ defmodule DotcomWeb.VehicleMapMarkerChannel do
trip = Schedules.Repo.trip(vehicle.trip_id)

prediction =
Predictions.Repo.all(
@predictions_repo.all(
route: vehicle.route_id,
direction_id: vehicle.direction_id
)
Expand Down
4 changes: 3 additions & 1 deletion lib/dotcom_web/controllers/schedule/finder_api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ defmodule DotcomWeb.ScheduleController.FinderApi do
require Logger

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

@type react_keys :: :date | :direction | :is_current
@type react_strings :: [{react_keys, String.t()}]
Expand Down Expand Up @@ -217,7 +218,8 @@ defmodule DotcomWeb.ScheduleController.FinderApi do
direction_id: direction_id
]

predictions_fn = Map.get(conn.assigns, :predictions_fn, &Predictions.Repo.all/1)
predictions_fn =
Map.get(conn.assigns, :predictions_fn, Function.capture(@predictions_repo, :all, 1))

predictions =
if current_service?,
Expand Down
3 changes: 2 additions & 1 deletion lib/dotcom_web/controllers/schedule/green.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ defmodule DotcomWeb.ScheduleController.Green do
plug(:channels)

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

def show(%Plug.Conn{query_params: %{"tab" => "alerts"}} = conn, _params),
do: alerts(conn, [])
Expand Down Expand Up @@ -75,7 +76,7 @@ defmodule DotcomWeb.ScheduleController.Green do
def predictions(conn, opts) do
{predictions, vehicle_predictions} =
if DotcomWeb.ScheduleController.Predictions.should_fetch_predictions?(conn) do
predictions_fn = opts[:predictions_fn] || (&Predictions.Repo.all/1)
predictions_fn = opts[:predictions_fn] || Function.capture(@predictions_repo, :all, 1)

predictions_stream =
conn
Expand Down
8 changes: 3 additions & 5 deletions lib/dotcom_web/controllers/schedule/predictions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ defmodule DotcomWeb.ScheduleController.Predictions do
alias Predictions.Prediction
alias Util.AsyncAssign

@default_opts [
predictions_fn: &Predictions.Repo.all/1
]
@predictions_repo Application.compile_env!(:dotcom, :repo_modules)[:predictions]

@typep predictions_fn :: (Keyword.t() -> [Prediction.t()] | {:error, any})

@impl true
def init(opts) do
Keyword.merge(@default_opts, opts)
def init(opts \\ []) do
Keyword.merge([predictions_fn: Function.capture(@predictions_repo, :all, 1)], opts)
end

@impl true
Expand Down
7 changes: 6 additions & 1 deletion lib/predicted_schedule.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ defmodule PredictedSchedule do
prediction: Prediction.t() | nil
}

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

def get(route_id, stop_id, opts \\ []) do
schedules_fn = Keyword.get(opts, :schedules_fn, &Schedules.Repo.by_route_ids/2)
predictions_fn = Keyword.get(opts, :predictions_fn, &Predictions.Repo.all/1)

predictions_fn =
Keyword.get(opts, :predictions_fn, Function.capture(@predictions_repo, :all, 1))

now = Keyword.get(opts, :now, Util.now())
direction_id = Keyword.get(opts, :direction_id)
sort_fn = Keyword.get(opts, :sort_fn, &sort_predicted_schedules/1)
Expand Down
15 changes: 15 additions & 0 deletions lib/predictions/repo/behaviour.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule Predictions.Repo.Behaviour do
@moduledoc """
Behavior for an API client for fetching prediction data.
"""

@doc """
Return predictions for given params
"""
@callback all(Keyword.t()) :: [Predictions.Prediction.t()] | []

@doc """
Return predictions for give prarams ignoring the cache
"""
@callback all_no_cache(Keyword.t()) :: [Predictions.Prediction.t()] | []
end
8 changes: 6 additions & 2 deletions test/dotcom/realtime_schedule_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule Dotcom.RealtimeScheduleTest do
use ExUnit.Case

import Mox

alias Alerts.Alert
alias Alerts.InformedEntity, as: IE
alias Alerts.InformedEntitySet, as: IESet
Expand Down Expand Up @@ -175,10 +177,11 @@ defmodule Dotcom.RealtimeScheduleTest do
end

test "stop_data/3 returns stop" do
expect(Predictions.Repo.Mock, :all_no_cache, 3, fn _ -> @predictions end)

opts = [
stops_fn: fn _ -> @stop end,
routes_fn: fn _ -> @route_with_patterns end,
predictions_fn: fn _ -> @predictions end,
schedules_fn: fn _, _ -> @schedules end,
alerts_fn: fn _, _ -> @alerts end
]
Expand Down Expand Up @@ -301,10 +304,11 @@ defmodule Dotcom.RealtimeScheduleTest do
end

test "stop_data/3 returns nil" do
expect(Predictions.Repo.Mock, :all_no_cache, fn _ -> [] end)

opts = [
stops_fn: fn _ -> nil end,
routes_fn: fn _ -> [] end,
predictions_fn: fn _ -> [] end,
schedules_fn: fn _, _ -> [] end,
alerts_fn: fn _, _ -> [] end
]
Expand Down
13 changes: 7 additions & 6 deletions test/dotcom/transit_near_me_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule Dotcom.TransitNearMeTest do
use ExUnit.Case

import Mox

alias LocationService.Address
alias Predictions.Prediction
alias Routes.Route
Expand Down Expand Up @@ -725,7 +727,9 @@ defmodule Dotcom.TransitNearMeTest do
}

test "returns time data for the next 2 predictions" do
predictions_fn = fn _ -> [@prediction1, @prediction2, @prediction3] end
expect(Predictions.Repo.Mock, :all, fn _ ->
[@prediction1, @prediction2, @prediction3]
end)

schedules_fn = fn _, _ ->
[@schedule1, @schedule2, @schedule3]
Expand Down Expand Up @@ -787,15 +791,14 @@ defmodule Dotcom.TransitNearMeTest do
actual =
TransitNearMe.time_data_for_route_by_stop(@route.id, 1,
schedules_fn: schedules_fn,
predictions_fn: predictions_fn,
now: @now
)

assert actual == expected
end

test "returns no data when schedules is empty" do
predictions_fn = fn _ -> [@prediction1] end
expect(Predictions.Repo.Mock, :all, fn _ -> [@prediction1] end)

schedules_fn = fn _, _ ->
[]
Expand All @@ -804,15 +807,14 @@ defmodule Dotcom.TransitNearMeTest do
actual =
TransitNearMe.time_data_for_route_by_stop(@route.id, 1,
schedules_fn: schedules_fn,
predictions_fn: predictions_fn,
now: @now
)

assert %{"95" => [%{}]} = actual
end

test "return neither schedules nor predictions if date is outside rating" do
predictions_fn = fn _ -> [@prediction1] end
expect(Predictions.Repo.Mock, :all, fn _ -> [@prediction1] end)

schedules_fn = fn _, _ ->
{:error,
Expand All @@ -833,7 +835,6 @@ defmodule Dotcom.TransitNearMeTest do
actual =
TransitNearMe.time_data_for_route_by_stop(@route.id, 1,
schedules_fn: schedules_fn,
predictions_fn: predictions_fn,
now: @now
)

Expand Down