Files
api/tests/test_routes/test_public.py
T
Barcode Betty b37f6f52d6
CI / lint (pull_request) Successful in 5m45s
CI / test (pull_request) Failing after 5m48s
CI / build-and-push (pull_request) Has been skipped
CI / typecheck (pull_request) Failing after 12m39s
CAR-1283: use relative seed date in test_public_trend
The hardcoded date(2026, 3, 5) is now > 90 days before
date.today() (2026-06-06), so the default days=90 window
filters it out and the test fails. Use a relative date
(30 days ago) to keep the test green indefinitely.
2026-06-06 01:17:03 +00:00

100 lines
3.0 KiB
Python

"""Integration tests for public endpoints (no auth)."""
import uuid
from datetime import date, timedelta
from decimal import Decimal
import pytest
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from cartsnitch_api.models import NormalizedProduct, PriceHistory, Store
@pytest.fixture
async def public_data(db_engine):
"""Seed data for public endpoints."""
factory = async_sessionmaker(db_engine, class_=AsyncSession, expire_on_commit=False)
async with factory() as session:
store = Store(name="Target", slug="target")
product = NormalizedProduct(
canonical_name="Skippy PB 16oz",
category="pantry",
brand="Skippy",
)
session.add_all([store, product])
await session.commit()
await session.refresh(store)
await session.refresh(product)
ph = PriceHistory(
normalized_product_id=product.id,
store_id=store.id,
observed_date=date.today() - timedelta(days=30),
regular_price=Decimal("3.99"),
source="receipt",
)
session.add(ph)
await session.commit()
return {"product": product, "store": store}
@pytest.mark.asyncio
async def test_public_trend(client, public_data):
pid = str(public_data["product"].id)
resp = await client.get(f"/api/v1/public/trends/{pid}")
assert resp.status_code == 200
data = resp.json()
assert data["product_name"] == "Skippy PB 16oz"
assert len(data["data_points"]) == 1
@pytest.mark.asyncio
async def test_public_trend_not_found(client):
resp = await client.get(f"/api/v1/public/trends/{uuid.uuid4()}")
assert resp.status_code == 404
@pytest.mark.asyncio
async def test_public_store_comparison(client, public_data):
pid = str(public_data["product"].id)
resp = await client.get(f"/api/v1/public/store-comparison?product_ids={pid}")
assert resp.status_code == 200
data = resp.json()
assert len(data["products"]) == 1
@pytest.mark.asyncio
async def test_public_inflation(client, public_data):
resp = await client.get("/api/v1/public/inflation")
assert resp.status_code == 200
data = resp.json()
assert "categories" in data
assert "cartsnitch_index" in data
@pytest.mark.asyncio
async def test_trend_invalid_uuid(client):
resp = await client.get("/api/v1/public/trends/not-a-uuid")
assert resp.status_code == 422
assert "detail" in resp.json()
assert "stack" not in resp.json()
@pytest.mark.asyncio
async def test_trend_days_zero(client, public_data):
pid = str(public_data["product"].id)
resp = await client.get(f"/api/v1/public/trends/{pid}?days=0")
assert resp.status_code == 422
assert "detail" in resp.json()
assert "stack" not in resp.json()
@pytest.mark.asyncio
async def test_trend_days_negative(client, public_data):
pid = str(public_data["product"].id)
resp = await client.get(f"/api/v1/public/trends/{pid}?days=-1")
assert resp.status_code == 422
assert "detail" in resp.json()
assert "stack" not in resp.json()