9e46bdc460
- main.py: add docstring inside the lifespan function explaining why dispose_engine is lazy-imported rather than top-level. The original import path (top-level) crashed the container at import time with 'ImportError: cannot import name dispose_engine from cartsnitch_api.database' when database.py was stale or stripped during a CI build. Lazy import keeps the engine disposal behavior while preventing the module-load crash. - tests/test_openapi.py: add test_dispose_engine_importable_from_database that asserts dispose_engine is importable and callable. This is the exact path the deployed UAT image was failing on, captured as a regression test so a future regression lands in CI before deploy. Refs CAR-1135. Co-Authored-By: Paperclip <noreply@paperclip.ing>
103 lines
3.5 KiB
Python
103 lines
3.5 KiB
Python
"""Verify all expected routes are present in the OpenAPI spec."""
|
|
|
|
import pytest
|
|
from httpx import ASGITransport, AsyncClient
|
|
|
|
from cartsnitch_api.database import dispose_engine
|
|
from cartsnitch_api.main import app
|
|
|
|
|
|
def test_dispose_engine_importable_from_database():
|
|
"""Regression for CAR-1135: api main.py used to import dispose_engine
|
|
at module level. A stale database.py (no dispose_engine) crashed the
|
|
container at import time with ImportError on line 9. The fix moved
|
|
the import inside the lifespan function, but `dispose_engine` must
|
|
still be importable from `cartsnitch_api.database` for the lifespan
|
|
teardown to actually close pooled connections.
|
|
"""
|
|
assert callable(dispose_engine)
|
|
assert dispose_engine.__name__ == "dispose_engine"
|
|
|
|
EXPECTED_ROUTES = [
|
|
# Auth (3 — register/login/refresh are handled by Better-Auth service)
|
|
("get", "/auth/me"),
|
|
("patch", "/auth/me"),
|
|
("delete", "/auth/me"),
|
|
# Stores (4)
|
|
("get", "/api/v1/stores"),
|
|
("get", "/api/v1/me/stores"),
|
|
("post", "/api/v1/me/stores/{store_slug}/connect"),
|
|
("delete", "/api/v1/me/stores/{store_slug}"),
|
|
# Purchases (3)
|
|
("get", "/api/v1/purchases"),
|
|
("get", "/api/v1/purchases/stats"),
|
|
("get", "/api/v1/purchases/{purchase_id}"),
|
|
# Products (3)
|
|
("get", "/api/v1/products"),
|
|
("get", "/api/v1/products/{product_id}"),
|
|
("get", "/api/v1/products/{product_id}/prices"),
|
|
# Prices (3)
|
|
("get", "/api/v1/prices/trends"),
|
|
("get", "/api/v1/prices/increases"),
|
|
("get", "/api/v1/prices/comparison"),
|
|
# Coupons (2)
|
|
("get", "/api/v1/coupons"),
|
|
("get", "/api/v1/coupons/relevant"),
|
|
# Shopping (2)
|
|
("post", "/api/v1/shopping/optimize"),
|
|
("get", "/api/v1/shopping/lists"),
|
|
# Alerts (3)
|
|
("get", "/api/v1/alerts"),
|
|
("get", "/api/v1/alerts/settings"),
|
|
("put", "/api/v1/alerts/settings"),
|
|
# Scraping (2)
|
|
("post", "/api/v1/scraping/{store_slug}/sync"),
|
|
("get", "/api/v1/scraping/status"),
|
|
# Public (3)
|
|
("get", "/api/v1/public/trends/{product_id}"),
|
|
("get", "/api/v1/public/store-comparison"),
|
|
("get", "/api/v1/public/inflation"),
|
|
# Health (1)
|
|
("get", "/health"),
|
|
]
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_all_routes_in_openapi():
|
|
transport = ASGITransport(app=app)
|
|
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
|
resp = await client.get("/openapi.json")
|
|
assert resp.status_code == 200
|
|
spec = resp.json()
|
|
paths = spec["paths"]
|
|
|
|
registered = set()
|
|
for path, methods in paths.items():
|
|
for method in methods:
|
|
if method in ("get", "post", "put", "delete", "patch"):
|
|
registered.add((method, path))
|
|
|
|
missing = []
|
|
for method, path in EXPECTED_ROUTES:
|
|
if (method, path) not in registered:
|
|
missing.append(f"{method.upper()} {path}")
|
|
|
|
assert not missing, "Missing routes in OpenAPI spec:\n" + "\n".join(missing)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_route_count():
|
|
transport = ASGITransport(app=app)
|
|
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
|
resp = await client.get("/openapi.json")
|
|
spec = resp.json()
|
|
paths = spec["paths"]
|
|
|
|
count = 0
|
|
for _path, methods in paths.items():
|
|
for method in methods:
|
|
if method in ("get", "post", "put", "delete", "patch"):
|
|
count += 1
|
|
|
|
assert count == 31, f"Expected 31 routes, found {count}"
|