Skip to content

Commit f99ab96

Browse files
committed
refactor e2e tests, move random_refs and api_client out
1 parent 120f598 commit f99ab96

File tree

2 files changed

+43
-25
lines changed

2 files changed

+43
-25
lines changed

tests/e2e/api_client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import requests
2+
from allocation import config
3+
4+
5+
def post_to_add_batch(ref, sku, qty, eta):
6+
url = config.get_api_url()
7+
r = requests.post(
8+
f"{url}/add_batch", json={"ref": ref, "sku": sku, "qty": qty, "eta": eta}
9+
)
10+
assert r.status_code == 201
11+
12+
13+
def post_to_allocate(orderid, sku, qty, expect_success=True):
14+
url = config.get_api_url()
15+
r = requests.post(
16+
f"{url}/allocate",
17+
json={
18+
"orderid": orderid,
19+
"sku": sku,
20+
"qty": qty,
21+
},
22+
)
23+
if expect_success:
24+
assert r.status_code == 201
25+
return r

tests/e2e/test_api.py

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
import pytest
2-
import requests
3-
4-
from allocation import config
5-
from ..random_refs import random_sku, random_batchref, random_orderid
6-
7-
8-
def post_to_add_batch(ref, sku, qty, eta):
9-
url = config.get_api_url()
10-
r = requests.post(
11-
f"{url}/add_batch", json={"ref": ref, "sku": sku, "qty": qty, "eta": eta}
12-
)
13-
assert r.status_code == 201
2+
from ..random_refs import random_batchref, random_orderid, random_sku
3+
from . import api_client
144

155

166
@pytest.mark.usefixtures("postgres_db")
@@ -20,24 +10,27 @@ def test_happy_path_returns_201_and_allocated_batch():
2010
earlybatch = random_batchref(1)
2111
laterbatch = random_batchref(2)
2212
otherbatch = random_batchref(3)
23-
post_to_add_batch(laterbatch, sku, 100, "2011-01-02")
24-
post_to_add_batch(earlybatch, sku, 100, "2011-01-01")
25-
post_to_add_batch(otherbatch, othersku, 100, None)
26-
data = {"orderid": random_orderid(), "sku": sku, "qty": 3}
13+
api_client.post_to_add_batch(laterbatch, sku, 100, "2011-01-02")
14+
api_client.post_to_add_batch(earlybatch, sku, 100, "2011-01-01")
15+
api_client.post_to_add_batch(otherbatch, othersku, 100, None)
2716

28-
url = config.get_api_url()
29-
r = requests.post(f"{url}/allocate", json=data)
17+
response = api_client.post_to_allocate(random_orderid(), sku, qty=3)
3018

31-
assert r.status_code == 201
32-
assert r.json()["batchref"] == earlybatch
19+
assert response.status_code == 201
20+
assert response.json()["batchref"] == earlybatch
3321

3422

3523
@pytest.mark.usefixtures("postgres_db")
3624
@pytest.mark.usefixtures("restart_api")
3725
def test_unhappy_path_returns_400_and_error_message():
3826
unknown_sku, orderid = random_sku(), random_orderid()
39-
data = {"orderid": orderid, "sku": unknown_sku, "qty": 20}
40-
url = config.get_api_url()
41-
r = requests.post(f"{url}/allocate", json=data)
42-
assert r.status_code == 400
43-
assert r.json()["message"] == f"Invalid sku {unknown_sku}"
27+
28+
response = api_client.post_to_allocate(
29+
orderid,
30+
unknown_sku,
31+
qty=20,
32+
expect_success=False,
33+
)
34+
35+
assert response.status_code == 400
36+
assert response.json()["message"] == f"Invalid sku {unknown_sku}"

0 commit comments

Comments
 (0)