Skip to content

Commit 2fbf6bc

Browse files
committed
wip on django repository tests
1 parent 7526014 commit 2fbf6bc

2 files changed

Lines changed: 23 additions & 47 deletions

File tree

src/allocation/service_layer/services.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ def is_valid_sku(sku, batches):
1616

1717

1818
def add_batch(
19-
ref: str, sku: str, qty: int, eta: Optional[date],
19+
ref: str,
20+
sku: str,
21+
qty: int,
22+
eta: Optional[date],
2023
uow: unit_of_work.AbstractUnitOfWork,
2124
):
2225
with uow:
@@ -25,7 +28,9 @@ def add_batch(
2528

2629

2730
def allocate(
28-
orderid: str, sku: str, qty: int,
31+
orderid: str,
32+
sku: str,
33+
qty: int,
2934
uow: unit_of_work.AbstractUnitOfWork,
3035
) -> str:
3136
line = OrderLine(orderid, sku, qty)
Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,38 @@
11
# pylint: disable=protected-access
22
from allocation.domain import model
33
from allocation.adapters import repository
4+
from allocation import django_model
45

56

67
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))
89

910
repo = repository.SqlAlchemyRepository(session)
1011
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)]
1712

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
5018

5119

5220
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+
)
5728

5829
repo = repository.SqlAlchemyRepository(session)
5930
retrieved = repo.get("batch1")
6031

61-
expected = model.Batch("batch1", "GENERIC-SOFA", 100, eta=None)
32+
expected = model.Batch("batch1", sku, 100, eta=None)
6233
assert retrieved == expected # Batch.__eq__ only compares reference
6334
assert retrieved.sku == expected.sku
6435
assert retrieved._purchased_quantity == expected._purchased_quantity
6536
assert retrieved._allocations == {
66-
model.OrderLine("order1", "GENERIC-SOFA", 12),
37+
model.OrderLine("order1", sku, 12),
6738
}

0 commit comments

Comments
 (0)