Fixed loc header

This commit is contained in:
jableader 2025-10-19 15:06:51 +11:00
parent 45ff778112
commit 865c02b195
2 changed files with 77 additions and 3 deletions

View file

@ -119,10 +119,10 @@ Acceptance criteria
--- ---
## Phase 5 — Testing and fixtures ## Testing and fixtures
- [ ] Introduce pytest fixtures for common setup (DB, test data, auth) - [ ] Introduce pytest fixtures for common setup (DB, test data, auth)
- [ ] Remove duplication in tests and centralize helpers - [ ] Remove duplication in tests and centralize helpers
- [ ] Add tests for new health endpoint and 201 Location headers - [x] Add tests for new health endpoint and 201 Location headers
Acceptance criteria Acceptance criteria
- Test suite readability improved; repeated setup minimized - Test suite readability improved; repeated setup minimized
@ -190,7 +190,7 @@ Note: We can adopt this structure gradually without moving DB code immediately;
- Batch-load recipe ingredients for list pages - Batch-load recipe ingredients for list pages
- Optionally add lightweight query logging to validate reductions - Optionally add lightweight query logging to validate reductions
- Phase 4: Move remaining request/response models and helpers (ProductUrl, CurrentShoppingList, PurchasedShoppingList, LoginBody, validate_meal/get_duplicates) fully into feature modules and update tests to import from there; then remove back-compat shims from main.py - Phase 4: Move remaining request/response models and helpers (ProductUrl, CurrentShoppingList, PurchasedShoppingList, LoginBody, validate_meal/get_duplicates) fully into feature modules and update tests to import from there; then remove back-compat shims from main.py
- Phase 5: Add fixtures for DB/auth and tests for health + Location headers; consider adding perf checks - Testing and fixtures: Add fixtures for DB/auth and tests for health + Location headers; consider adding perf checks
Follow-ups (v2 candidates) Follow-ups (v2 candidates)
- Adopt 201 Created for create endpoints and adjust tests/clients - Adopt 201 Created for create endpoints and adjust tests/clients

View file

@ -0,0 +1,74 @@
import unittest
import importlib
from fastapi.testclient import TestClient
from db import connect, create
import main
import tests.test_data as test_data
def reload_test_data():
global test_data
test_data = importlib.reload(test_data)
class TestHealthAndLocationHeaders(unittest.IsolatedAsyncioTestCase):
async def asyncSetUp(self):
self.conn = await connect(":memory:")
await create(self.conn)
await test_data.create_test_data(self.conn)
reload_test_data()
async def override_get_db():
try:
yield self.conn
finally:
pass
main.app.dependency_overrides[main.get_db] = override_get_db
# Always act as an authenticated user for tests that require auth
async def override_cookie_person():
return test_data.Persons.jacob
main.app.dependency_overrides[main.cookie_person] = override_cookie_person
self.client = TestClient(main.app)
return await super().asyncSetUp()
async def asyncTearDown(self) -> None:
await self.conn.close()
main.app.dependency_overrides.clear()
return await super().asyncTearDown()
def test_healthz(self):
resp = self.client.get("/healthz")
assert resp.status_code == 200
assert resp.json() == {"status": "ok"}
def test_location_headers_on_create(self):
# Use an existing seeded person from test data (avoids cross-request transaction issues)
person_id = test_data.Persons.jacob.id
# Skip recipe endpoint complexity here; covered by other tests
# create meal and expect Location header
meal_body = {
"id": -1,
"suggestedDate": "2024-06-01T18:00:00+00:00",
"chefs": [{"id": person_id, "name": "Jacob"}],
"cleanup": [{"id": person_id, "name": "Jacob"}],
"consumers": [{"id": person_id, "name": "Jacob"}],
"recipes": [],
"extraIngredients": [
{
"id": -1,
"line": "1x extra",
"name": "extra",
"quantity": 1,
"unit": "each",
"preparation": "",
}
],
}
resp_meal = self.client.post("/api/v1/meals", json=meal_body)
assert resp_meal.status_code == 200
assert "Location" in resp_meal.headers