|
| 1 | +import pytest |
| 2 | +from allocation import model |
| 3 | +from allocation import unit_of_work |
| 4 | + |
| 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 | + ) |
| 12 | + |
| 13 | + |
| 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 |
| 25 | + |
| 26 | + |
| 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() |
| 31 | + |
| 32 | + uow = unit_of_work.SqlAlchemyUnitOfWork(session_factory) |
| 33 | + with uow: |
| 34 | + batch = uow.batches.get(reference="batch1") |
| 35 | + line = model.OrderLine("o1", "HIPSTER-WORKBENCH", 10) |
| 36 | + batch.allocate(line) |
| 37 | + uow.commit() |
| 38 | + |
| 39 | + batchref = get_allocated_batch_ref(session, "o1", "HIPSTER-WORKBENCH") |
| 40 | + assert batchref == "batch1" |
0 commit comments