Skip to content

Commit 469958e

Browse files
committed
two more tests for rollback behaviour [chapter_05_uow_ends]
1 parent c2aab09 commit 469958e

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

tests/integration/test_uow.py

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

5+
46
def insert_batch(session, ref, sku, qty, eta):
57
session.execute(
68
'INSERT INTO batches (reference, sku, _purchased_quantity, eta)'
79
' VALUES (:ref, :sku, :qty, :eta)',
810
dict(ref=ref, sku=sku, qty=qty, eta=eta)
911
)
1012

13+
1114
def get_allocated_batch_ref(session, orderid, sku):
1215
[[orderlineid]] = session.execute(
1316
'SELECT id FROM order_lines WHERE orderid=:orderid AND sku=:sku',
@@ -35,3 +38,28 @@ def test_uow_can_retrieve_a_batch_and_allocate_to_it(session_factory):
3538

3639
batchref = get_allocated_batch_ref(session, 'o1', 'HIPSTER-WORKBENCH')
3740
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)