|
| 1 | +# pylint: disable=protected-access |
| 2 | +import model |
| 3 | +import repository |
| 4 | + |
| 5 | +def test_repository_can_save_a_batch(session): |
| 6 | + batch = model.Batch("batch1", "RUSTY-SOAPDISH", 100, eta=None) |
| 7 | + |
| 8 | + repo = repository.SqlAlchemyRepository(session) |
| 9 | + repo.add(batch) |
| 10 | + session.commit() |
| 11 | + |
| 12 | + rows = list(session.execute( |
| 13 | + 'SELECT reference, sku, _purchased_quantity, eta FROM "batches"' |
| 14 | + )) |
| 15 | + assert rows == [("batch1", "RUSTY-SOAPDISH", 100, None)] |
| 16 | + |
| 17 | + |
| 18 | +def insert_order_line(session): |
| 19 | + session.execute( |
| 20 | + 'INSERT INTO order_lines (orderid, sku, qty)' |
| 21 | + ' VALUES ("order1", "GENERIC-SOFA", 12)' |
| 22 | + ) |
| 23 | + [[orderline_id]] = session.execute( |
| 24 | + 'SELECT id FROM order_lines WHERE orderid=:orderid AND sku=:sku', |
| 25 | + dict(orderid="order1", sku="GENERIC-SOFA") |
| 26 | + ) |
| 27 | + return orderline_id |
| 28 | + |
| 29 | +def insert_batch(session, batch_id): |
| 30 | + session.execute( |
| 31 | + 'INSERT INTO batches (reference, sku, _purchased_quantity, eta)' |
| 32 | + ' VALUES (:batch_id, "GENERIC-SOFA", 100, null)', |
| 33 | + dict(batch_id=batch_id) |
| 34 | + ) |
| 35 | + [[batch_id]] = session.execute( |
| 36 | + 'SELECT id FROM batches WHERE reference=:batch_id AND sku="GENERIC-SOFA"', |
| 37 | + dict(batch_id=batch_id) |
| 38 | + ) |
| 39 | + return batch_id |
| 40 | + |
| 41 | +def insert_allocation(session, orderline_id, batch_id): |
| 42 | + session.execute( |
| 43 | + 'INSERT INTO allocations (orderline_id, batch_id)' |
| 44 | + ' VALUES (:orderline_id, :batch_id)', |
| 45 | + dict(orderline_id=orderline_id, batch_id=batch_id) |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +def test_repository_can_retrieve_a_batch_with_allocations(session): |
| 50 | + orderline_id = insert_order_line(session) |
| 51 | + batch1_id = insert_batch(session, "batch1") |
| 52 | + insert_batch(session, "batch2") |
| 53 | + insert_allocation(session, orderline_id, batch1_id) |
| 54 | + |
| 55 | + repo = repository.SqlAlchemyRepository(session) |
| 56 | + retrieved = repo.get("batch1") |
| 57 | + |
| 58 | + expected = model.Batch("batch1", "GENERIC-SOFA", 100, eta=None) |
| 59 | + assert retrieved == expected # Batch.__eq__ only compares reference |
| 60 | + assert retrieved.sku == expected.sku |
| 61 | + assert retrieved._purchased_quantity == expected._purchased_quantity |
| 62 | + assert retrieved._allocations == { |
| 63 | + model.OrderLine("order1", "GENERIC-SOFA", 12), |
| 64 | + } |
0 commit comments