test_postgres_ledger.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from pathlib import Path
  2. from app.runner.ledger import PostgresTaskLedger
  3. class Result:
  4. rowcount = 1
  5. class Connection:
  6. def __init__(self):
  7. self.calls = []
  8. def execute(self, statement, parameters):
  9. self.calls.append((str(statement), parameters))
  10. return Result()
  11. class Begin:
  12. def __init__(self, connection):
  13. self.connection = connection
  14. def __enter__(self):
  15. return self.connection
  16. def __exit__(self, *_args):
  17. return False
  18. class Engine:
  19. def __init__(self):
  20. self.connection = Connection()
  21. def begin(self):
  22. return Begin(self.connection)
  23. def test_postgres_ledger_claim_is_atomic_and_persists_only_bounded_fields():
  24. engine = Engine()
  25. ledger = PostgresTaskLedger(engine)
  26. binding = {
  27. "task_uid": "01900000-0000-7000-8000-000000000011",
  28. "dataflow_uid": "01900000-0000-7000-8000-000000000012",
  29. "workflow_version": 7,
  30. "correlation_id": "01900000-0000-7000-8000-000000000013",
  31. "node_id": "write_orders",
  32. "node_type": "sql.execute",
  33. "data_source_uid": "01900000-0000-7000-8000-000000000014",
  34. "idempotency_key": "orders:2026-07-19",
  35. }
  36. assert ledger.claim(
  37. "01900000-0000-7000-8000-000000000015",
  38. binding,
  39. expires_at=2_000,
  40. )
  41. sql, parameters = engine.connection.calls[0]
  42. assert "ON CONFLICT DO NOTHING" in sql
  43. assert "encrypted_payload" not in sql
  44. assert "password" not in sql
  45. assert parameters["idempotency_key"] == "orders:2026-07-19"
  46. def test_v52_migration_has_single_use_and_commit_outcome_constraints():
  47. root = Path(__file__).resolve().parents[2]
  48. source = (
  49. root
  50. / "migrations/versions/20260719_70_runner_task_ledger.py"
  51. ).read_text(encoding="utf-8")
  52. assert "runner_task_executions" in source
  53. assert "token_jti UUID PRIMARY KEY" in source
  54. assert "UNIQUE (task_uid)" in source
  55. assert "'not_committed', 'committed', 'unknown'" in source
  56. assert "encrypted_payload" not in source
  57. assert "connection_string" not in source