diff --git a/app/api/config.py b/app/api/config.py index 8978086..67d6629 100644 --- a/app/api/config.py +++ b/app/api/config.py @@ -22,6 +22,7 @@ class Settings(BaseSettings): client_id: str | None = Field(alias="NB_QUERY_CLIENT_ID", default=None) @computed_field + @property def query_url(self) -> str: """Construct the URL of the graph store to be queried.""" return f"http://{self.graph_address}:{self.graph_port}/{self.graph_db}" diff --git a/pytest.ini b/pytest.ini index 3dab34c..8994749 100644 --- a/pytest.ini +++ b/pytest.ini @@ -3,7 +3,11 @@ markers = integration: mark integration tests that need the test graph to run ; Default to not running tests with the integration marker addopts = -m "not integration" -; Note: The following environment variables are set before any tests +; NOTE: The following environment variables are set to non-default values before any tests are run, +; allowing us to actually test that user-provided values are parsed accurately from the environment +; while avoiding issues related to import order in tests. +; In individual tests, these values may then be overridden as needed to test downstream logic +; by monkeypatching attributes of the global settings object env = NB_API_ALLOWED_ORIGINS=* NB_GRAPH_USERNAME=DBUSER diff --git a/tests/conftest.py b/tests/conftest.py index af8310f..46bd14e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -49,6 +49,19 @@ def mock_auth_header() -> dict: return {"Authorization": "Bearer foo"} +@pytest.fixture() +def set_graph_url_vars_for_integration_tests(monkeypatch): + """ + Set the graph URL to the default value for integration tests. + + NOTE: These should correspond to the default configuration values, but are set explicitly here for clarity and + to override any environment defined in pytest.ini. + """ + monkeypatch.setattr(settings, "graph_address", "localhost") + monkeypatch.setattr(settings, "graph_port", 7200) + monkeypatch.setattr(settings, "graph_db", "repositories/my_db") + + @pytest.fixture() def test_data(): """Create valid aggregate response data for two toy datasets for testing.""" diff --git a/tests/test_query.py b/tests/test_query.py index b552e4b..4e02636 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -649,18 +649,15 @@ def test_query_without_token_succeeds_when_auth_disabled( @pytest.mark.integration def test_integration_query_without_auth_succeeds( - test_app, monkeypatch, disable_auth + test_app, + monkeypatch, + disable_auth, + set_graph_url_vars_for_integration_tests, ): """ Running a test against a real local test graph should succeed when authentication is disabled. """ - # Patching the query URL directly means we don't need to worry about the constituent - # graph environment variables - monkeypatch.setattr( - settings, "query_url", "http://localhost:7200/repositories/my_db" - ) - response = test_app.get(ROUTE) assert response.status_code == 200 @@ -727,15 +724,15 @@ def test_missing_derivatives_info_handled_by_nonagg_api_response( @pytest.mark.integration def test_only_imaging_and_phenotypic_sessions_returned_in_query_response( - test_app, monkeypatch, disable_auth + test_app, + monkeypatch, + disable_auth, + set_graph_url_vars_for_integration_tests, ): """ Test that only sessions of type PhenotypicSession and ImagingSession are returned in an unaggregated query response. """ monkeypatch.setattr(settings, "return_agg", False) - monkeypatch.setattr( - settings, "query_url", "http://localhost:7200/repositories/my_db" - ) response = test_app.get(ROUTE) assert response.status_code == 200 @@ -757,14 +754,16 @@ def test_only_imaging_and_phenotypic_sessions_returned_in_query_response( @pytest.mark.integration -def test_min_cell_size_removes_results(test_app, monkeypatch, disable_auth): +def test_min_cell_size_removes_results( + test_app, + monkeypatch, + disable_auth, + set_graph_url_vars_for_integration_tests, +): """ If the minimum cell size is large enough, all results should be filtered out """ monkeypatch.setattr(settings, "min_cell_size", 100) - monkeypatch.setattr( - settings, "query_url", "http://localhost:7200/repositories/my_db" - ) response = test_app.get(ROUTE) assert response.status_code == 200