def test_repository_last_raise_error_if_entity_not_found( self, repo: Repository, entity: Entity, ) -> None: """ Given: an empty repository. When: trying to get the last entity. Then: An EntityNotFoundError error is raised. """ with pytest.raises( EntityNotFoundError, match=( f"There are no entities of type {entity.model_name} in the repository" ), ): repo.last(type(entity))
def test_repository_last_raise_error_if_entity_not_found_and_staged_entities( self, repo: Repository, ) -> None: """ Given: A repository with a staged entity When: trying to get an entity of another model that is not present Then: the staged entity shouldn't affect and the error should be raised """ book = BookFactory.build(id_=5) repo.add(book) with pytest.raises( EntityNotFoundError, match=("There are no entities of type Genre in the repository"), ): repo.last(Genre)
def test_repository_last_returns_last_entity( self, repo: Repository, inserted_entities: List[Entity], ) -> None: """ Given: A repository with many entities. When: using the last method. Then: The greater entity is returned and it's also stored into the cache. """ greater_entity = max(inserted_entities) result = repo.last(type(greater_entity)) assert result == greater_entity assert repo.cache.get(greater_entity) == greater_entity
def test_repository_cant_save_an_entity_with_a_negative_id( self, repo: Repository, inserted_int_entity: Entity, merge: bool ) -> None: """ Given: A repository with an entity When: adding an entity with a negative id Then: the id of the new entity is one unit greater than the last one. """ entity = inserted_int_entity.__class__(id=-3, name="Entity with negative id") repo.add(entity, merge=merge) repo.commit() # act saved_entity = repo.last(type(inserted_int_entity)) # ignore: we know for sure that the id_ is an int assert saved_entity.id_ == inserted_int_entity.id_ + 1 # type: ignore assert saved_entity.name == "Entity with negative id"
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_last_if_some_entity_found_and_staged_entities( self, repo: Repository, ) -> None: """ Given: A repository with a commited entity of the desired type and a staged entity of another type with a greater ID When: get the last entity type Then: the staged entity shouldn't affect and the commited entity should be returned """ genre = GenreFactory.build(id_=2) repo.add(genre) repo.commit() book = BookFactory.build(id_=5) repo.add(book) result = repo.last(Genre) assert result == genre
def test_repository_can_save_an_entity_without_id( self, repo: Repository, inserted_int_entity: Entity, merge: bool, ) -> None: """ Given: A repository with an entity whose id_ type is an int When: adding an entity without id Then: the id of the new entity is one unit greater than the last one. """ entity = inserted_int_entity.__class__(name="Entity without id") repo.add(entity, merge=merge) repo.commit() # act saved_entity = repo.last(type(inserted_int_entity)) # ignore: we know that the entities have an int id_ assert saved_entity.id_ == inserted_int_entity.id_ + 1 # type: ignore assert saved_entity.name == "Entity without id"
def test_repository_can_save_an_entity_without_id_with_other_entity_in_repo( self, repo: Repository, merge: bool, ) -> None: """ Given: A repository with an entity whose id_ type is an int When: adding an entity of another model without id Then: the id of the new entity is not affected by the existent entity. """ book = BookFactory.build(id_=5) repo.add(book, merge=merge) repo.commit() entity = GenreFactory.build(id_=-1, name="Entity without id") repo.add(entity, merge=merge) repo.commit() # act saved_entity = repo.last(Genre) assert saved_entity.id_ == 0 assert saved_entity.name == "Entity without id"