-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtest_uow.py
More file actions
55 lines (40 loc) · 1.58 KB
/
test_uow.py
File metadata and controls
55 lines (40 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# pylint: disable=no-member
import pytest
from allocation.domain import model
from allocation.service_layer import unit_of_work
from djangoproject.alloc import models as django_models
def insert_batch(ref, sku, qty, eta):
django_models.Batch.objects.create(reference=ref, sku=sku, qty=qty, eta=eta)
def get_allocated_batch_ref(orderid, sku):
return django_models.Allocation.objects.get(
line__orderid=orderid, line__sku=sku
).batch.reference
@pytest.mark.django_db(transaction=True)
def test_uow_can_retrieve_a_batch_and_allocate_to_it():
insert_batch("batch1", "HIPSTER-WORKBENCH", 100, None)
uow = unit_of_work.DjangoUnitOfWork()
with uow:
batch = uow.batches.get(reference="batch1")
line = model.OrderLine("o1", "HIPSTER-WORKBENCH", 10)
batch.allocate(line)
uow.commit()
batchref = get_allocated_batch_ref("o1", "HIPSTER-WORKBENCH")
assert batchref == "batch1"
@pytest.mark.django_db(transaction=True)
def test_rolls_back_uncommitted_work_by_default():
uow = unit_of_work.DjangoUnitOfWork()
with uow:
insert_batch("batch1", "MEDIUM-PLINTH", 100, None)
rows = django_models.Batch.objects.all()
assert list(rows) == []
@pytest.mark.django_db(transaction=True)
def test_rolls_back_on_error():
class MyException(Exception):
pass
uow = unit_of_work.DjangoUnitOfWork()
with pytest.raises(MyException):
with uow:
insert_batch("batch1", "LARGE-FORK", 100, None)
raise MyException()
rows = django_models.Batch.objects.all()
assert list(rows) == []