|
| 1 | +"""Unit tests for database init / enum migration helpers.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Any |
| 6 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from voucherbot.database import init_db as init |
| 11 | +from voucherbot.database.init_db import _ensure_source_type_enum |
| 12 | + |
| 13 | + |
| 14 | +class _FakeResult: |
| 15 | + """Minimal stand-in for a SQLAlchemy result/mapping.""" |
| 16 | + |
| 17 | + def __init__(self, rows: list[Any]) -> None: |
| 18 | + self._rows = rows |
| 19 | + |
| 20 | + def __iter__(self) -> Any: |
| 21 | + return iter(self._rows) |
| 22 | + |
| 23 | + def scalar(self) -> Any: |
| 24 | + if not self._rows: |
| 25 | + return None |
| 26 | + return self._rows[0][0] if isinstance(self._rows[0], tuple) else self._rows[0] |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.asyncio |
| 30 | +async def test_ensure_source_type_enum_skips_when_type_missing() -> None: |
| 31 | + conn = _conn_mock(scalar_value=False) |
| 32 | + engine = MagicMock() |
| 33 | + engine.connect.return_value = conn |
| 34 | + |
| 35 | + with patch.object(init, "engine", engine): |
| 36 | + await _ensure_source_type_enum() |
| 37 | + |
| 38 | + conn.execute.assert_not_called() |
| 39 | + conn.__aexit__.assert_awaited_once() |
| 40 | + |
| 41 | + |
| 42 | +def _conn_mock(scalar_value: bool, rows: list[Any] | None = None) -> Any: |
| 43 | + """AsyncMock connection that behaves like a started ``AsyncConnection``. |
| 44 | +
|
| 45 | + Supports ``async with`` (starting/exit) and returns ``self`` from |
| 46 | + ``execution_options`` as the real connection does. |
| 47 | + """ |
| 48 | + conn = AsyncMock() |
| 49 | + conn.__aenter__.return_value = conn |
| 50 | + conn.execution_options.return_value = conn |
| 51 | + conn.scalar.return_value = scalar_value |
| 52 | + conn.execute.return_value = _FakeResult(rows or []) |
| 53 | + return conn |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.asyncio |
| 57 | +async def test_ensure_source_type_enum_adds_missing_values_only() -> None: |
| 58 | + calls: list[str] = [] |
| 59 | + |
| 60 | + async def fake_execute(statement: Any) -> Any: |
| 61 | + calls.append(str(statement)) |
| 62 | + if "enum_range" in str(statement): |
| 63 | + return _FakeResult([("REDDIT",), ("RSS",), ("BLOG",)]) |
| 64 | + return _FakeResult([]) |
| 65 | + |
| 66 | + conn = _conn_mock(scalar_value=True, rows=[("REDDIT",), ("RSS",), ("BLOG",)]) |
| 67 | + conn.execute.side_effect = fake_execute |
| 68 | + engine = MagicMock() |
| 69 | + engine.connect.return_value = conn |
| 70 | + |
| 71 | + with patch.object(init, "engine", engine): |
| 72 | + await _ensure_source_type_enum() |
| 73 | + |
| 74 | + alter_sql = [c for c in calls if c.startswith("ALTER TYPE")] |
| 75 | + assert alter_sql == [ |
| 76 | + "ALTER TYPE sourcetype ADD VALUE 'PEARSONVUE'", |
| 77 | + "ALTER TYPE sourcetype ADD VALUE 'TRAINING_PROVIDER'", |
| 78 | + ] |
| 79 | + assert "REDDIT" not in alter_sql |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.asyncio |
| 83 | +async def test_ensure_source_type_enum_closes_connection() -> None: |
| 84 | + conn = _conn_mock(scalar_value=True, rows=[]) |
| 85 | + engine = MagicMock() |
| 86 | + engine.connect.return_value = conn |
| 87 | + |
| 88 | + with patch.object(init, "engine", engine): |
| 89 | + await _ensure_source_type_enum() |
| 90 | + |
| 91 | + conn.__aexit__.assert_awaited_once() |
0 commit comments