Skip to content

Commit 2156971

Browse files
committed
two more tests for rollback behaviour [chapter_05_uow_ends]
1 parent 650d8e4 commit 2156971

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

‎tests/integration/test_uow.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,28 @@ def test_uow_can_retrieve_a_batch_and_allocate_to_it(session_factory):
3636

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

0 commit comments

Comments
 (0)