"""Unit tests for TableRepositoryInterface.""" from typing import Any import pytest from python_repositories.interfaces.table_repository_interface import ( TableRepositoryInterface, ) class InMemoryTableRepo: """Plain class that satisfies TableRepositoryInterface without inheritance.""" def fetch_one(self, pk: Any) -> dict[str, Any] | None: return None def fetch_all(self, *, limit: int | None = None) -> list[dict[str, Any]]: del limit return [] def upsert(self, row: dict[str, Any]) -> None: pass def delete(self, pk: Any) -> None: pass def execute( self, sql: str, params: tuple[Any, ...] = (), ) -> list[dict[str, Any]]: del sql, params return [] def accepts_table_repo(repo: TableRepositoryInterface) -> None: """Type-checking hook for TableRepositoryInterface structural subtyping.""" repo.fetch_one("pk") def test_instantiation_fails_when_fetch_one_not_implemented() -> None: """Test that instantiation fails if fetch_one is not implemented.""" class Incomplete(TableRepositoryInterface): def fetch_all(self, *, limit: int | None = None) -> list[dict[str, Any]]: return [] def upsert(self, row: dict[str, Any]) -> None: pass def delete(self, pk: Any) -> None: pass def execute( self, sql: str, params: tuple[Any, ...] = (), ) -> list[dict[str, Any]]: return [] with pytest.raises(TypeError): _ = Incomplete() # type: ignore def test_instantiation_fails_when_fetch_all_not_implemented() -> None: """Test that instantiation fails if fetch_all is not implemented.""" class Incomplete(TableRepositoryInterface): def fetch_one(self, pk: Any) -> dict[str, Any] | None: return None def upsert(self, row: dict[str, Any]) -> None: pass def delete(self, pk: Any) -> None: pass def execute( self, sql: str, params: tuple[Any, ...] = (), ) -> list[dict[str, Any]]: return [] with pytest.raises(TypeError): _ = Incomplete() # type: ignore def test_instantiation_fails_when_upsert_not_implemented() -> None: """Test that instantiation fails if upsert is not implemented.""" class Incomplete(TableRepositoryInterface): def fetch_one(self, pk: Any) -> dict[str, Any] | None: return None def fetch_all(self, *, limit: int | None = None) -> list[dict[str, Any]]: return [] def delete(self, pk: Any) -> None: pass def execute( self, sql: str, params: tuple[Any, ...] = (), ) -> list[dict[str, Any]]: return [] with pytest.raises(TypeError): _ = Incomplete() # type: ignore def test_instantiation_fails_when_delete_not_implemented() -> None: """Test that instantiation fails if delete is not implemented.""" class Incomplete(TableRepositoryInterface): def fetch_one(self, pk: Any) -> dict[str, Any] | None: return None def fetch_all(self, *, limit: int | None = None) -> list[dict[str, Any]]: return [] def upsert(self, row: dict[str, Any]) -> None: pass def execute( self, sql: str, params: tuple[Any, ...] = (), ) -> list[dict[str, Any]]: return [] with pytest.raises(TypeError): _ = Incomplete() # type: ignore def test_instantiation_fails_when_execute_not_implemented() -> None: """Test that instantiation fails if execute is not implemented.""" class Incomplete(TableRepositoryInterface): def fetch_one(self, pk: Any) -> dict[str, Any] | None: return None def fetch_all(self, *, limit: int | None = None) -> list[dict[str, Any]]: return [] def upsert(self, row: dict[str, Any]) -> None: pass def delete(self, pk: Any) -> None: pass with pytest.raises(TypeError): _ = Incomplete() # type: ignore def test_structural_subtyping() -> None: """Test that a plain class satisfies TableRepositoryInterface structurally.""" repo: TableRepositoryInterface = InMemoryTableRepo() accepts_table_repo(repo) assert isinstance(repo, TableRepositoryInterface)