|
1 | | -from unittest.mock import ANY, patch |
2 | | - |
3 | | -from fastapi.testclient import TestClient |
4 | 1 | from sqlmodel import Session |
5 | 2 |
|
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 |
| 3 | +from murfey.server.api.session_control import count_number_of_movies |
10 | 4 | from murfey.util.db import ( |
11 | 5 | AutoProcProgram, |
12 | 6 | DataCollection, |
|
15 | 9 | MurfeyLedger, |
16 | 10 | ProcessingJob, |
17 | 11 | ) |
18 | | -from tests.conftest import ExampleVisit, get_or_create_db_entry, murfey_db_url |
19 | | - |
20 | | -# @pytest.fixture(scope="module") |
21 | | -# def test_user(): |
22 | | -# return {"username": "testuser", "password": "testpass"} |
23 | | - |
24 | | - |
25 | | -# def movies_return(): |
26 | | -# return [("Supervisor_1", 2)] |
27 | | - |
28 | | - |
29 | | -# expression = Mock() |
30 | | -# expression.all = movies_return |
31 | | - |
32 | | -# mock_session = create_autospec(Session, instance=True) |
33 | | -# mock_session.exec.return_value = expression |
34 | | - |
35 | | - |
36 | | -# def override_murfey_db(): |
37 | | -# try: |
38 | | -# db = mock_session |
39 | | -# yield db |
40 | | -# finally: |
41 | | -# db.close() |
42 | | - |
43 | | - |
44 | | -# app.dependency_overrides[murfey_db] = override_murfey_db |
| 12 | +from tests.conftest import ExampleVisit, get_or_create_db_entry |
45 | 13 |
|
46 | 14 |
|
47 | 15 | def test_movie_count( |
48 | | - mock_security_configuration, # From conftest.py |
49 | 16 | murfey_db_session: Session, # From conftest.py |
50 | 17 | ): |
51 | 18 |
|
52 | | - # Set up mock security configs for |
53 | | - with ( |
54 | | - patch( |
55 | | - "murfey.server.murfey_db.get_security_config" |
56 | | - ) as mock_get_security_config, |
57 | | - patch("murfey.server.murfey_db.url") as mock_get_url, |
58 | | - ): |
59 | | - |
60 | | - # Defer import of 'murfey_db' until after mocks are configured |
61 | | - mock_get_url.return_value = murfey_db_url |
62 | | - mock_get_security_config.return_value = security_from_file( |
63 | | - mock_security_configuration |
| 19 | + # Insert table dependencies |
| 20 | + dcg_entry: DataCollectionGroup = get_or_create_db_entry( |
| 21 | + murfey_db_session, |
| 22 | + DataCollectionGroup, |
| 23 | + lookup_kwargs={ |
| 24 | + "id": 0, |
| 25 | + "session_id": ExampleVisit.murfey_session_id, |
| 26 | + "tag": "test_dcg", |
| 27 | + }, |
| 28 | + ) |
| 29 | + dc_entry: DataCollection = get_or_create_db_entry( |
| 30 | + murfey_db_session, |
| 31 | + DataCollection, |
| 32 | + lookup_kwargs={ |
| 33 | + "id": 0, |
| 34 | + "tag": "test_dc", |
| 35 | + "dcg_id": dcg_entry.id, |
| 36 | + }, |
| 37 | + ) |
| 38 | + processing_job_entry: ProcessingJob = get_or_create_db_entry( |
| 39 | + murfey_db_session, |
| 40 | + ProcessingJob, |
| 41 | + lookup_kwargs={ |
| 42 | + "id": 0, |
| 43 | + "recipe": "test_recipe", |
| 44 | + "dc_id": dc_entry.id, |
| 45 | + }, |
| 46 | + ) |
| 47 | + autoproc_entry: AutoProcProgram = get_or_create_db_entry( |
| 48 | + murfey_db_session, |
| 49 | + AutoProcProgram, |
| 50 | + lookup_kwargs={ |
| 51 | + "id": 0, |
| 52 | + "pj_id": processing_job_entry.id, |
| 53 | + }, |
| 54 | + ) |
| 55 | + |
| 56 | + # Insert test movies and one-to-one dependencies into Murfey DB |
| 57 | + tag = "test_movie" |
| 58 | + num_movies = 5 |
| 59 | + murfey_db_session |
| 60 | + for i in range(num_movies): |
| 61 | + murfey_ledger_entry: MurfeyLedger = get_or_create_db_entry( |
| 62 | + murfey_db_session, |
| 63 | + MurfeyLedger, |
| 64 | + lookup_kwargs={ |
| 65 | + "id": i, |
| 66 | + "app_id": autoproc_entry.id, |
| 67 | + }, |
| 68 | + ) |
| 69 | + _: Movie = get_or_create_db_entry( |
| 70 | + murfey_db_session, |
| 71 | + Movie, |
| 72 | + lookup_kwargs={ |
| 73 | + "murfey_id": murfey_ledger_entry.id, |
| 74 | + "path": "/some/path", |
| 75 | + "image_number": i, |
| 76 | + "tag": tag, |
| 77 | + }, |
64 | 78 | ) |
65 | | - from murfey.server.murfey_db import murfey_db |
66 | | - |
67 | | - # Replace the murfey_db instance in endpoint with properly initialised pytest one |
68 | | - app.dependency_overrides[murfey_db] = murfey_db_session |
69 | | - # Disable instrument token validation |
70 | | - app.dependency_overrides[validate_instrument_token] = lambda: None |
71 | | - |
72 | | - with TestClient(app) as client: |
73 | | - |
74 | | - # Insert table dependencies |
75 | | - dcg_entry: DataCollectionGroup = get_or_create_db_entry( |
76 | | - murfey_db_session, |
77 | | - DataCollectionGroup, |
78 | | - lookup_kwargs={ |
79 | | - "id": 0, |
80 | | - "session_id": ExampleVisit.murfey_session_id, |
81 | | - "tag": "test_dcg", |
82 | | - }, |
83 | | - ) |
84 | | - dc_entry: DataCollection = get_or_create_db_entry( |
85 | | - murfey_db_session, |
86 | | - DataCollection, |
87 | | - lookup_kwargs={ |
88 | | - "id": 0, |
89 | | - "tag": "test_dc", |
90 | | - "dcg_id": dcg_entry.id, |
91 | | - }, |
92 | | - ) |
93 | | - processing_job_entry: ProcessingJob = get_or_create_db_entry( |
94 | | - murfey_db_session, |
95 | | - ProcessingJob, |
96 | | - lookup_kwargs={ |
97 | | - "id": 0, |
98 | | - "recipe": "test_recipe", |
99 | | - "dc_id": dc_entry.id, |
100 | | - }, |
101 | | - ) |
102 | | - autoproc_entry: AutoProcProgram = get_or_create_db_entry( |
103 | | - murfey_db_session, |
104 | | - AutoProcProgram, |
105 | | - lookup_kwargs={ |
106 | | - "id": 0, |
107 | | - "pj_id": processing_job_entry.id, |
108 | | - }, |
109 | | - ) |
110 | | - |
111 | | - # Insert test movies and one-to-one dependencies into Murfey DB |
112 | | - tag = "test_movie" |
113 | | - num_movies = 5 |
114 | | - murfey_db_session |
115 | | - for i in range(num_movies): |
116 | | - murfey_ledger_entry: MurfeyLedger = get_or_create_db_entry( |
117 | | - murfey_db_session, |
118 | | - MurfeyLedger, |
119 | | - lookup_kwargs={ |
120 | | - "id": i, |
121 | | - "app_id": autoproc_entry.id, |
122 | | - }, |
123 | | - ) |
124 | | - _: Movie = get_or_create_db_entry( |
125 | | - murfey_db_session, |
126 | | - Movie, |
127 | | - lookup_kwargs={ |
128 | | - "murfey_id": murfey_ledger_entry.id, |
129 | | - "path": "/some/path", |
130 | | - "image_number": i, |
131 | | - "tag": tag, |
132 | | - }, |
133 | | - ) |
134 | 79 |
|
135 | | - response = client.get( |
136 | | - f"{url_path_for('session_control.router', 'count_number_of_movies')}", |
137 | | - headers={"Authorization": f"Bearer {ANY}"}, |
138 | | - ) |
139 | | - assert response.json() == {tag: num_movies} |
| 80 | + # Run function and evaluate result |
| 81 | + result = count_number_of_movies(murfey_db_session) |
| 82 | + assert result == {tag: num_movies} |
0 commit comments