Skip to content

Commit b42fa4e

Browse files
committed
Try testing the API call again (I don't believe it's not doable)
1 parent de4a73a commit b42fa4e

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

tests/server/api/test_movies.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
from unittest.mock import ANY, patch
2+
3+
from fastapi.testclient import TestClient
14
from sqlmodel import Session
25

3-
from murfey.server.api.session_control import count_number_of_movies
6+
from murfey.server.api.auth import validate_instrument_token
7+
from murfey.server.main import app
8+
from murfey.util.api import url_path_for
9+
from murfey.util.config import security_from_file
410
from murfey.util.db import (
511
AutoProcProgram,
612
DataCollection,
@@ -9,13 +15,17 @@
915
MurfeyLedger,
1016
ProcessingJob,
1117
)
12-
from tests.conftest import ExampleVisit, get_or_create_db_entry
18+
from tests.conftest import ExampleVisit, get_or_create_db_entry, murfey_db_url
1319

1420

1521
def test_movie_count(
22+
mock_security_configuration,
1623
murfey_db_session: Session, # From conftest.py
1724
):
1825

26+
def override_get_db():
27+
yield murfey_db_session
28+
1929
# Insert table dependencies
2030
dcg_entry: DataCollectionGroup = get_or_create_db_entry(
2131
murfey_db_session,
@@ -77,6 +87,28 @@ def test_movie_count(
7787
},
7888
)
7989

80-
# Run function and evaluate result
81-
result = count_number_of_movies(murfey_db_session)
82-
assert result == {tag: num_movies}
90+
# Load up test client
91+
with (
92+
patch(
93+
"murfey.server.murfey_db.get_security_config"
94+
) as mock_get_security_config,
95+
patch("murfey.server.murfey_db.url") as mock_get_url,
96+
):
97+
# Mock out security config and url before importing 'murfey_db' for overriding
98+
mock_get_security_config.return_value = security_from_file(
99+
mock_security_configuration
100+
)
101+
mock_get_url.return_value = murfey_db_url
102+
from murfey.server.murfey_db import murfey_db
103+
104+
# Override database and validation in app
105+
app.dependency_overrides[murfey_db] = override_get_db
106+
app.dependency_overrides[validate_instrument_token] = lambda: None
107+
108+
with TestClient(app) as client:
109+
response = client.get(
110+
f"{url_path_for('session_control.router', 'count_number_of_movies')}",
111+
headers={"Authorization": f"Bearer {ANY}"},
112+
)
113+
assert response.status_code == 200
114+
assert response.json() == {tag: num_movies}

0 commit comments

Comments
 (0)