Skip to content

Commit

Permalink
Configured predictions repo, and updated predictions controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kotva006 committed May 8, 2024
1 parent eacd9bb commit 9ca4253
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 52 deletions.
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

config :dotcom, :redis, Dotcom.Redis.Mock
config :dotcom, :redix, Dotcom.Redix.Mock
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
4 changes: 0 additions & 4 deletions lib/predictions/repo/behaviour.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ defmodule Predictions.Repo.Behaviour do
Behavior for an API client for fetching prediction data.
"""

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

@doc """
Return predictions for given params
"""
Expand Down
127 changes: 86 additions & 41 deletions test/dotcom_web/controllers/schedule/predictions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule DotcomWeb.ScheduleController.PredictionsTest do
use DotcomWeb.ConnCase, async: true

import DotcomWeb.ScheduleController.Predictions
import Mox
alias Predictions.Prediction

defmodule PredictionsTest do
Expand All @@ -26,8 +27,8 @@ defmodule DotcomWeb.ScheduleController.PredictionsTest do
end

describe "init/1" do
test "defaults to using Predictions.Repo.all" do
assert init([]) == [predictions_fn: &Predictions.Repo.all/1]
test "defaults to using Predictions.Repo.Mock.all" do
assert init() == [predictions_fn: &Predictions.Repo.Mock.all/1]
end
end

Expand All @@ -36,39 +37,58 @@ defmodule DotcomWeb.ScheduleController.PredictionsTest do
conn =
conn
|> assign(:date, ~D[2016-12-31])
|> assign(:origin, Faker.Pokemon.location())
|> call(@opts)

assert conn.assigns[:predictions] == []
assert conn.assigns[:vehicle_predictions] == []
end

test "when there is no origin, assigns no predictions", %{conn: conn} do
conn =
conn
|> assign(:origin, nil)
|> call(@opts)

assert conn.assigns[:predictions] == []
assert conn.assigns[:vehicle_predictions] == []
end

test "assigns predictions for a route, stop, and direction ID", %{conn: conn} do
expect(Predictions.Repo.Mock, :all, fn [route: "4", direction_id: "0"] ->
@empty
end)

conn =
conn
|> assign(:origin, %Stops.Stop{id: "place-sstat"})
|> assign(:destination, nil)
|> assign(:route, %{id: "4"})
|> assign(:direction_id, "0")
|> call(predictions_fn: fn [route: "4", direction_id: "0"] -> @empty end)
|> call(init())

assert conn.assigns[:predictions] == []
end

test "ignores predictions which have the origin as their destination", %{conn: conn} do
prediction = %Predictions.Prediction{
time: ~N[2017-01-01T00:00:00],
stop: %Stops.Stop{id: "origin"},
trip: 1234,
departing?: false
}
expect(Predictions.Repo.Mock, :all, fn _ ->
[
%Predictions.Prediction{
time: ~N[2017-01-01T00:00:00],
stop: %Stops.Stop{id: "origin"},
trip: 1234,
departing?: false
}
]
end)

conn =
conn
|> assign(:origin, %Stops.Stop{id: "origin"})
|> assign(:destination, nil)
|> assign(:route, %{id: "4"})
|> assign(:direction_id, "0")
|> call(predictions_fn: fn _ -> [prediction] end)
|> call(init())

assert conn.assigns.predictions == []
end
Expand All @@ -82,13 +102,17 @@ defmodule DotcomWeb.ScheduleController.PredictionsTest do
departing?: true
}

expect(Predictions.Repo.Mock, :all, fn _ ->
[prediction]
end)

conn =
conn
|> assign(:origin, %Stops.Stop{id: "origin"})
|> assign(:destination, nil)
|> assign(:route, %{id: "4"})
|> assign(:direction_id, "0")
|> call(predictions_fn: fn _ -> [prediction] end)
|> call(init())

assert conn.assigns.predictions == [prediction]
end
Expand All @@ -102,13 +126,17 @@ defmodule DotcomWeb.ScheduleController.PredictionsTest do
departing?: true
}

expect(Predictions.Repo.Mock, :all, fn _ ->
[prediction]
end)

conn =
conn
|> assign(:origin, %Stops.Stop{id: "origin"})
|> assign(:destination, nil)
|> assign(:route, %{id: "4"})
|> assign(:direction_id, "0")
|> call(predictions_fn: fn _ -> [prediction] end)
|> call(init())

assert conn.assigns.predictions == [prediction]
end
Expand All @@ -122,13 +150,17 @@ defmodule DotcomWeb.ScheduleController.PredictionsTest do
departing?: true
}

expect(Predictions.Repo.Mock, :all, fn _ ->
[prediction]
end)

conn =
conn
|> assign(:origin, %Stops.Stop{id: "origin"})
|> assign(:destination, nil)
|> assign(:route, %{id: "4"})
|> assign(:direction_id, "0")
|> call(predictions_fn: fn _ -> [prediction] end)
|> call(init())

assert conn.assigns.predictions == []
end
Expand All @@ -141,33 +173,45 @@ defmodule DotcomWeb.ScheduleController.PredictionsTest do
departing?: true
}

expect(Predictions.Repo.Mock, :all, fn _ ->
[prediction]
end)

conn =
conn
|> assign(:origin, %Stops.Stop{id: "origin"})
|> assign(:destination, nil)
|> assign(:route, %{id: "4"})
|> assign(:direction_id, "0")
|> call(predictions_fn: fn _ -> [prediction] end)
|> call(init())

assert conn.assigns.predictions == [prediction]
end

test "otherwise, assigns no predictions", %{conn: conn} do
expect(Predictions.Repo.Mock, :all, fn _ ->
[]
end)

conn =
conn
|> call(@opts)
|> call(init())

assert conn.assigns[:predictions] == []
end

test "destination predictions are assigned if destination is assigned", %{conn: conn} do
expect(Predictions.Repo.Mock, :all, fn [route: "66"] ->
@empty
end)

conn =
conn
|> assign(:origin, %Stops.Stop{id: "1148"})
|> assign(:destination, %Stops.Stop{id: "21148"})
|> assign(:route, %{id: "66"})
|> assign(:direction_id, "0")
|> call(predictions_fn: fn [route: "66"] -> @empty end)
|> call(init())

assert conn.assigns[:predictions] == []
end
Expand All @@ -188,26 +232,24 @@ defmodule DotcomWeb.ScheduleController.PredictionsTest do
}
}

Predictions.Repo.Mock
|> expect(:all, fn [route: "66"] -> [] end)
|> expect(:all, fn [trip: "1,2"] ->
# we transform the data into this form so that we only need to make one repo call
[
%Prediction{stop: %Stops.Stop{id: "place-sstat"}},
%Prediction{stop: %Stops.Stop{id: "place-north"}}
]
end)

conn =
conn
|> assign(:origin, %Stops.Stop{id: "1148"})
|> assign(:destination, %Stops.Stop{id: "21148"})
|> assign(:route, %{id: "66"})
|> assign(:direction_id, "0")
|> assign(:vehicle_locations, vehicle_locations)
|> call(
predictions_fn: fn
[route: "66"] ->
[]

# we transform the data into this form so that we only need to make one repo call
[trip: "1,2"] ->
[
%Prediction{stop: %Stops.Stop{id: "place-sstat"}},
%Prediction{stop: %Stops.Stop{id: "place-north"}}
]
end
)
|> call(init())

assert conn.assigns.vehicle_predictions == [
%Prediction{stop: %Stops.Stop{id: "place-sstat"}},
Expand All @@ -229,32 +271,35 @@ defmodule DotcomWeb.ScheduleController.PredictionsTest do
}
}

Predictions.Repo.Mock
|> expect(:all, fn [route: "66"] -> [] end)
|> expect(:all, fn [trip: "1,2"] ->
# we transform the data into this form so that we only need to make one repo call
[
%Prediction{stop: %Stops.Stop{id: "place-sstat"}}
]
end)

conn =
conn
|> assign(:origin, %Stops.Stop{id: "1148"})
|> assign(:destination, %Stops.Stop{id: "21148"})
|> assign(:route, %{id: "66"})
|> assign(:direction_id, "0")
|> assign(:vehicle_locations, vehicle_locations)
|> call(
predictions_fn: fn
[route: "66"] ->
[]

# we transform the data into this form so that we only need to make one repo call
[trip: "1,2"] ->
[%Prediction{stop: %Stops.Stop{id: "place-sstat"}}]
end
)
|> call(init())

assert conn.assigns.vehicle_predictions == [
%Prediction{stop: %Stops.Stop{id: "place-sstat"}}
]
end

test "assigns empty lists if the predictions return an error", %{conn: conn} do
predictions_fn = fn _ -> {:error, :no_predictions} end
conn = call(conn, init(predictions_fn: predictions_fn))
expect(Predictions.Repo.Mock, :all, fn [route: "66"] ->
{:error, :no_predictions}
end)

conn = call(conn, init())

assert conn.assigns.predictions == []
assert conn.assigns.vehicle_predictions == []
Expand Down

0 comments on commit 9ca4253

Please sign in to comment.