feat: serialize official record imports
This commit is contained in:
@@ -7,7 +7,7 @@ from sqlalchemy import create_engine, func, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.database import Base
|
||||
from app.importer import FetchResult, ImportSourceError, import_records, parse_html
|
||||
from app.importer import FetchResult, ImportAlreadyRunning, ImportSourceError, _lock_key, _official_import_lock, import_records, parse_html
|
||||
from app.models import CatchReport, ImportStatus, OfficialRecordImport, SourceType
|
||||
|
||||
|
||||
@@ -15,6 +15,26 @@ FIXTURE = Path(__file__).parents[3] / "tests" / "fixtures" / "records_ru_sample.
|
||||
WEEKLY_FIXTURE = Path(__file__).parents[3] / "tests" / "fixtures" / "weekly_records_sample.html"
|
||||
|
||||
|
||||
def test_import_lock_is_stable_and_fails_closed_when_busy() -> None:
|
||||
class Connection:
|
||||
def scalar(self, statement, parameters):
|
||||
assert "pg_try_advisory_lock" in str(statement)
|
||||
assert parameters == {"key": _lock_key("https://example.test", "RU", "records")}
|
||||
return False
|
||||
|
||||
def close(self):
|
||||
self.closed = True
|
||||
|
||||
connection = Connection()
|
||||
bind = type("Bind", (), {"dialect": type("Dialect", (), {"name": "postgresql"})(), "connect": lambda self: connection})()
|
||||
session = type("Session", (), {"get_bind": lambda self: bind})()
|
||||
assert _lock_key("https://example.test", "ru", "records") == _lock_key("https://example.test", "RU", "records")
|
||||
with pytest.raises(ImportAlreadyRunning, match="already running"):
|
||||
with _official_import_lock(session, url="https://example.test", region="RU", category="records"):
|
||||
raise AssertionError("busy lock must not enter import")
|
||||
assert connection.closed is True
|
||||
|
||||
|
||||
def test_parser_and_import_are_idempotent() -> None:
|
||||
html = FIXTURE.read_text(encoding="utf-8")
|
||||
parsed = parse_html(html, region="RU", category="records")
|
||||
|
||||
Reference in New Issue
Block a user