Files
TimeTracker/tests/test_api_per_diem_v1.py
T
MacJediWizard 3e67791b87 test(api-v1): use unique tempfile path per fixture, not hardcoded sqlite name
20 test files share an anti-pattern: each declares its own local `app`
fixture with a hardcoded SQLite filename in the test runner's CWD,
e.g.

    "SQLALCHEMY_DATABASE_URI": "sqlite:///test_api_kanban.sqlite",

Two problems:

1. If a prior test run crashed before teardown, the .sqlite file
   persists. The next run starts on a non-empty database and trips
   over FK / uniqueness constraints — `test_api_tax_currency_flow`
   surfaces this as a 409 Conflict when its USD currency row already
   exists.

2. `test_api_invoice_templates.sqlite` is used by two files
   (`test_api_invoice_templates_v1.py` and
   `test_api_invoice_templates_api_v1.py`). Under pytest-xdist they
   race for the same file and corrupt each other's state.

Fix: each fixture now generates a unique per-call path using the same
pattern already established in `tests/conftest.py:204`:

    unique_db_path = os.path.join(
        tempfile.gettempdir(), f"test_<slug>_{uuid.uuid4().hex}.sqlite"
    )
    "SQLALCHEMY_DATABASE_URI": f"sqlite:///{unique_db_path}",

Adds `import os`, `import tempfile`, `import uuid` where missing.
Test lifecycle is unchanged — the existing fixture teardown
(`db.drop_all()` + engine dispose) still runs; the OS later reaps the
temp file. Black-formatted to the project's 120-char line limit.

Test plan
- pytest tests/test_api_kanban_v1.py
- pytest tests/test_api_tax_currency_v1.py (no leftover-USD 409)
- pytest -p xdist -n 2 tests/test_api_invoice_templates_v1.py tests/test_api_invoice_templates_api_v1.py (no collision)
2026-05-14 17:04:30 -04:00

92 lines
2.3 KiB
Python

import pytest
import uuid
import tempfile
import os
pytestmark = [pytest.mark.api, pytest.mark.integration]
from datetime import date, timedelta
from app import create_app, db
from app.models import User, ApiToken
@pytest.fixture
def app():
unique_db_path = os.path.join(tempfile.gettempdir(), f"test_api_per_diem_{uuid.uuid4().hex}.sqlite")
app = create_app(
{
"TESTING": True,
"SQLALCHEMY_DATABASE_URI": f"sqlite:///{unique_db_path}",
"WTF_CSRF_ENABLED": False,
}
)
with app.app_context():
db.create_all()
yield app
db.session.remove()
db.drop_all()
@pytest.fixture
def client(app):
return app.test_client()
@pytest.fixture
def user(app):
u = User(username="pduser", email="pduser@example.com", role="user")
u.is_active = True
db.session.add(u)
db.session.commit()
return u
@pytest.fixture
def api_token(app, user):
token, plain = ApiToken.create_token(user_id=user.id, name="PerDiem Token", scopes="read:per_diem,write:per_diem")
db.session.add(token)
db.session.commit()
return plain
def _auth(t):
return {"Authorization": f"Bearer {t}", "Content-Type": "application/json"}
def test_per_diem_crud(client, api_token):
# list empty
r = client.get("/api/v1/per-diems", headers=_auth(api_token))
assert r.status_code == 200
assert r.get_json()["per_diems"] == []
# create
payload = {
"trip_purpose": "Conference",
"start_date": date.today().isoformat(),
"end_date": (date.today() + timedelta(days=2)).isoformat(),
"country": "Germany",
"full_day_rate": 30.0,
"half_day_rate": 15.0,
"full_days": 2,
"half_days": 0,
}
r = client.post("/api/v1/per-diems", headers=_auth(api_token), json=payload)
assert r.status_code == 201
pd = r.get_json()["per_diem"]
pd_id = pd["id"]
# get
r = client.get(f"/api/v1/per-diems/{pd_id}", headers=_auth(api_token))
assert r.status_code == 200
# update
r = client.patch(f"/api/v1/per-diems/{pd_id}", headers=_auth(api_token), json={"notes": "OK"})
assert r.status_code == 200
assert r.get_json()["per_diem"]["notes"] == "OK"
# delete (reject)
r = client.delete(f"/api/v1/per-diems/{pd_id}", headers=_auth(api_token))
assert r.status_code == 200