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