from types import SimpleNamespace from unittest.mock import AsyncMock from uuid import uuid4 import pytest from sqlalchemy.dialects import postgresql from app.infra.db.metadb.repositories.metadata_repository import MetadataRepository @pytest.fixture def anyio_backend(): return "asyncio" class _ProjectListResult: def __init__(self, rows): self._rows = rows def all(self): return self._rows @pytest.mark.anyio async def test_list_projects_for_user_only_queries_active_projects(): user_id = uuid4() project = SimpleNamespace( id=uuid4(), name="Active Project", code="active-project", description=None, gs_workspace="active-project", map_extent=None, status="active", ) session = SimpleNamespace( execute=AsyncMock(return_value=_ProjectListResult([(project, "member")])) ) projects = await MetadataRepository(session).list_projects_for_user(user_id) statement = session.execute.await_args.args[0] sql = str( statement.compile( dialect=postgresql.dialect(), compile_kwargs={"literal_binds": True}, ) ) assert "user_project_membership.user_id" in sql assert "projects.status = 'active'" in sql assert [item.code for item in projects] == ["active-project"]