Skip to content

Commit

Permalink
refactor(RoutePatterns.Repo): use the mock for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thecristen committed May 2, 2024
1 parent 1ac1088 commit 1a82ad5
Show file tree
Hide file tree
Showing 19 changed files with 242 additions and 321 deletions.
1 change: 1 addition & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ config :elixir, ansi_enabled: true
config :dotcom, :httpoison, HTTPoison

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

config :dotcom, :redis, Dotcom.Cache.Multilevel.Redis
config :dotcom, :redix, Redix
Expand Down
4 changes: 0 additions & 4 deletions config/dotcom/dotcom.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ config :dotcom, route_populate_caches?: config_env() == :prod
routes_repo = if config_env() == :test, do: Routes.MockRepoApi, else: Routes.Repo
config :dotcom, :routes_repo_api, routes_repo

repo_module = if config_env() == :test, do: RoutePatterns.Repo.Mock, else: RoutePatterns.Repo

config :dotcom, :route_patterns_repo_api, repo_module

predictions_broadcast_interval_ms = if config_env() == :test, do: 50, else: 10_000

config :dotcom, predictions_broadcast_interval_ms: predictions_broadcast_interval_ms
1 change: 1 addition & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ config :dotcom, :cache, Dotcom.Cache.TestCache
config :dotcom, :httpoison, HTTPoison.Mock

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

config :dotcom, :redis, Dotcom.Redis.Mock
config :dotcom, :redix, Dotcom.Redix.Mock
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 @@ -19,6 +19,8 @@ defmodule DotcomWeb.ScheduleController.FinderApi do

require Logger

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

