Files
ik 870d9cc7f9
CI / backend-and-migrations (push) Canceled after 0s
CI / astro-build (push) Canceled after 0s
CI / compose-e2e (push) Canceled after 0s
feat: harden production data and backups
2026-09-06 14:08:07 +07:00

28 lines
1.2 KiB
Python

from sqlalchemy import create_engine, func, select
from sqlalchemy.orm import Session, sessionmaker
from sqlalchemy.pool import StaticPool
from app.database import Base
from app.models import CatchReport, Fish, Spot, Waterbody
from app import seed as seed_module
def test_seed_repairs_partial_database_and_is_idempotent(monkeypatch) -> None:
engine = create_engine("sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool)
Base.metadata.create_all(engine)
sessions = sessionmaker(bind=engine, expire_on_commit=False)
with Session(engine) as db:
db.add(Fish(slug="pike", name_ru="Щука", trophy_weight_g=10_000))
db.commit()
monkeypatch.setattr(seed_module, "SessionLocal", sessions)
monkeypatch.setattr(seed_module.settings, "seed_demo_data", False)
seed_module.seed()
seed_module.seed()
with Session(engine) as db:
assert db.scalar(select(func.count()).select_from(Fish)) == 2
assert db.scalar(select(func.count()).select_from(Waterbody)) == 2
assert db.scalar(select(func.count()).select_from(Spot)) == 2
assert db.scalar(select(func.count()).select_from(CatchReport)) == 0