Skip to content

Commit 5e3e7ae

Browse files
committed
E2E test for deallocate
1 parent 952a3d2 commit 5e3e7ae

4 files changed

Lines changed: 47 additions & 4 deletions

File tree

‎conftest.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,13 @@ def _add_stock(lines):
9191
dict(batch_id=batch_id),
9292
)
9393
postgres_session.execute(
94-
"DELETE FROM batches WHERE id=:batch_id", dict(batch_id=batch_id),
94+
"DELETE FROM batches WHERE id=:batch_id",
95+
dict(batch_id=batch_id),
9596
)
9697
for sku in skus_added:
9798
postgres_session.execute(
98-
"DELETE FROM order_lines WHERE sku=:sku", dict(sku=sku),
99+
"DELETE FROM order_lines WHERE sku=:sku",
100+
dict(sku=sku),
99101
)
100102
postgres_session.commit()
101103

‎flask_app.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ def allocate_endpoint():
1919
session = get_session()
2020
repo = repository.SqlAlchemyRepository(session)
2121
line = model.OrderLine(
22-
request.json["orderid"], request.json["sku"], request.json["qty"],
22+
request.json["orderid"],
23+
request.json["sku"],
24+
request.json["qty"],
2325
)
2426

2527
try:

‎orm.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ def start_mappers():
4141
batches,
4242
properties={
4343
"_allocations": relationship(
44-
lines_mapper, secondary=allocations, collection_class=set,
44+
lines_mapper,
45+
secondary=allocations,
46+
collection_class=set,
4547
)
4648
},
4749
)

‎test_api.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,40 @@ def test_unhappy_path_returns_400_and_error_message():
5151
r = requests.post(f"{url}/allocate", json=data)
5252
assert r.status_code == 400
5353
assert r.json()["message"] == f"Invalid sku {unknown_sku}"
54+
55+
56+
@pytest.mark.usefixtures("postgres_db")
57+
@pytest.mark.usefixtures("restart_api")
58+
def test_deallocate():
59+
sku, order1, order2 = random_sku(), random_orderid(), random_orderid()
60+
batch = random_batchref()
61+
post_to_add_batch(batch, sku, 100, "2011-01-02")
62+
url = config.get_api_url()
63+
# fully allocate
64+
r = requests.post(
65+
f"{url}/allocate", json={"orderid": order1, "sku": sku, "qty": 100}
66+
)
67+
assert r.json()["batchid"] == batch
68+
69+
# cannot allocate second order
70+
r = requests.post(
71+
f"{url}/allocate", json={"orderid": order2, "sku": sku, "qty": 100}
72+
)
73+
assert r.status_code == 400
74+
75+
# deallocate
76+
r = requests.post(
77+
f"{url}/deallocate",
78+
json={
79+
"orderid": order1,
80+
"sku": sku,
81+
},
82+
)
83+
assert r.ok
84+
85+
# now we can allocate second order
86+
r = requests.post(
87+
f"{url}/allocate", json={"orderid": order2, "sku": sku, "qty": 100}
88+
)
89+
assert r.ok
90+
assert r.json()["batchid"] == batch

0 commit comments

Comments
 (0)