@davoodmehraban89 / Aram-2 Mirror · read-only / commits / 4ad6fc5
Fork 0

Mirrored from https://github.com/davoodmehraban89/Aram.git

Synced Dec 3, 19:30 UTC

test(mapping): reject audit migration over preexisting unbackfilled drafts

davoodmehraban89 committed Oct 30, 2026 at 19:53 UTC 4ad6fc5239c9cf08e7fb960026d235ad05af438f
1 file changed +66
backend/tests/test_mapping_draft_store.py
+66
@@ -385,3 +385,69 @@ def test_draft_event_is_system_staging_not_user_approval(setup):
385 assert "reviewer_id" not in mapping_draft_events.c
386 assert not hasattr(saved, "approve")
387 assert session.scalars(select(MappingRule)).all() == []
388 +
389 +
390 +def test_sqlite_parent_draft_cannot_be_deleted_while_audit_event_exists(setup):
391 + from sqlalchemy import delete
392 + session, year, note, _ = setup
393 + staged = save_unreviewed_batch(
394 + session, proposal(session, year, note), request_key="audit-09",
395 + )
396 + session.commit()
397 + with pytest.raises(IntegrityError):
398 + session.execute(
399 + delete(mapping_review_drafts).where(mapping_review_drafts.c.id == staged.id)
400 + )
401 + session.rollback()
402 + assert verify_draft_staging_event(
403 + session, draft_id=staged.id, year_id=year.id
404 + ) is None
405 +
406 +
407 +def test_separate_drafts_get_separate_insert_only_events(setup):
408 + session, year, cash, other = setup
409 + a = save_unreviewed_batch(
410 + session, proposal(session, year, cash, key="cash"), request_key="audit-10",
411 + )
412 + b = save_unreviewed_batch(
413 + session, proposal(session, year, other, key="ar"), request_key="audit-11",
414 + )
415 + session.commit()
416 + assert a.id != b.id
417 + events = session.execute(select(mapping_draft_events)).mappings().all()
418 + assert len(events) == 2
419 + assert {x["draft_id"] for x in events} == {a.id, b.id}
420 + assert {x["event_hash"] for x in events} != {a.checksum}
421 + assert session.scalars(select(MappingRule)).all() == []
422 +
423 +
424 +def test_audit_migration_refuses_legacy_drafts_without_backfill(setup, monkeypatch):
425 + # A database that has run only the preceding PR #53 migration can already
426 + # contain drafts. Adding an empty audit table must NOT make their readback
427 + # silently fail; block the upgrade until explicit owner-approved recovery.
428 + from importlib.util import module_from_spec, spec_from_file_location
429 + from pathlib import Path
430 + import alembic.op as op
431 +
432 + session, year, note, _ = setup
433 + save_unreviewed_batch(
434 + session, proposal(session, year, note), request_key="audit-12",
435 + )
436 + session.commit()
437 +
438 + path = (
439 + Path(__file__).resolve().parents[1] / "migrations" / "versions"
440 + / "20261030_03_system_staging_audit.py"
441 + )
442 + spec = spec_from_file_location("audit_migration_test", path)
443 + assert spec is not None and spec.loader is not None
444 + migration = module_from_spec(spec)
445 + spec.loader.exec_module(migration)
446 + monkeypatch.setattr(op, "get_bind", lambda: session.connection())
447 + def unexpected_ddl(*args, **kwargs):
448 + raise AssertionError("migration must stop before any DDL on nonempty drafts")
449 + monkeypatch.setattr(op, "create_table", unexpected_ddl)
450 +
451 + with pytest.raises(RuntimeError, match="existing.*draft|backfill"):
452 + migration.upgrade()
453 + assert len(session.execute(select(mapping_draft_events)).all()) == 1