|
| 1 | +from unittest.mock import ANY, patch |
| 2 | + |
| 3 | +from fastapi.testclient import TestClient |
1 | 4 | from sqlmodel import Session |
2 | 5 |
|
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 |
4 | 10 | from murfey.util.db import ( |
5 | 11 | AutoProcProgram, |
6 | 12 | DataCollection, |
|
9 | 15 | MurfeyLedger, |
10 | 16 | ProcessingJob, |
11 | 17 | ) |
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 |
13 | 19 |
|
14 | 20 |
|
15 | 21 | def test_movie_count( |
| 22 | + mock_security_configuration, |
16 | 23 | murfey_db_session: Session, # From conftest.py |
17 | 24 | ): |
18 | 25 |
|
| 26 | + def override_get_db(): |
| 27 | + yield murfey_db_session |
| 28 | + |
19 | 29 | # Insert table dependencies |
20 | 30 | dcg_entry: DataCollectionGroup = get_or_create_db_entry( |
21 | 31 | murfey_db_session, |
@@ -77,6 +87,28 @@ def test_movie_count( |
77 | 87 | }, |
78 | 88 | ) |
79 | 89 |
|
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