def test_uow_can_be_initiated_explicitly(self, test_domain): uow = UnitOfWork() assert uow is not None assert uow.in_progress is False uow.start() assert uow.in_progress is True
def test_tracking_aggregate_on_add(test_domain): uow = UnitOfWork() uow.start() test_domain.repository_for(User).add(User(name="John Doe")) assert len(uow._seen) == 1
def test_that_uow_can_be_started_manually(self, test_domain): uow = UnitOfWork() uow.start() uow.commit() # `commit` should not raise exception uow.start() uow.rollback() # `rollback` should not raise exception
def test_tracking_aggregate_on_filtering(test_domain): test_domain.repository_for(User).add(User(id=12, name="John Doe")) test_domain.repository_for(User).add(User(id=13, name="Jane Doe")) uow = UnitOfWork() uow.start() test_domain.repository_for(User)._dao.query.filter(name__contains="Doe").all() assert len(uow._seen) == 2 assert all(isinstance(item, User) for item in uow._seen)
def test_tracking_aggregate_on_get(test_domain): test_domain.repository_for(User).add(User(id=12, name="John Doe")) uow = UnitOfWork() uow.start() test_domain.repository_for(User).get(12) assert len(uow._seen) == 1 seen_item = next(iter(uow._seen)) assert isinstance(seen_item, User)
def test_tracking_aggregate_on_update(test_domain): test_domain.repository_for(User).add(User(id=12, name="John Doe")) user = test_domain.repository_for(User).get(12) uow = UnitOfWork() uow.start() user.name = "Name Changed" test_domain.repository_for(User).add(user) assert len(uow._seen) == 1 seen_item = next(iter(uow._seen)) assert seen_item.name == "Name Changed"
def test_changed_objects_are_committed_as_part_of_one_transaction(self, test_domain): # Add a Person the database repo = test_domain.repository_for(Person) person_to_be_updated = self.persisted_person(test_domain) person_to_be_deleted = self.persisted_person(test_domain) repo.add(person_to_be_updated) repo.add(person_to_be_deleted) person_dao = test_domain.get_dao(Person) # Initiate a UnitOfWork Session with UnitOfWork(): repo_with_uow = test_domain.repository_for(Person) # Create a new person object to be added person_to_be_added = Person(first_name='John', last_name='Doe') repo_with_uow.add(person_to_be_added) # Update an existing Person record person_to_be_updated.last_name = 'FooBar' repo_with_uow.add(person_to_be_updated) # Remove an existing Person record repo_with_uow.remove(person_to_be_deleted) # Test that the underlying database is untouched assert len(person_dao.query.all().items) == 2 assert person_dao.outside_uow().get(person_to_be_updated.id).last_name != 'FooBar' assert person_dao.get(person_to_be_deleted.id) is not None assert len(person_dao.query.all().items) == 2 assert person_dao.get(person_to_be_added.id) is not None assert person_dao.get(person_to_be_updated.id).last_name == 'FooBar' with pytest.raises(ObjectNotFoundError): person_dao.get(person_to_be_deleted.id)
def add(cls, command: PersonCommand): with UnitOfWork(): newcomer = Person.add_newcomer(command.to_dict()) person_repo = current_domain.repository_for(Person) person_repo.add(newcomer) return newcomer
def test_that_events_are_empty_after_uow(): user = User(name="John Doe", email="*****@*****.**") user.change_name("Jane Doe") user.activate() with UnitOfWork(): user_repo = current_domain.repository_for(User) user_repo.add(user) assert len(user._events) == 0
def test_session_is_destroyed_after_rollback(self, test_domain): uow = UnitOfWork() uow.start() uow.rollback() assert uow._sessions == {} assert uow.in_progress is False
def test_that_uow_throws_exception_on_commit_or_rollback_without_being_started( self, test_domain): uow = UnitOfWork() with pytest.raises(InvalidOperationError): uow.commit() with pytest.raises(InvalidOperationError): uow.rollback()
def test_that_an_entity_can_be_added_within_uow(self, test_domain, persisted_post): repo = test_domain.repository_for(Post) with UnitOfWork(): comment = Comment(content='So La Ti Do') persisted_post.comments.add(comment) repo = test_domain.repository_for(Post) repo.add(persisted_post) # FIXME Refactor `outside_uow` to be a global thread variable # post_dao = test_domain.get_dao(Post) # assert len(post_dao.outside_uow().get(persisted_post.id).comments) == 0 post = repo.get(persisted_post.id) assert len(post.comments) == 1 assert post.comments[0].content == 'So La Ti Do'
def test_deleted_objects_are_committed_as_part_of_one_transaction(self, test_domain): # Add a Person the database repo = test_domain.repository_for(Person) person_to_be_added = self.persisted_person(test_domain) repo.add(person_to_be_added) person_dao = test_domain.get_dao(Person) # Initiate a UnitOfWork Session with UnitOfWork(): repo = test_domain.repository_for(Person) persisted_person = repo.get(person_to_be_added.id) repo.remove(persisted_person) # Test that the underlying database is untouched assert len(person_dao.outside_uow().query.all().items) == 1 assert len(person_dao.query.all().items) == 0
def test_new_objects_are_committed_as_part_of_one_transaction(self, test_domain): # Add a Person the database repo = test_domain.repository_for(Person) repo.add(self.persisted_person(test_domain)) person_dao = test_domain.get_dao(Person) # Initiate a UnitOfWork Session with UnitOfWork(): repo = test_domain.repository_for(Person) person2 = Person(first_name='Jane', last_name='Doe') repo.add(person2) # Test that the underlying database is untouched assert len(person_dao.outside_uow().query.all().items) == 1 assert len(person_dao.query.all().items) == 2
def test_that_an_entity_can_be_removed_within_uow(self, test_domain, persisted_post): comment = Comment(content='So La Ti Do') persisted_post.comments.add(comment) repo = test_domain.repository_for(Post) repo.add(persisted_post) with UnitOfWork(): comment = persisted_post.comments[0] persisted_post.comments.remove(comment) repo = test_domain.repository_for(Post) repo.add(persisted_post) # FIXME Refactor `outside_uow` to be a global thread variable # assert comment.id in uow.changes_to_be_committed['default']['REMOVED'] post = repo.get(persisted_post.id) assert len(post.comments) == 0
def test_updated_objects_are_committed_as_part_of_one_transaction(self, test_domain): # Add a Person the database repo = test_domain.repository_for(Person) person = Person(first_name='John', last_name='Doe') repo.add(person) person_dao = test_domain.get_dao(Person) # Initiate a UnitOfWork Session with UnitOfWork(): repo = test_domain.repository_for(Person) persisted_person = repo.get(person.id) persisted_person.last_name = 'Dane' repo.add(persisted_person) # Test that the underlying database is untouched assert person_dao.outside_uow().find_by(id=person.id).last_name == 'Doe' assert person_dao.get(person.id).last_name == 'Dane'
def test_that_an_entity_can_be_updated_within_uow(self, test_domain, persisted_post): comment = Comment(content='So La Ti Do') persisted_post.comments.add(comment) repo = test_domain.repository_for(Post) repo.add(persisted_post) post = repo.get(persisted_post.id) assert post.comments[0].content == 'So La Ti Do' with UnitOfWork(): comment = persisted_post.comments[0] comment.content = ('Pa Da Ni Sa') persisted_post.comments.add(comment) repo = test_domain.repository_for(Post) repo.add(persisted_post) # FIXME Refactor `outside_uow` to be a global thread variable # assert comment.id in uow.changes_to_be_committed['default']['UPDATED'] post = repo.get(persisted_post.id) assert post.comments[0].content == 'Pa Da Ni Sa'
def test_all_changes_are_discard_on_rollback(self, test_domain): repo = test_domain.repository_for(Person) person_to_be_updated = self.persisted_person(test_domain) person_to_be_deleted = self.persisted_person(test_domain) repo.add(person_to_be_updated) repo.add(person_to_be_deleted) person_dao = test_domain.get_dao(Person) # Initiate a UnitOfWork Session uow = UnitOfWork() uow.start() repo_with_uow = test_domain.repository_for(Person) # Create a new person object to be added person_to_be_added = Person(first_name="John", last_name="Doe") repo_with_uow.add(person_to_be_added) # Update an existing Person record person_to_be_updated.last_name = "FooBar" repo_with_uow.add(person_to_be_updated) # Remove an existing Person record repo_with_uow.remove(person_to_be_deleted) # Test that the underlying database is untouched assert len(person_dao.outside_uow().query.all().items) == 2 assert (person_dao.outside_uow().get(person_to_be_updated.id).last_name != "FooBar") assert person_dao.outside_uow().get( person_to_be_deleted.id) is not None uow.rollback() assert uow.in_progress is False assert len(person_dao.query.all().items) == 2 assert person_dao.get(person_to_be_updated.id).last_name != "FooBar" assert person_dao.get(person_to_be_deleted.id) is not None
def wrapper(instance, target_obj): # Wrap function call within a UoW with UnitOfWork(): fn(instance, target_obj)
def test_uow_can_be_initiated_with_context_manager(self, test_domain): with UnitOfWork() as uow: assert uow is not None assert uow.in_progress is True