Skip to content

Commit

Permalink
E2E test for deallocate
Browse files Browse the repository at this point in the history
  • Loading branch information
hjwp committed Feb 24, 2021
1 parent 952a3d2 commit 5e3e7ae
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
6 changes: 4 additions & 2 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,13 @@ def _add_stock(lines):
dict(batch_id=batch_id),
)
postgres_session.execute(
"DELETE FROM batches WHERE id=:batch_id", dict(batch_id=batch_id),
"DELETE FROM batches WHERE id=:batch_id",
dict(batch_id=batch_id),
)
for sku in skus_added:
postgres_session.execute(
"DELETE FROM order_lines WHERE sku=:sku", dict(sku=sku),
"DELETE FROM order_lines WHERE sku=:sku",
dict(sku=sku),
)
postgres_session.commit()

Expand Down
4 changes: 3 additions & 1 deletion flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def allocate_endpoint():
session = get_session()
repo = repository.SqlAlchemyRepository(session)
line = model.OrderLine(
request.json["orderid"], request.json["sku"], request.json["qty"],
request.json["orderid"],
request.json["sku"],
request.json["qty"],
)

try:
Expand Down
4 changes: 3 additions & 1 deletion orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def start_mappers():
batches,
properties={
"_allocations": relationship(
lines_mapper, secondary=allocations, collection_class=set,
lines_mapper,
secondary=allocations,
collection_class=set,
)
},
)
37 changes: 37 additions & 0 deletions test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,40 @@ def test_unhappy_path_returns_400_and_error_message():
r = requests.post(f"{url}/allocate", json=data)
assert r.status_code == 400
assert r.json()["message"] == f"Invalid sku {unknown_sku}"


@pytest.mark.usefixtures("postgres_db")
@pytest.mark.usefixtures("restart_api")
def test_deallocate():
sku, order1, order2 = random_sku(), random_orderid(), random_orderid()
batch = random_batchref()
post_to_add_batch(batch, sku, 100, "2011-01-02")
url = config.get_api_url()
# fully allocate
r = requests.post(
f"{url}/allocate", json={"orderid": order1, "sku": sku, "qty": 100}
)
assert r.json()["batchid"] == batch

# cannot allocate second order
r = requests.post(
f"{url}/allocate", json={"orderid": order2, "sku": sku, "qty": 100}
)
assert r.status_code == 400

# deallocate
r = requests.post(
f"{url}/deallocate",
json={
"orderid": order1,
"sku": sku,
},
)
assert r.ok

# now we can allocate second order
r = requests.post(
f"{url}/allocate", json={"orderid": order2, "sku": sku, "qty": 100}
)
assert r.ok
assert r.json()["batchid"] == batch

0 comments on commit 5e3e7ae

Please sign in to comment.