From 865c02b195426185ad745408f4c34dec16e2178a Mon Sep 17 00:00:00 2001 From: jableader Date: Sun, 19 Oct 2025 15:06:51 +1100 Subject: [PATCH] Fixed loc header --- refactor-project-strategy.md | 6 +- tests/test_health_and_location_headers.py | 74 +++++++++++++++++++++++ 2 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 tests/test_health_and_location_headers.py diff --git a/refactor-project-strategy.md b/refactor-project-strategy.md index e7e3285..81dcdb3 100644 --- a/refactor-project-strategy.md +++ b/refactor-project-strategy.md @@ -119,10 +119,10 @@ Acceptance criteria --- -## Phase 5 — Testing and fixtures +## Testing and fixtures - [ ] Introduce pytest fixtures for common setup (DB, test data, auth) - [ ] 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 - 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 - 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 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) - Adopt 201 Created for create endpoints and adjust tests/clients diff --git a/tests/test_health_and_location_headers.py b/tests/test_health_and_location_headers.py new file mode 100644 index 0000000..0d90d32 --- /dev/null +++ b/tests/test_health_and_location_headers.py @@ -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