Ejemplo n.º 1
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
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
    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()
Ejemplo n.º 4
0
    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