From 5e3e7ae03e852c2e04c826741b24b24543dee32c Mon Sep 17 00:00:00 2001 From: Harry Date: Fri, 28 Jun 2019 15:12:28 +0100 Subject: [PATCH] E2E test for deallocate --- conftest.py | 6 ++++-- flask_app.py | 4 +++- orm.py | 4 +++- test_api.py | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/conftest.py b/conftest.py index da82d88a..83df460b 100644 --- a/conftest.py +++ b/conftest.py @@ -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() diff --git a/flask_app.py b/flask_app.py index 3412cbe9..d14af59e 100644 --- a/flask_app.py +++ b/flask_app.py @@ -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: diff --git a/orm.py b/orm.py index 3e6b4a70..eb30a62d 100644 --- a/orm.py +++ b/orm.py @@ -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, ) }, ) diff --git a/test_api.py b/test_api.py index 5386ab88..89fa1241 100644 --- a/test_api.py +++ b/test_api.py @@ -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