|
3 | 3 | from allocation.service_layer import unit_of_work |
4 | 4 |
|
5 | 5 |
|
6 | | -def insert_batch(session, ref, sku, qty, eta): |
7 | | - session.execute( |
8 | | - "INSERT INTO batches (reference, sku, _purchased_quantity, eta)" |
9 | | - " VALUES (:ref, :sku, :qty, :eta)", |
10 | | - dict(ref=ref, sku=sku, qty=qty, eta=eta), |
11 | | - ) |
| 6 | +def insert_batch(django_models, ref, sku, qty, eta): |
| 7 | + django_models.Batch.objects.create(reference=ref, sku=sku, qty=qty, eta=eta) |
12 | 8 |
|
13 | 9 |
|
14 | | -def get_allocated_batch_ref(session, orderid, sku): |
15 | | - [[orderlineid]] = session.execute( |
16 | | - "SELECT id FROM order_lines WHERE orderid=:orderid AND sku=:sku", |
17 | | - dict(orderid=orderid, sku=sku), |
18 | | - ) |
19 | | - [[batchref]] = session.execute( |
20 | | - "SELECT b.reference FROM allocations JOIN batches AS b ON batch_id = b.id" |
21 | | - " WHERE orderline_id=:orderlineid", |
22 | | - dict(orderlineid=orderlineid), |
23 | | - ) |
24 | | - return batchref |
| 10 | +def get_allocated_batch_ref(django_models, orderid, sku): |
| 11 | + print(django_models.Allocation.objects.all()) |
| 12 | + return django_models.Allocation.objects.get( |
| 13 | + line__orderid=orderid, line__sku=sku |
| 14 | + ).batch.reference |
25 | 15 |
|
26 | 16 |
|
27 | | -def test_uow_can_retrieve_a_batch_and_allocate_to_it(session_factory): |
28 | | - session = session_factory() |
29 | | - insert_batch(session, "batch1", "HIPSTER-WORKBENCH", 100, None) |
30 | | - session.commit() |
| 17 | +@pytest.mark.django_db(transaction=True) |
| 18 | +def test_uow_can_retrieve_a_batch_and_allocate_to_it(django_models): |
| 19 | + insert_batch(django_models, "batch1", "HIPSTER-WORKBENCH", 100, None) |
31 | 20 |
|
32 | | - uow = unit_of_work.SqlAlchemyUnitOfWork(session_factory) |
| 21 | + uow = unit_of_work.DjangoUnitOfWork() |
33 | 22 | with uow: |
34 | 23 | batch = uow.batches.get(reference="batch1") |
35 | 24 | line = model.OrderLine("o1", "HIPSTER-WORKBENCH", 10) |
36 | 25 | batch.allocate(line) |
37 | 26 | uow.commit() |
38 | 27 |
|
39 | | - batchref = get_allocated_batch_ref(session, "o1", "HIPSTER-WORKBENCH") |
| 28 | + batchref = get_allocated_batch_ref(django_models, "o1", "HIPSTER-WORKBENCH") |
40 | 29 | assert batchref == "batch1" |
41 | 30 |
|
42 | 31 |
|
43 | | -def test_rolls_back_uncommitted_work_by_default(session_factory): |
44 | | - uow = unit_of_work.SqlAlchemyUnitOfWork(session_factory) |
| 32 | +@pytest.mark.django_db(transaction=True) |
| 33 | +def test_rolls_back_uncommitted_work_by_default(django_models): |
| 34 | + uow = unit_of_work.DjangoUnitOfWork() |
45 | 35 | with uow: |
46 | | - insert_batch(uow.session, "batch1", "MEDIUM-PLINTH", 100, None) |
| 36 | + insert_batch(django_models, "batch1", "MEDIUM-PLINTH", 100, None) |
47 | 37 |
|
48 | | - new_session = session_factory() |
49 | | - rows = list(new_session.execute('SELECT * FROM "batches"')) |
50 | | - assert rows == [] |
| 38 | + rows = django_models.Batch.objects.all() |
| 39 | + assert list(rows) == [] |
51 | 40 |
|
52 | 41 |
|
53 | | -def test_rolls_back_on_error(session_factory): |
| 42 | +@pytest.mark.django_db(transaction=True) |
| 43 | +def test_rolls_back_on_error(django_models): |
54 | 44 | class MyException(Exception): |
55 | 45 | pass |
56 | 46 |
|
57 | | - uow = unit_of_work.SqlAlchemyUnitOfWork(session_factory) |
| 47 | + uow = unit_of_work.DjangoUnitOfWork() |
58 | 48 | with pytest.raises(MyException): |
59 | 49 | with uow: |
60 | | - insert_batch(uow.session, "batch1", "LARGE-FORK", 100, None) |
| 50 | + insert_batch(django_models, "batch1", "LARGE-FORK", 100, None) |
61 | 51 | raise MyException() |
62 | 52 |
|
63 | | - new_session = session_factory() |
64 | | - rows = list(new_session.execute('SELECT * FROM "batches"')) |
65 | | - assert rows == [] |
| 53 | + rows = django_models.Batch.objects.all() |
| 54 | + assert list(rows) == [] |
0 commit comments