Skip to content

Commit 7526014

Browse files
committed
two more tests for rollback behaviour [chapter_06_uow_ends]
1 parent 658e61a commit 7526014

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

tests/integration/test_uow.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
from allocation.domain import model
23
from allocation.service_layer import unit_of_work
34

@@ -37,3 +38,28 @@ def test_uow_can_retrieve_a_batch_and_allocate_to_it(session_factory):
3738

3839
batchref = get_allocated_batch_ref(session, "o1", "HIPSTER-WORKBENCH")
3940
assert batchref == "batch1"
41+
42+
43+
def test_rolls_back_uncommitted_work_by_default(session_factory):
44+
uow = unit_of_work.SqlAlchemyUnitOfWork(session_factory)
45+
with uow:
46+
insert_batch(uow.session, "batch1", "MEDIUM-PLINTH", 100, None)
47+
48+
new_session = session_factory()
49+
rows = list(new_session.execute('SELECT * FROM "batches"'))
50+
assert rows == []
51+
52+
53+
def test_rolls_back_on_error(session_factory):
54+
class MyException(Exception):
55+
pass
56+
57+
uow = unit_of_work.SqlAlchemyUnitOfWork(session_factory)
58+
with pytest.raises(MyException):
59+
with uow:
60+
insert_batch(uow.session, "batch1", "LARGE-FORK", 100, None)
61+
raise MyException()
62+
63+
new_session = session_factory()
64+
rows = list(new_session.execute('SELECT * FROM "batches"'))
65+
assert rows == []

0 commit comments

Comments
 (0)