@type react_keys :: :date | :direction | :is_current
@type react_strings :: [{react_keys, String.t()}]
@type converted_values :: {Date.t(), integer, boolean}
Expand Down Expand Up @@ -508,7 +510,7 @@ defmodule DotcomWeb.ScheduleController.FinderApi do
with %Schedules.Trip{route_pattern_id: route_pattern_id} when not is_nil(route_pattern_id) <-
Schedules.Repo.trip(trip_id),
%RoutePatterns.RoutePattern{route_id: route_id} <-
RoutePatterns.Repo.get(route_pattern_id) do
@route_patterns_repo.get(route_pattern_id) do
route_id
else
_ ->
Expand Down
9 changes: 5 additions & 4 deletions lib/dotcom_web/controllers/schedule/line/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ defmodule DotcomWeb.ScheduleController.Line.Helpers do
Helpers for the line page
"""

alias RoutePatterns.Repo, as: RoutePatternsRepo
alias RoutePatterns.RoutePattern
alias Routes.Repo, as: RoutesRepo
alias Routes.{Route, Shape}
alias Stops.Repo, as: StopsRepo
alias Stops.{RouteStop, RouteStops, Stop}

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

@type query_param :: String.t() | nil
@type direction_id :: 0 | 1
@typep stops_by_route :: %{String.t() => [Stop.t()]}
Expand Down Expand Up @@ -88,7 +89,7 @@ defmodule DotcomWeb.ScheduleController.Line.Helpers do

def get_map_route_patterns(route_id, type) do
route_id
|> RoutePatternsRepo.by_route_id(
|> @route_patterns_repo.by_route_id(
include: "representative_trip.shape,representative_trip.stops"
)
|> filter_map_route_patterns(type)
Expand Down Expand Up @@ -267,12 +268,12 @@ defmodule DotcomWeb.ScheduleController.Line.Helpers do
base_opts
end

RoutePatternsRepo.by_route_id(route_id, opts)
@route_patterns_repo.by_route_id(route_id, opts)
|> Enum.filter(&(&1.route_id == route_id))
end

defp get_line_route_patterns(_route, _direction_id, route_pattern_id) do
case RoutePatternsRepo.get(route_pattern_id,
case @route_patterns_repo.get(route_pattern_id,
include: "representative_trip.stops"
) do
%RoutePattern{} = route_pattern ->
Expand Down
4 changes: 3 additions & 1 deletion lib/dotcom_web/controllers/schedule/timetable_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule DotcomWeb.ScheduleController.TimetableController do

require Logger

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

plug(DotcomWeb.Plugs.Route)
plug(DotcomWeb.Plugs.DateInRating)
plug(:tab_name)
Expand Down Expand Up @@ -61,7 +63,7 @@ defmodule DotcomWeb.ScheduleController.TimetableController do
} = build_timetable(conn.assigns.all_stops, timetable_schedules, direction_id)

canonical_rps =
RoutePatterns.Repo.by_route_id(route.id,
@route_patterns_repo.by_route_id(route.id,
direction_id: direction_id,
canonical: true,
include: "representative_trip.stops"
Expand Down
4 changes: 3 additions & 1 deletion lib/dotcom_web/controllers/stop_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ defmodule DotcomWeb.StopController do
alias Stops.{Repo, Stop}
alias Util.AndOr

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

plug(:alerts)
plug(DotcomWeb.Plugs.AlertsByTimeframe)

Expand Down Expand Up @@ -90,7 +92,7 @@ defmodule DotcomWeb.StopController do
@spec route_patterns_by_route_and_headsign(Stop.id_t()) :: by_route_and_headsign()
defp route_patterns_by_route_and_headsign(stop_id) do
stop_id
|> RoutePatterns.Repo.by_stop_id()
|> @route_patterns_repo.by_stop_id()
|> Stream.reject(&ends_at?(&1, stop_id))
|> Stream.reject(&exclusively_drop_offs?(&1, stop_id))
|> Enum.group_by(& &1.route_id)
Expand Down
4 changes: 3 additions & 1 deletion lib/predictions/stream_topic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ defmodule Predictions.StreamTopic do

defstruct [:topic, :fetch_keys, :streams]

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

@type filter_params :: String.t()
@type clear_keys :: Store.fetch_keys()
@type t :: %__MODULE__{
Expand Down Expand Up @@ -56,7 +58,7 @@ defmodule Predictions.StreamTopic do

@spec streams_from_fetch_keys(Store.fetch_keys()) :: [{clear_keys, filter_params}]
defp streams_from_fetch_keys(stop: stop_id) do
RoutePatterns.Repo.by_stop_id(stop_id)
@route_patterns_repo.by_stop_id(stop_id)
|> Enum.map(&{to_keys(&1), to_filter_name(&1)})
end

Expand Down
7 changes: 2 additions & 5 deletions lib/route_patterns/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ defmodule RoutePatterns.Repo do
@cache Application.compile_env!(:dotcom, :cache)
@ttl :timer.hours(1)

@doc """
Returns a single route pattern by ID
"""
@callback get(RoutePattern.id_t()) :: RoutePattern.t() | nil
@callback get(RoutePattern.id_t(), keyword()) :: RoutePattern.t() | nil
@impl RoutePatterns.Repo.Behaviour
def get(id, opts \\ []) when is_binary(id) do
case get_id(id, opts) do
{:ok, route_pattern} -> route_pattern
Expand Down Expand Up @@ -51,6 +47,7 @@ defmodule RoutePatterns.Repo do
|> Enum.sort(&reorder_mrts(&1, &2, route_id))
end

@impl RoutePatterns.Repo.Behaviour
def by_stop_id(stop_id) do
[stop: stop_id]
|> Keyword.put(:include, "representative_trip.shape,representative_trip.stops")
Expand Down
12 changes: 12 additions & 0 deletions lib/route_patterns/repo/behaviour.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@ defmodule RoutePatterns.Repo.Behaviour do

alias RoutePatterns.RoutePattern
alias Routes.Route
alias Stops.Stop

@doc """
Return a route pattern by ID
"""
@callback get(RoutePattern.id_t()) :: RoutePattern.t() | nil
@callback get(RoutePattern.id_t(), keyword()) :: RoutePattern.t() | nil

@doc """
Return all route patterns for a route ID
"""
@callback by_route_id(Route.id_t()) :: [RoutePattern.t()]
@callback by_route_id(Route.id_t(), keyword()) :: [RoutePattern.t()]

@doc """
Return all route patterns for a stop ID
"""
@callback by_stop_id(Stop.id_t()) :: [RoutePattern.t()]
end
215 changes: 0 additions & 215 deletions lib/route_patterns/repo/mock.ex

This file was deleted.

Loading

0 comments on commit 1a82ad5

Please sign in to comment.