def test_cant_merge_if_id_is_not_equal(self) -> None: """ Given: Two entities with different ids When: Trying to merge Then: An error is raised """ original = BookFactory.build() other = BookFactory.build() with pytest.raises(ValueError, match="Can't merge two Books with different id"): original.merge(other)
def test_merge_does_overwrite_defined_values(self) -> None: """ Given: Two mergeable entities When: Merging an entity with other that has user defined values when initializing the object. Then: The manually defined attributes are updated. The factory manually sets all values. """ original = BookFactory.build() other = BookFactory.build(id_=original.id_) original.merge(other) # act assert original.dict() == other.dict()
def test_cant_merge_if_not_same_model(self) -> None: """ Given: Two entities with different models When: Trying to merge Then: An error is raised """ original = BookFactory.build() other = GenreFactory.build() with pytest.raises(ValueError, match="Can't merge objects of different models"): original.merge(other)
def test_merge_overwrites_manually_updated_value(self) -> None: """ Given: Two mergeable entities When: Merging an entity with another where the user has updated an attribute value after object creation. Then: The manually updated attributes are propagated """ original = BookFactory.build() other = original.copy() other.name = "New name" original.merge(other) # act assert original.name == "New name"
def test_merge_doesnt_overwrite_default_values(self) -> None: """ Given: Two mergeable entities When: Trying to merge an entity with another that has attributes not set by the user and therefore have the default values defined in the class. Then: The default values are not propagated to the original entity. """ original = BookFactory.build() original_copy = original.copy() other = Book(id_=original.id_, name=original.name) original.merge(other) # act assert original.dict() == original_copy.dict()
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_empty_removes_all_entities(repo: Repository) -> None: """ Given: A full repository When: calling empty Then: No entity remains in the repository """ books = BookFactory.batch(25) genres = GenreFactory.batch(20) repo.add(books) repo.add(genres) repo.commit() result = repo.empty() assert result is None assert repo.all(Book) == [] assert repo.all(Genre) == []
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_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"
def get_books() -> List[Book]: """Return some books.""" return BookFactory.batch(5)