|
1 | 1 | # pylint: disable=protected-access |
2 | 2 | from allocation.domain import model |
3 | 3 | from allocation.adapters import repository |
| 4 | +from allocation import django_model |
4 | 5 |
|
5 | 6 |
|
6 | 7 | def test_repository_can_save_a_batch(session): |
7 | | - batch = model.Batch("batch1", "RUSTY-SOAPDISH", 100, eta=None) |
| 8 | + batch = model.Batch("batch1", "RUSTY-SOAPDISH", 100, eta=date(2011, 12, 25)) |
8 | 9 |
|
9 | 10 | repo = repository.SqlAlchemyRepository(session) |
10 | 11 | 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 | 12 |
|
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 | | - ) |
| 13 | + [saved_batch] = django_model.Batch.objects.all() |
| 14 | + assert saved_batch.reference == batch.reference |
| 15 | + assert saved_batch.sku == batch.sku |
| 16 | + assert saved_batch.qty == batch.qty |
| 17 | + assert saved_batch.eta == batch.eta |
50 | 18 |
|
51 | 19 |
|
52 | 20 | 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) |
| 21 | + sku = "PONY-STATUE" |
| 22 | + line = django_model.OrderLine.objects.create(orderid="order1", sku=sku, qty=12) |
| 23 | + django_model.Batch.objects.create(reference="batch1", sku=sku, qty=100, eta=None) |
| 24 | + django_model.Batch.objects.create(reference="batch2", sku=sku, qty=100, eta=None) |
| 25 | + django_model.Allocation.objects.create( |
| 26 | + reference="batch2", sku=sku, qty=100, eta=None |
| 27 | + ) |
57 | 28 |
|
58 | 29 | repo = repository.SqlAlchemyRepository(session) |
59 | 30 | retrieved = repo.get("batch1") |
60 | 31 |
|
61 | | - expected = model.Batch("batch1", "GENERIC-SOFA", 100, eta=None) |
| 32 | + expected = model.Batch("batch1", sku, 100, eta=None) |
62 | 33 | assert retrieved == expected # Batch.__eq__ only compares reference |
63 | 34 | assert retrieved.sku == expected.sku |
64 | 35 | assert retrieved._purchased_quantity == expected._purchased_quantity |
65 | 36 | assert retrieved._allocations == { |
66 | | - model.OrderLine("order1", "GENERIC-SOFA", 12), |
| 37 | + model.OrderLine("order1", sku, 12), |
67 | 38 | } |
0 commit comments