from __future__ import annotations from pathlib import Path from sqlalchemy import create_engine, func, select from sqlalchemy.orm import Session from app.database import Base from app.importer import import_records, parse_html from app.models import CatchReport, OfficialRecordImport, SourceType FIXTURE = Path(__file__).parents[3] / "tests" / "fixtures" / "records_ru_sample.html" def test_parser_and_import_are_idempotent() -> None: html = FIXTURE.read_text(encoding="utf-8") parsed = parse_html(html, region="RU", category="records") assert len(parsed) == 2 assert parsed[1].weight_g == 2_519_264 engine = create_engine("sqlite://") Base.metadata.create_all(engine) with Session(engine) as db: first = import_records(db, url="fixture://records", region="RU", category="records", html=html) second = import_records(db, url="fixture://records", region="RU", category="records", html=html) assert (first.rows_created, first.rows_updated) == (2, 0) assert (second.rows_created, second.rows_updated) == (0, 2) assert db.scalar(select(func.count()).select_from(CatchReport).where(CatchReport.source_type == SourceType.official_record)) == 2 assert db.scalar(select(func.count()).select_from(OfficialRecordImport)) == 2