import pytest

from src.repositories import approval_repository as repo_mod


class _FakeScalars:
    def all(self) -> list:
        return []


class _FakeResult:
    def scalars(self) -> _FakeScalars:
        return _FakeScalars()


class _FakeDb:
    def __init__(self) -> None:
        self.statements = []

    async def execute(self, statement):
        self.statements.append(statement)
        return _FakeResult()


class _FakeDbContext:
    def __init__(self, db: _FakeDb) -> None:
        self.db = db

    async def __aenter__(self) -> _FakeDb:
        return self.db

    async def __aexit__(self, exc_type, exc, tb) -> bool:
        return False


@pytest.mark.asyncio
async def test_repository_get_pending_expires_old_pending_before_select(monkeypatch) -> None:
    fake_db = _FakeDb()
    monkeypatch.setattr(repo_mod, "get_db_context", lambda: _FakeDbContext(fake_db))

    pending = await repo_mod.ApprovalDBRepository().get_pending()

    assert pending == []
    assert len(fake_db.statements) == 2
    assert fake_db.statements[0].__class__.__name__ == "Update"
    assert fake_db.statements[1].__class__.__name__ == "Select"
