def test_repository_can_retrieve_an_entity_if_no_model_defined( self, repo: Repository, inserted_entity: Entity, ) -> None: """Given an entity_id the repository returns the entity object.""" repo.models = [type(inserted_entity)] # type: ignore with pytest.warns(UserWarning, match="In 2022-06-10.*deprecated"): result: Entity = repo.get(inserted_entity.id_) assert result == inserted_entity assert result.id_ == inserted_entity.id_
def test_repository_can_search_by_property_specifying_a_list_of_types( self, repo: Repository, inserted_entities: List[Entity], ) -> None: """Search should return the objects that match the desired property.""" entity_types: List[Type[Entity]] = [type(inserted_entities[0]), OtherEntity] expected_entity = inserted_entities[1] repo.models = entity_types # type: ignore with pytest.warns(UserWarning, match="In 2022-06-10.*deprecated"): result = repo.search({"id_": expected_entity.id_}, entity_types) assert result == [expected_entity]
def test_repository_can_retrieve_an_entity_if_list_of_models_defined( self, repo: Repository, inserted_entity: Entity, ) -> None: """Given an entity_id the repository returns the entity object.""" entity_models: List[Type[Entity]] = [type(inserted_entity), OtherEntity] repo.models = entity_models # type: ignore with pytest.warns(UserWarning, match="In 2022-06-10.*deprecated"): result = repo.get(inserted_entity.id_, entity_models) assert result == inserted_entity assert result.id_ == inserted_entity.id_
def test_repository_raises_error_if_get_finds_more_than_one_entity( self, repo: Repository, inserted_entity: Entity ) -> None: """ Given: Two entities of different type with the same ID When: We get the ID without specifying the model Then: a TooManyEntitiesError error is raised """ other_entity = OtherEntity(id_=inserted_entity.id_, name="Other entity") repo.models = [type(inserted_entity), OtherEntity] # type: ignore repo.add(other_entity) repo.commit() with pytest.warns( UserWarning, match="In 2022-06-10.*deprecated" ), pytest.raises(TooManyEntitiesError, match=""): repo.get(inserted_entity.id_) # act
def test_repository_last_returns_last_entity_if_list_of_types( self, repo: Repository, inserted_entities: List[Entity], ) -> None: """ Given: A repository with many entities. When: using the last method with a list of types. Then: The greater entity is returned """ greater_entity = max(inserted_entities) entity_types: List[Type[Entity]] = [type(inserted_entities[0]), OtherEntity] repo.models = entity_types # type: ignore with pytest.warns(UserWarning, match="In 2022-06-10.*deprecated"): result = repo.last(entity_types) assert result == greater_entity
def test_repository_can_retrieve_all_objects_of_a_list_of_entity_types( self, repo: Repository, inserted_entities: List[Entity], ) -> None: """ Given: Three entities of a type and another of other type. When: all is called using a list of entities Then: all elements are returned ordered by ID, we need it so that we know for sure that the results are always ordered. """ other_entity = OtherEntity(id_=0, name="Other entity") repo.add(other_entity) repo.commit() entity_types: List[Type[Entity]] = [type(inserted_entities[0]), OtherEntity] repo.models = entity_types # type: ignore with pytest.warns(UserWarning, match="In 2022-06-10.*deprecated"): result = repo.all(entity_types) assert result == [other_entity] + inserted_entities # type: ignore assert len(result) == 4
def test_repository_can_retrieve_all( self, repo: Repository, inserted_entities: List[Entity], ) -> None: """ Given: A repository with inserted entities When: all is called Then: all entities are returned and saved to the repo cache. The defined_values of all entities are empty, otherwise the merge fails. """ entity_types: List[Type[Entity]] = [type(inserted_entities[0]), OtherEntity] repo.models = entity_types # type: ignore with pytest.warns(UserWarning, match="In 2022-06-10.*deprecated"): result: List[Entity] = repo.all() assert result == inserted_entities assert len(result) == 3 assert result[0] < result[1] assert result[1] < result[2] for entity in result: assert repo.cache.get(entity) == entity assert entity.defined_values == {}