99 lines
3.6 KiB
Python
99 lines
3.6 KiB
Python
from __future__ import annotations
|
|
|
|
from sqlalchemy import create_engine, func, select
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.db.base import Base
|
|
from app.models.contact import Contact
|
|
from app.models.customer import Customer, CustomerType
|
|
from app.models.device import Device
|
|
from app.models.equipment import Equipment
|
|
from app.models.validation import Validation
|
|
from app import cli as app_cli
|
|
from app.services.demo_data import DemoDataService
|
|
|
|
|
|
def session() -> Session:
|
|
engine = create_engine("sqlite+pysqlite:///:memory:")
|
|
Base.metadata.create_all(engine)
|
|
return Session(engine)
|
|
|
|
|
|
def test_demo_data_cli_parser_accepts_expected_options():
|
|
args = app_cli.build_parser().parse_args(
|
|
[
|
|
"demo-data",
|
|
"--customers",
|
|
"10",
|
|
"--devices",
|
|
"10",
|
|
"--validations",
|
|
"20",
|
|
"--reports",
|
|
"--images",
|
|
]
|
|
)
|
|
|
|
assert args.command == "demo-data"
|
|
assert args.customers == 10
|
|
assert args.devices == 10
|
|
assert args.validations == 20
|
|
assert args.reports is True
|
|
assert args.images is True
|
|
|
|
|
|
def test_demo_data_service_is_idempotent_and_creates_realistic_data(tmp_path):
|
|
db = session()
|
|
service = DemoDataService(db, upload_root=tmp_path / "uploads", report_root=tmp_path / "reports")
|
|
|
|
summary_first = service.run(customers=10, devices=10, validations=20, images=True)
|
|
|
|
assert summary_first["customers"] == 10
|
|
assert summary_first["devices"] == 10
|
|
assert summary_first["validations"] == 20
|
|
assert db.scalar(select(func.count()).select_from(Customer)) == 10
|
|
assert db.scalar(select(func.count()).select_from(Device)) == 10
|
|
assert db.scalar(select(func.count()).select_from(Equipment)) >= 6
|
|
assert db.scalar(select(func.count()).select_from(Validation)) == 20
|
|
assert db.scalar(select(func.count()).select_from(Contact)) >= 10
|
|
assert list((tmp_path / "uploads").rglob("*.svg"))
|
|
|
|
summary_second = service.run(customers=10, devices=10, validations=20, images=True)
|
|
|
|
assert summary_second["customers"] == 10
|
|
assert db.scalar(select(func.count()).select_from(Customer)) == 10
|
|
assert db.scalar(select(func.count()).select_from(Device)) == 10
|
|
assert db.scalar(select(func.count()).select_from(Validation)) == 20
|
|
|
|
|
|
def test_demo_data_reset_removes_only_demo_rows(tmp_path):
|
|
db = session()
|
|
real_customer = Customer(
|
|
customer_type=CustomerType.practice,
|
|
name="Reale Praxis",
|
|
email="real@example.com",
|
|
)
|
|
db.add(real_customer)
|
|
db.flush()
|
|
|
|
service = DemoDataService(db, upload_root=tmp_path / "uploads", report_root=tmp_path / "reports")
|
|
service.run(customers=10, devices=10, validations=20, images=True)
|
|
service.reset()
|
|
|
|
assert db.scalar(select(Customer).where(Customer.email == "real@example.com")) is not None
|
|
assert db.scalar(select(Customer).where(Customer.notes.like("%DEMO_DATA%"))) is None
|
|
assert db.scalar(select(Device).where(Device.notes.like("%DEMO_DATA%"))) is None
|
|
assert db.scalar(select(Validation).where(Validation.notes.like("%DEMO_DATA%"))) is None
|
|
|
|
|
|
def test_demo_data_cli_command_uses_sessionlocal(monkeypatch):
|
|
db = session()
|
|
monkeypatch.setattr(app_cli, "SessionLocal", lambda: db)
|
|
|
|
args = app_cli.build_parser().parse_args(["demo-data", "--customers", "10", "--devices", "10", "--validations", "20"])
|
|
exit_code = app_cli.cmd_demo_data(args)
|
|
|
|
assert exit_code == 0
|
|
assert db.scalar(select(func.count()).select_from(Customer)) == 10
|
|
assert db.scalar(select(func.count()).select_from(Device)) == 10
|
|
assert db.scalar(select(func.count()).select_from(Validation)) == 20
|