From dddf1e2e52defbb7632368c8442417fa26076e74 Mon Sep 17 00:00:00 2001 From: Peter Schutt Date: Thu, 1 Dec 2022 08:12:22 +1000 Subject: [PATCH] fix: Fix for usage of mock repo. --- src/starlite_saqlalchemy/testing.py | 14 +++++++------- tests/unit/conftest.py | 19 ++++--------------- tests/unit/test_testing.py | 15 ++++++--------- 3 files changed, 17 insertions(+), 31 deletions(-) diff --git a/src/starlite_saqlalchemy/testing.py b/src/starlite_saqlalchemy/testing.py index 5d32352e..cf13c715 100644 --- a/src/starlite_saqlalchemy/testing.py +++ b/src/starlite_saqlalchemy/testing.py @@ -27,12 +27,12 @@ class GenericMockRepository(AbstractRepository[ModelT], Generic[ModelT]): Uses a `dict` for storage. """ - _collections: MutableMapping[type[ModelT], MutableMapping[Hashable, ModelT]] = {} + collection: MutableMapping[Hashable, ModelT] = {} + model_type: type[ModelT] def __init__(self, id_factory: Callable[[], Any] = uuid4, **_: Any) -> None: super().__init__() self._id_factory = id_factory - self.collection = self._collections[self.model_type] @classmethod def __class_getitem__(cls: type[MockRepoT], item: type[ModelT]) -> type[MockRepoT]: @@ -41,8 +41,9 @@ def __class_getitem__(cls: type[MockRepoT], item: type[ModelT]) -> type[MockRepo Args: item: The type that the class has been parametrized with. """ - cls._collections.setdefault(item, {}) - return cls + return type( # pyright:ignore + f"{cls.__name__}[{item.__name__}]", (cls,), {"collection": {}, "model_type": item} + ) def _find_or_raise_not_found(self, id_: Any) -> ModelT: return self.check_not_found(self.collection.get(id_)) @@ -161,11 +162,10 @@ def seed_collection(cls, instances: Iterable[ModelT]) -> None: Args: instances: the instances to be added to the collection. """ - collection = cls._collections[cls.model_type] for instance in instances: - collection[cls.get_id_attribute_value(instance)] = instance + cls.collection[cls.get_id_attribute_value(instance)] = instance @classmethod def clear_collection(cls) -> None: """Empty the collection for repository type.""" - cls._collections[cls.model_type] = {} + cls.collection = {} diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index f953fb43..47acb1e8 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -47,27 +47,16 @@ def _patch_worker() -> abc.Iterator: monkeypatch.undo() -@pytest.fixture(autouse=True) -def _clear_mock_repo_collections() -> None: - """Ensure all tests start with fresh collections.""" - # pylint: disable=protected-access - GenericMockRepository._collections = {} # type:ignore[misc] - - @pytest.fixture(name="author_repository_type") def fx_author_repository_type( authors: list[Author], monkeypatch: pytest.MonkeyPatch ) -> type[GenericMockRepository[Author]]: """Mock Author repository, pre-seeded with collection data.""" - class AuthorRepository(GenericMockRepository[Author]): - """Mock Author repo.""" - - model_type = Author - - AuthorRepository.seed_collection(authors) - monkeypatch.setattr(AuthorService, "repository_type", AuthorRepository) - return AuthorRepository + repo = GenericMockRepository[Author] + repo.seed_collection(authors) + monkeypatch.setattr(AuthorService, "repository_type", repo) + return repo @pytest.fixture(name="author_repository") diff --git a/tests/unit/test_testing.py b/tests/unit/test_testing.py index c574c4db..104c5fac 100644 --- a/tests/unit/test_testing.py +++ b/tests/unit/test_testing.py @@ -19,11 +19,10 @@ async def test_repo_raises_conflict_if_add_with_id( def test_generic_mock_repository_parametrization() -> None: """Test that the mock repository handles multiple types.""" - # pylint: disable=pointless-statement,protected-access - testing.GenericMockRepository[Author] - testing.GenericMockRepository[Book] - assert Author in testing.GenericMockRepository._collections # type:ignore[misc] - assert Book in testing.GenericMockRepository._collections # type:ignore[misc] + author_repo = testing.GenericMockRepository[Author] + book_repo = testing.GenericMockRepository[Book] + assert author_repo.model_type is Author # type:ignore[misc] + assert book_repo.model_type is Book # type:ignore[misc] def test_generic_mock_repository_seed_collection( @@ -31,8 +30,7 @@ def test_generic_mock_repository_seed_collection( ) -> None: """Test seeding instances.""" author_repository_type.seed_collection([Author(id="abc")]) - # pylint: disable=protected-access - assert "abc" in author_repository_type._collections[Author] + assert "abc" in author_repository_type.collection def test_generic_mock_repository_clear_collection( @@ -40,5 +38,4 @@ def test_generic_mock_repository_clear_collection( ) -> None: """Test clearing collection for type.""" author_repository_type.clear_collection() - # pylint: disable=protected-access - assert not author_repository_type._collections[Author] + assert not author_repository_type.collection