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_all_changes_are_discarded_on_rollback(self, test_domain): repo = test_domain.repository_for(Person) person_to_be_updated = self.persisted_person(test_domain) repo.add(person_to_be_updated) person_dao = test_domain.repository_for(Person)._dao # 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) # Test that the underlying database is untouched assert len(person_dao.outside_uow().query.all().items) == 1 assert (person_dao.outside_uow().get(person_to_be_updated.id).last_name != "FooBar") uow.rollback() assert uow.in_progress is False assert len(person_dao.query.all().items) == 1 assert person_dao.get(person_to_be_updated.id).last_name != "FooBar"
def test_changed_objects_are_committed_as_part_of_one_transaction_on_explicit_commit( self, test_domain): # Add a Person the database repo = test_domain.repository_for(Person) person_to_be_updated = self.persisted_person(test_domain) repo.add(person_to_be_updated) person_dao = test_domain.repository_for(Person)._dao # 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) # Test that the underlying database is untouched assert len(person_dao.outside_uow().query.all().items) == 1 assert (person_dao.outside_uow().get(person_to_be_updated.id).last_name != "FooBar") uow.commit() assert uow.in_progress is False 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"
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_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