feat: harden production data and backups
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
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
|
||||
Reference in New Issue
Block a user