Files
Meal-Planner/backend/tests/test_alembic.py
T
adminandClaude Opus 4.7 8e89f793d5 feat: phase r1+r2 recovery + r3-0 swiftly api ingestion
R1 stabilization: pytest harness with transactional db fixture, smoke
+ alembic + auth + scrape + approval + swiftly tests, github actions
ci yaml. Bearer-token admin auth + signed-cookie session for family
ui mutations. Async POST /api/admin/scrape (BackgroundTasks, returns
202). Path canonicalization (no /list, /planned suffixes). DATABASE_URL
fail-fast on empty.

R2 deferred-risk spikes: live lucky california fetch (R2-A), full
email+per-voter approval click round trip with single-use enforcement
(R2-B, console email backend, sendgrid stub).

R3-0 phase 3 redesign: replaced playwright html scraper with requests
based swiftly json api client. 17 categories, ~10k products per scrape,
upsert by (source, external_id). 401 surfaces actionable token-refresh
message via ScrapeLog.error_message.

Pre-existing defects fixed: shopping_list.py syntax error blocking app
import, MealPlan.votes orphan relationship, JSONB(astext=True) invalid
kwarg, missing requests dep, calorie_target schema drift, every SQLEnum
needed values_callable, 0001 had empty downgrade(), seed had duplicate
ingredient rows.

Migrations added: 0003 grocery_item.description, 0004 family_profile.
calorie_target, 0005 grocery_item.external_id + source + composite index.

Verified: 31/31 pytest green, alembic upgrade->downgrade->upgrade clean,
frontend npm run build clean, live scrape 9,960 grocery_item rows in 36s.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 14:08:19 -07:00

48 lines
1.3 KiB
Python

"""
Alembic round-trip test.
Verifies that ``alembic upgrade head`` followed by ``alembic downgrade base``
runs without error against a Postgres throwaway database. Skipped when no
Postgres is reachable (the migrations rely on PG-specific types and cannot
target SQLite).
"""
from __future__ import annotations
import os
import pathlib
import subprocess
import pytest
BACKEND_ROOT = pathlib.Path(__file__).resolve().parent.parent
@pytest.mark.requires_postgres
def test_alembic_upgrade_head_roundtrip():
dsn = os.environ.get("TEST_DATABASE_URL") or os.environ.get("DATABASE_URL")
assert dsn, "TEST_DATABASE_URL or DATABASE_URL must be set"
env = os.environ.copy()
env["DATABASE_URL"] = dsn
# The session fixture has already upgraded; downgrade then re-upgrade to
# exercise both paths inside this test without polluting the rest of the
# session schema state.
down = subprocess.run(
["alembic", "downgrade", "base"],
cwd=str(BACKEND_ROOT),
env=env,
capture_output=True,
text=True,
)
assert down.returncode == 0, f"downgrade failed: {down.stderr}"
up = subprocess.run(
["alembic", "upgrade", "head"],
cwd=str(BACKEND_ROOT),
env=env,
capture_output=True,
text=True,
)
assert up.returncode == 0, f"upgrade failed: {up.stderr}"