From fa0d08a9fef7f1025f54faf1286f377ae64cd803 Mon Sep 17 00:00:00 2001 From: cmaddox5 Date: Tue, 17 Dec 2024 11:36:21 -0500 Subject: [PATCH] Restart state app before each test. --- .../test/api_web/controllers/alert_controller_test.exs | 3 --- .../test/api_web/controllers/health_controller_test.exs | 2 -- .../prediction_controller/sort_by_arrival_time_test.exs | 7 +------ .../api_web/controllers/prediction_controller_test.exs | 3 --- .../test/api_web/controllers/stop_controller_test.exs | 1 - .../test/api_web/controllers/trip_controller_test.exs | 2 -- apps/api_web/test/api_web/event_stream_test.exs | 1 - .../test/api_web/plugs/experimental_features_test.exs | 2 +- apps/api_web/test/api_web/views/api_view_helpers_test.exs | 3 +-- apps/api_web/test/api_web/views/facility_view_test.exs | 5 ----- apps/api_web/test/support/conn_case.ex | 5 +++++ apps/state/test/state/trip/added_test.exs | 6 ++---- 12 files changed, 10 insertions(+), 30 deletions(-) diff --git a/apps/api_web/test/api_web/controllers/alert_controller_test.exs b/apps/api_web/test/api_web/controllers/alert_controller_test.exs index cce22de56..0d1782a68 100644 --- a/apps/api_web/test/api_web/controllers/alert_controller_test.exs +++ b/apps/api_web/test/api_web/controllers/alert_controller_test.exs @@ -50,9 +50,6 @@ defmodule ApiWeb.AlertControllerTest do setup tags do State.Stop.new_state([%Model.Stop{}]) - State.Trip.new_state([]) - State.Route.new_state([]) - State.Schedule.new_state([]) State.RoutesPatternsAtStop.update!() Facility.new_state([ diff --git a/apps/api_web/test/api_web/controllers/health_controller_test.exs b/apps/api_web/test/api_web/controllers/health_controller_test.exs index fa86e5ffe..763be0922 100644 --- a/apps/api_web/test/api_web/controllers/health_controller_test.exs +++ b/apps/api_web/test/api_web/controllers/health_controller_test.exs @@ -6,8 +6,6 @@ defmodule ApiWeb.HealthControllerTest do end test "defaults to 503", %{conn: conn} do - State.Alert.new_state([]) - State.Schedule.new_state([]) State.StopsOnRoute.update!() conn = get(conn, health_path(conn, :index)) assert json_response(conn, 503) diff --git a/apps/api_web/test/api_web/controllers/prediction_controller/sort_by_arrival_time_test.exs b/apps/api_web/test/api_web/controllers/prediction_controller/sort_by_arrival_time_test.exs index 9c4b633c4..1b169a7d7 100644 --- a/apps/api_web/test/api_web/controllers/prediction_controller/sort_by_arrival_time_test.exs +++ b/apps/api_web/test/api_web/controllers/prediction_controller/sort_by_arrival_time_test.exs @@ -61,16 +61,11 @@ defmodule ApiWeb.PredictionController.SortByArrivalTimeTest do } ] - setup_all do + setup %{conn: conn} do # stop is needed since we look up parent stops State.Stop.new_state([@stop, @parent_stop]) - State.Trip.new_state([]) State.Route.new_state([@route1, @route2, @route3]) State.Prediction.new_state(Enum.shuffle(@cr_predictions)) - :ok - end - - setup %{conn: conn} do {:ok, conn: put_req_header(conn, "accept", "application/json")} end diff --git a/apps/api_web/test/api_web/controllers/prediction_controller_test.exs b/apps/api_web/test/api_web/controllers/prediction_controller_test.exs index 71f850f6e..02ca92188 100644 --- a/apps/api_web/test/api_web/controllers/prediction_controller_test.exs +++ b/apps/api_web/test/api_web/controllers/prediction_controller_test.exs @@ -44,7 +44,6 @@ defmodule ApiWeb.PredictionControllerTest do @cr_prediction hd(@cr_predictions) setup %{conn: conn} do - State.Trip.new_state([]) # stop is needed since we look up parent stops State.Stop.new_state([@stop, @parent_stop, %Stop{id: "2"}]) State.Prediction.new_state(@cr_predictions) @@ -584,7 +583,6 @@ defmodule ApiWeb.PredictionControllerTest do test "does not return alerts when no alerts exist", %{conn: conn} do different_route_prediction = %{@cr_prediction | route_id: "red", trip_id: "trip2"} - State.Alert.new_state([]) State.Prediction.new_state([different_route_prediction, @cr_prediction]) include_nothing_conn = @@ -626,7 +624,6 @@ defmodule ApiWeb.PredictionControllerTest do %{conn: conn} do # run it 100 times since it's a race condition between State.Prediction and State.Trip.Added for _ <- 0..100 do - State.Prediction.new_state([]) # wait for State,Trip.Added to clear State.Trip.Added.last_updated() # need a stop ID to generate an Added trip diff --git a/apps/api_web/test/api_web/controllers/stop_controller_test.exs b/apps/api_web/test/api_web/controllers/stop_controller_test.exs index 2d7087032..fdefc8d24 100644 --- a/apps/api_web/test/api_web/controllers/stop_controller_test.exs +++ b/apps/api_web/test/api_web/controllers/stop_controller_test.exs @@ -3,7 +3,6 @@ defmodule ApiWeb.StopControllerTest do alias Model.{Facility, Schedule, Stop, Trip} setup %{conn: conn} do - State.Stop.new_state([]) {:ok, conn: put_req_header(conn, "accept", "application/json")} end diff --git a/apps/api_web/test/api_web/controllers/trip_controller_test.exs b/apps/api_web/test/api_web/controllers/trip_controller_test.exs index 599839c78..ee11d7b42 100644 --- a/apps/api_web/test/api_web/controllers/trip_controller_test.exs +++ b/apps/api_web/test/api_web/controllers/trip_controller_test.exs @@ -4,8 +4,6 @@ defmodule ApiWeb.TripControllerTest do alias Model.Trip setup %{conn: conn} do - State.Trip.new_state([]) - {:ok, conn: put_req_header(conn, "accept", "application/json")} end diff --git a/apps/api_web/test/api_web/event_stream_test.exs b/apps/api_web/test/api_web/event_stream_test.exs index 0ac2661d6..93a902c07 100644 --- a/apps/api_web/test/api_web/event_stream_test.exs +++ b/apps/api_web/test/api_web/event_stream_test.exs @@ -13,7 +13,6 @@ defmodule ApiWeb.EventStreamTest do setup %{conn: conn} do CheckForShutdown.reset() - State.Prediction.new_state([]) conn = conn diff --git a/apps/api_web/test/api_web/plugs/experimental_features_test.exs b/apps/api_web/test/api_web/plugs/experimental_features_test.exs index ef11e8d0c..0da722248 100644 --- a/apps/api_web/test/api_web/plugs/experimental_features_test.exs +++ b/apps/api_web/test/api_web/plugs/experimental_features_test.exs @@ -1,6 +1,6 @@ defmodule ApiWeb.Plugs.ExperimentalFeaturesTest do import Phoenix.ConnTest - use ApiWeb.ConnCase, async: true + use ApiWeb.ConnCase test "init" do assert ApiWeb.Plugs.ExperimentalFeatures.init([]) == [] diff --git a/apps/api_web/test/api_web/views/api_view_helpers_test.exs b/apps/api_web/test/api_web/views/api_view_helpers_test.exs index b6c080163..6c4ad8f88 100644 --- a/apps/api_web/test/api_web/views/api_view_helpers_test.exs +++ b/apps/api_web/test/api_web/views/api_view_helpers_test.exs @@ -1,5 +1,5 @@ defmodule ApiWeb.ApiViewHelpersTest do - use ExUnit.Case + use ApiWeb.ConnCase alias ApiWeb.ApiViewHelpers import ApiViewHelpers @@ -35,7 +35,6 @@ defmodule ApiWeb.ApiViewHelpersTest do end test "otherwise uses trip_id to lookup trip" do - State.Trip.new_state([]) conn = %Plug.Conn{assigns: %{split_include: MapSet.new(["trip"])}} assert trip(%{trip_id: @trip_id}, conn) == nil diff --git a/apps/api_web/test/api_web/views/facility_view_test.exs b/apps/api_web/test/api_web/views/facility_view_test.exs index 96e1e19d6..eb0fa72ef 100644 --- a/apps/api_web/test/api_web/views/facility_view_test.exs +++ b/apps/api_web/test/api_web/views/facility_view_test.exs @@ -14,11 +14,6 @@ defmodule ApiWeb.FacilityViewTest do longitude: -71.794593 } - setup do - State.Facility.Property.new_state([]) - :ok - end - test "can do a basic rendering", %{conn: conn} do rendered = render("index.json-api", data: @facility, conn: conn)["data"] assert rendered["type"] == "facility" diff --git a/apps/api_web/test/support/conn_case.ex b/apps/api_web/test/support/conn_case.ex index e7aee6f98..80f54fbb1 100644 --- a/apps/api_web/test/support/conn_case.ex +++ b/apps/api_web/test/support/conn_case.ex @@ -32,6 +32,11 @@ defmodule ApiWeb.ConnCase do setup _tags do conn = conn_with_api_key(Phoenix.ConnTest.build_conn()) + + # Ensure each test is using a fresh instance of State modules + Application.stop(:state) + Application.start(:state) + {:ok, conn: conn} end diff --git a/apps/state/test/state/trip/added_test.exs b/apps/state/test/state/trip/added_test.exs index 67adcd5fa..7cc822ae1 100644 --- a/apps/state/test/state/trip/added_test.exs +++ b/apps/state/test/state/trip/added_test.exs @@ -179,8 +179,7 @@ defmodule State.Trip.AddedTest do {:ok, %{shape: shape, prediction: prediction}} end - test "if there's a matching shape for the route/direction, uses the last stop from that shape", - %{prediction: prediction} do + test "if there's a matching shape for the route/direction, uses the last stop from that shape" do predictions = [ %{@prediction | stop_sequence: 3, stop_id: "child"}, %{@prediction | stop_sequence: 2, stop_id: "other"} @@ -190,8 +189,7 @@ defmodule State.Trip.AddedTest do assert [%{headsign: "Last Stop on Shape"}] = by_id(@trip_id) end - test "if there's a matching shape for the route/direction and it's a subway route, uses the last predicted stop", - %{prediction: prediction} do + test "if there's a matching shape for the route/direction and it's a subway route, uses the last predicted stop" do State.Route.new_state([%Model.Route{id: @route_id, type: 0}]) predictions = [