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

Conversation

kotva006
Copy link
Contributor

@kotva006 kotva006 commented May 7, 2024

No ticket. Adding some mox for the predictions, and updating tests that use the repo functions

@kotva006 kotva006 requested a review from a team as a code owner May 7, 2024 18:09
@kotva006 kotva006 requested review from anthonyshull and removed request for anthonyshull May 7, 2024 18:09
@kotva006 kotva006 marked this pull request as draft May 7, 2024 18:10
config/test.exs Outdated
Comment on lines 11 to 12
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.

@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).

@kotva006 kotva006 marked this pull request as ready for review May 9, 2024 20:09
Copy link
Contributor

@anthonyshull anthonyshull left a comment

Choose a reason for hiding this comment

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

Looks a lot cleaner, but there are still a lot of magic strings.

Comment on lines 61 to 63
time: ~N[2017-01-01T00:00:00],
stop: %Stops.Stop{id: "origin"},
trip: 1234,
Copy link
Contributor

Choose a reason for hiding this comment

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

Should use Faker for all of these unless they mean something.

Copy link
Contributor

Choose a reason for hiding this comment

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

Would also be super simple to turn this into a factory.

@@ -13,3 +13,4 @@ Mox.defmock(CMS.Api.Mock, for: CMS.Api.Behaviour)
Mox.defmock(MBTA.Api.Mock, for: MBTA.Api.Behaviour)
Mox.defmock(RoutePatterns.Repo.Mock, for: RoutePatterns.Repo.Behaviour)
Mox.defmock(OpenTripPlannerClient.Mock, for: OpenTripPlannerClient.Behaviour)
Mox.defmock(Predictions.Repo.Mock, for: Predictions.Repo.Behaviour)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should probably separate out the Repo.Mocks and make sure they're alphabetized. I would label them too.

@@ -578,7 +589,8 @@ 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
init = init(trip_fn: &trip_fn/2, vehicle_fn: &vehicle_fn/1, prediction_fn: &prediction_fn/1)
expect(Predictions.Repo.Mock, :all, &prediction_fn/1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we still referencing prediction_fn? Should be able to remove that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

prediction_fn/1 is a helper defined in this file. It seemed fine to keep it used here instead of redefining it for every test

Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't need it. It looks like it is being used as a kind of factory.


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())
Copy link
Contributor

Choose a reason for hiding this comment

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

init() will just return an empty list. And call/1 will default to an empty list. In fact call/2 just ignores the options. So, I'm pretty sure you can just remove it.

Comment on lines 704 to 705
schedule_relationship: nil,
status: nil,
Copy link
Contributor

Choose a reason for hiding this comment

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

See the below comment for the factory.

alias Predictions.Prediction

def prediction_factory do
%Prediction{}
Copy link
Contributor

Choose a reason for hiding this comment

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

In the above tests, you are building a prediction and passing in every single value. I'm fairly certain that not setting anything here will set them all to struct default which is probably nil. So, passing in values set to nil won't do anything. You should set reasonable defaults here and then you only have to override ones that are necessary for the tests. Makes the tests shorter and clearer.

@kotva006 kotva006 merged commit 5885848 into main May 15, 2024
24 checks passed
@kotva006 kotva006 deleted the rk/predictions-mox branch May 15, 2024 14:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants