Skip to content

Commit 00fe67f

Browse files
committed
Put the database and validation overrides in a test FastAPI client fixture
1 parent 89a0607 commit 00fe67f

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

tests/server/api/test_movies.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from unittest.mock import ANY
22

33
from fastapi.testclient import TestClient
4+
from pytest import fixture
45
from sqlmodel import Session
56

67
from murfey.server.api.auth import validate_instrument_token
@@ -43,11 +44,21 @@
4344

4445
# app.dependency_overrides[murfey_db] = override_murfey_db
4546

46-
client = TestClient(app)
47+
48+
@fixture(scope="module")
49+
def fastapi_client(murfey_db_session):
50+
# Replace the murfey_db instance in endpoint with properly initialised pytest one
51+
app.dependency_overrides[murfey_db] = murfey_db_session
52+
# Disable instrument token validation
53+
app.dependency_overrides[validate_instrument_token] = lambda: None
54+
55+
with TestClient(app) as client:
56+
yield client
4757

4858

4959
def test_movie_count(
50-
murfey_db_session: Session,
60+
fastapi_client: TestClient,
61+
murfey_db_session: Session, # From conftest.py
5162
):
5263

5364
# Insert table dependencies
@@ -111,12 +122,7 @@ def test_movie_count(
111122
},
112123
)
113124

114-
# Replace the murfey_db instance in endpoint with properly initialised pytest one
115-
app.dependency_overrides[murfey_db] = murfey_db_session
116-
# Disable instrument token validation
117-
app.dependency_overrides[validate_instrument_token] = lambda: None
118-
119-
response = client.get(
125+
response = fastapi_client.get(
120126
f"{url_path_for('session_control.router', 'count_number_of_movies')}",
121127
headers={"Authorization": f"Bearer {ANY}"},
122128
)

0 commit comments

Comments
 (0)