Skip to content

Commit a46bbc5

Browse files
authored
✅ Update database setup for tests (fastapi#1226)
* 🗃️ Update database setup for tests * ✅ Add pragmas and update db handling for tests
1 parent a4405bb commit a46bbc5

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

tests/test_tutorial/test_sql_databases/test_sql_databases.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import importlib
12
from pathlib import Path
23

34
import pytest
@@ -283,13 +284,18 @@
283284

284285
@pytest.fixture(scope="module")
285286
def client():
287+
test_db = Path("./sql_app.db")
288+
if test_db.is_file(): # pragma: nocover
289+
test_db.unlink()
286290
# Import while creating the client to create the DB after starting the test session
287-
from sql_databases.sql_app.main import app
291+
from sql_databases.sql_app import main
288292

289-
test_db = Path("./sql_app.db")
290-
with TestClient(app) as c:
293+
# Ensure import side effects are re-executed
294+
importlib.reload(main)
295+
with TestClient(main.app) as c:
291296
yield c
292-
test_db.unlink()
297+
if test_db.is_file(): # pragma: nocover
298+
test_db.unlink()
293299

294300

295301
def test_openapi_schema(client):

tests/test_tutorial/test_sql_databases/test_sql_databases_middleware.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import importlib
12
from pathlib import Path
23

34
import pytest
@@ -283,13 +284,19 @@
283284

284285
@pytest.fixture(scope="module")
285286
def client():
287+
test_db = Path("./sql_app.db")
288+
if test_db.is_file(): # pragma: nocover
289+
test_db.unlink()
286290
# Import while creating the client to create the DB after starting the test session
287-
from sql_databases.sql_app.alt_main import app
291+
from sql_databases.sql_app import alt_main
288292

289-
test_db = Path("./sql_app.db")
290-
with TestClient(app) as c:
293+
# Ensure import side effects are re-executed
294+
importlib.reload(alt_main)
295+
296+
with TestClient(alt_main.app) as c:
291297
yield c
292-
test_db.unlink()
298+
if test_db.is_file(): # pragma: nocover
299+
test_db.unlink()
293300

294301

295302
def test_openapi_schema(client):
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
import importlib
12
from pathlib import Path
23

34

45
def test_testing_dbs():
6+
test_db = Path("./test.db")
7+
if test_db.is_file(): # pragma: nocover
8+
test_db.unlink()
59
# Import while creating the client to create the DB after starting the test session
6-
from sql_databases.sql_app.tests.test_sql_app import test_create_user
10+
from sql_databases.sql_app.tests import test_sql_app
711

8-
test_db = Path("./test.db")
9-
app_db = Path("./sql_app.db")
10-
test_create_user()
11-
test_db.unlink()
12-
if app_db.is_file(): # pragma: nocover
13-
app_db.unlink()
12+
# Ensure import side effects are re-executed
13+
importlib.reload(test_sql_app)
14+
test_sql_app.test_create_user()
15+
if test_db.is_file(): # pragma: nocover
16+
test_db.unlink()

0 commit comments

Comments
 (0)