Skip to content

Commit cf28862

Browse files
fix(root): isolate api test sqlite database per process
Use a process-local temporary SQLite path for API tests so concurrent test runs and pre-push hooks do not collide on the same file-backed database. Co-Authored-By: First Fluke <our.first.fluke@gmail.com>
1 parent 74c8dc4 commit cf28862

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

apps/api/tests/conftest.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
import asyncio
22
import os
3+
import tempfile
34
from collections.abc import Iterator
45
from pathlib import Path
56

67
import pytest
78
from fastapi.testclient import TestClient
89

9-
TEST_DB_PATH = Path(__file__).parent / "test.db"
10+
TEST_DB_PATH = (
11+
Path(tempfile.gettempdir()) / f"fullstack-starter-api-tests-{os.getpid()}.db"
12+
)
1013
os.environ["PROJECT_ENV"] = "staging"
1114
os.environ["DATABASE_URL"] = f"sqlite+aiosqlite:///{TEST_DB_PATH}"
1215
os.environ["DATABASE_URL_SYNC"] = f"sqlite:///{TEST_DB_PATH}"
1316

1417
from src.lib.database import Base, engine # noqa: E402
1518
from src.main import app # noqa: E402
1619

20+
if TEST_DB_PATH.exists():
21+
TEST_DB_PATH.unlink()
22+
1723

1824
@pytest.fixture(autouse=True)
1925
def reset_database() -> Iterator[None]:
@@ -33,3 +39,11 @@ def client() -> Iterator[TestClient]:
3339
"""Test client fixture."""
3440
with TestClient(app) as test_client:
3541
yield test_client
42+
43+
44+
@pytest.fixture(scope="session", autouse=True)
45+
def cleanup_test_database() -> Iterator[None]:
46+
"""Remove the process-local SQLite file after the test session."""
47+
yield
48+
if TEST_DB_PATH.exists():
49+
TEST_DB_PATH.unlink()

0 commit comments

Comments
 (0)