Skip to content

Commit

Permalink
create fixture for graph URL vars for integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alyssadai committed Feb 21, 2025
1 parent 9166c1a commit 7fd4eb3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
1 change: 1 addition & 0 deletions app/api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
6 changes: 5 additions & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
29 changes: 14 additions & 15 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 7fd4eb3

Please sign in to comment.