Skip to content

Commit

Permalink
two more tests for rollback behaviour [chapter_06_uow_ends]
Browse files Browse the repository at this point in the history
  • Loading branch information
hjwp committed Feb 24, 2021
1 parent 658e61a commit 7526014
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/integration/test_uow.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
from allocation.domain import model
from allocation.service_layer import unit_of_work

Expand Down Expand Up @@ -37,3 +38,28 @@ def test_uow_can_retrieve_a_batch_and_allocate_to_it(session_factory):

batchref = get_allocated_batch_ref(session, "o1", "HIPSTER-WORKBENCH")
assert batchref == "batch1"


def test_rolls_back_uncommitted_work_by_default(session_factory):
uow = unit_of_work.SqlAlchemyUnitOfWork(session_factory)
with uow:
insert_batch(uow.session, "batch1", "MEDIUM-PLINTH", 100, None)

new_session = session_factory()
rows = list(new_session.execute('SELECT * FROM "batches"'))
assert rows == []


def test_rolls_back_on_error(session_factory):
class MyException(Exception):
pass

uow = unit_of_work.SqlAlchemyUnitOfWork(session_factory)
with pytest.raises(MyException):
with uow:
insert_batch(uow.session, "batch1", "LARGE-FORK", 100, None)
raise MyException()

new_session = session_factory()
rows = list(new_session.execute('SELECT * FROM "batches"'))
assert rows == []

0 comments on commit 7526014

Please sign in to comment.