Exemplo n.º 1
0
    def test_session_is_destroyed_after_commit(self, test_domain):
        uow = UnitOfWork()
        uow.start()

        uow.commit()
        assert uow._sessions == {}
        assert uow.in_progress is False
Exemplo 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
Exemplo 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()
Exemplo n.º 4
0
    def test_changed_objects_are_committed_as_part_of_one_transaction_on_explict_commit(
            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
        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.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"
        with pytest.raises(ObjectNotFoundError):
            person_dao.get(person_to_be_deleted.id)