Skip to content

Commit 8cb679a

Browse files
committed
Move test_client startup into test function, as database state doesn't carry over from test function into endpoint
1 parent c35cc74 commit 8cb679a

File tree

1 file changed

+70
-78
lines changed

1 file changed

+70
-78
lines changed

tests/server/api/test_movies.py

Lines changed: 70 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from unittest.mock import ANY, patch
22

3-
import pytest
43
from fastapi.testclient import TestClient
54
from sqlmodel import Session
65

@@ -45,11 +44,11 @@
4544
# app.dependency_overrides[murfey_db] = override_murfey_db
4645

4746

48-
@pytest.fixture
49-
def fastapi_test_client(
50-
mock_security_configuration,
51-
murfey_db_session,
47+
def test_movie_count(
48+
mock_security_configuration, # From conftest.py
49+
murfey_db_session: Session, # From conftest.py
5250
):
51+
5352
# Set up mock security configs for
5453
with (
5554
patch(
@@ -71,77 +70,70 @@ def fastapi_test_client(
7170
app.dependency_overrides[validate_instrument_token] = lambda: None
7271

7372
with TestClient(app) as client:
74-
yield client
75-
76-
77-
def test_movie_count(
78-
fastapi_test_client: TestClient,
79-
murfey_db_session: Session, # From conftest.py
80-
):
81-
82-
# Insert table dependencies
83-
dcg_entry: DataCollectionGroup = get_or_create_db_entry(
84-
murfey_db_session,
85-
DataCollectionGroup,
86-
lookup_kwargs={
87-
"id": 0,
88-
"session_id": ExampleVisit.murfey_session_id,
89-
"tag": "test_dcg",
90-
},
91-
)
92-
dc_entry: DataCollection = get_or_create_db_entry(
93-
murfey_db_session,
94-
DataCollection,
95-
lookup_kwargs={
96-
"id": 0,
97-
"tag": "test_dc",
98-
"dcg_id": dcg_entry.id,
99-
},
100-
)
101-
processing_job_entry: ProcessingJob = get_or_create_db_entry(
102-
murfey_db_session,
103-
ProcessingJob,
104-
lookup_kwargs={
105-
"id": 0,
106-
"recipe": "test_recipe",
107-
"dc_id": dc_entry.id,
108-
},
109-
)
110-
autoproc_entry: AutoProcProgram = get_or_create_db_entry(
111-
murfey_db_session,
112-
AutoProcProgram,
113-
lookup_kwargs={
114-
"id": 0,
115-
"pj_id": processing_job_entry.id,
116-
},
117-
)
118-
119-
# Insert test movies and one-to-one dependencies into Murfey DB
120-
tag = "test_movie"
121-
num_movies = 5
122-
murfey_db_session
123-
for i in range(num_movies):
124-
murfey_ledger_entry: MurfeyLedger = get_or_create_db_entry(
125-
murfey_db_session,
126-
MurfeyLedger,
127-
lookup_kwargs={
128-
"id": i,
129-
"app_id": autoproc_entry.id,
130-
},
131-
)
132-
_: Movie = get_or_create_db_entry(
133-
murfey_db_session,
134-
Movie,
135-
lookup_kwargs={
136-
"murfey_id": murfey_ledger_entry.id,
137-
"path": "/some/path",
138-
"image_number": i,
139-
"tag": tag,
140-
},
141-
)
14273

143-
response = fastapi_test_client.get(
144-
f"{url_path_for('session_control.router', 'count_number_of_movies')}",
145-
headers={"Authorization": f"Bearer {ANY}"},
146-
)
147-
assert response.json() == {tag: num_movies}
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+
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}

0 commit comments

Comments
 (0)