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