|
| 1 | +import pytest |
| 2 | +from allocation import model |
| 3 | +from allocation import unit_of_work |
| 4 | + |
| 5 | +def insert_batch(session, ref, sku, qty, eta): |
| 6 | + session.execute( |
| 7 | + 'INSERT INTO batches (reference, sku, _purchased_quantity, eta)' |
| 8 | + ' VALUES (:ref, :sku, :qty, :eta)', |
| 9 | + dict(ref=ref, sku=sku, qty=qty, eta=eta) |
| 10 | + ) |
| 11 | + |
| 12 | +def get_allocated_batch_ref(session, orderid, sku): |
| 13 | + [[orderlineid]] = session.execute( |
| 14 | + 'SELECT id FROM order_lines WHERE orderid=:orderid AND sku=:sku', |
| 15 | + dict(orderid=orderid, sku=sku) |
| 16 | + ) |
| 17 | + [[batchref]] = session.execute( |
| 18 | + 'SELECT b.reference FROM allocations JOIN batches AS b ON batch_id = b.id' |
| 19 | + ' WHERE orderline_id=:orderlineid', |
| 20 | + dict(orderlineid=orderlineid) |
| 21 | + ) |
| 22 | + return batchref |
| 23 | + |
| 24 | + |
| 25 | +def test_uow_can_retrieve_a_batch_and_allocate_to_it(session_factory): |
| 26 | + session = session_factory() |
| 27 | + insert_batch(session, 'batch1', 'HIPSTER-WORKBENCH', 100, None) |
| 28 | + session.commit() |
| 29 | + |
| 30 | + uow = unit_of_work.SqlAlchemyUnitOfWork(session_factory) |
| 31 | + with uow: |
| 32 | + batch = uow.batches.get(reference='batch1') |
| 33 | + line = model.OrderLine('o1', 'HIPSTER-WORKBENCH', 10) |
| 34 | + batch.allocate(line) |
| 35 | + uow.commit() |
| 36 | + |
| 37 | + batchref = get_allocated_batch_ref(session, 'o1', 'HIPSTER-WORKBENCH') |
| 38 | + assert batchref == 'batch1' |
0 commit comments