|
| 1 | +"""Tests for the admin CLI whole-bank transfer boundary.""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from unittest.mock import AsyncMock |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from hindsight_api.admin import cli |
| 9 | + |
| 10 | + |
| 11 | +class _FakeConnection: |
| 12 | + def __init__(self) -> None: |
| 13 | + self.closed = False |
| 14 | + |
| 15 | + async def close(self) -> None: |
| 16 | + self.closed = True |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.asyncio |
| 20 | +async def test_run_export_bank_declares_decoded_json_rows(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: |
| 21 | + """The codec-enabled admin producer must identify its rows as decoded.""" |
| 22 | + connection = _FakeConnection() |
| 23 | + export_bank = AsyncMock(return_value=b"archive") |
| 24 | + |
| 25 | + async def fake_admin_connect(db_url: str) -> _FakeConnection: |
| 26 | + assert db_url == "postgresql://example" |
| 27 | + return connection |
| 28 | + |
| 29 | + monkeypatch.setattr(cli, "_admin_connect", fake_admin_connect) |
| 30 | + monkeypatch.setattr(cli, "export_bank", export_bank) |
| 31 | + |
| 32 | + output = tmp_path / "bank.zip" |
| 33 | + size = await cli._run_export_bank( |
| 34 | + "postgresql://example", |
| 35 | + "source-bank", |
| 36 | + output, |
| 37 | + "tenant_schema", |
| 38 | + include_history=True, |
| 39 | + ) |
| 40 | + |
| 41 | + export_bank.assert_awaited_once_with( |
| 42 | + connection, |
| 43 | + "source-bank", |
| 44 | + include_history=True, |
| 45 | + bank_rows_json_encoding="decoded", |
| 46 | + ) |
| 47 | + assert output.read_bytes() == b"archive" |
| 48 | + assert size == len(b"archive") |
| 49 | + assert connection.closed is True |
0 commit comments