Skip to content

Commit 0cff3fc

Browse files
committed
refactor e2e tests, move random_refs and api_client out
1 parent 210fb65 commit 0cff3fc

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

tests/e2e/api_client.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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',
9+
json={'ref': ref, 'sku': sku, 'qty': qty, 'eta': eta}
10+
)
11+
assert r.status_code == 201
12+
13+
14+
def post_to_allocate(orderid, sku, qty, expect_success=True):
15+
url = config.get_api_url()
16+
r = requests.post(f'{url}/allocate', json={
17+
'orderid': orderid, 'sku': sku, 'qty': qty,
18+
})
19+
if expect_success:
20+
assert r.status_code == 201
21+
return r

tests/e2e/test_api.py

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

175

186
@pytest.mark.usefixtures('postgres_db')
197
@pytest.mark.usefixtures('restart_api')
208
def test_happy_path_returns_201_and_allocated_batch():
219
sku, othersku = random_sku(), random_sku('other')
2210
batch1, batch2, batch3 = random_batchref(1), random_batchref(2), random_batchref(3)
23-
post_to_add_batch(batch1, sku, 100, '2011-01-02')
24-
post_to_add_batch(batch2, sku, 100, '2011-01-01')
25-
post_to_add_batch(batch3, othersku, 100, None)
26-
data = {'orderid': random_orderid(), 'sku': sku, 'qty': 3}
27-
url = config.get_api_url()
28-
r = requests.post(f'{url}/allocate', json=data)
29-
assert r.status_code == 201
30-
assert r.json()['batchref'] == batch2
11+
api_client.post_to_add_batch(batch1, sku, 100, '2011-01-02')
12+
api_client.post_to_add_batch(batch2, sku, 100, '2011-01-01')
13+
api_client.post_to_add_batch(batch3, othersku, 100, None)
14+
15+
response = api_client.post_to_allocate(random_orderid(), sku, qty=3)
16+
17+
assert response.json()['batchref'] == batch2
3118

3219

3320
@pytest.mark.usefixtures('postgres_db')
3421
@pytest.mark.usefixtures('restart_api')
3522
def test_unhappy_path_returns_400_and_error_message():
3623
unknown_sku, orderid = random_sku(), random_orderid()
37-
data = {'orderid': orderid, 'sku': unknown_sku, 'qty': 20}
38-
url = config.get_api_url()
39-
r = requests.post(f'{url}/allocate', json=data)
40-
assert r.status_code == 400
41-
assert r.json()['message'] == f'Invalid sku {unknown_sku}'
24+
25+
response = api_client.post_to_allocate(
26+
orderid, unknown_sku, qty=20, expect_success=False,
27+
)
28+
29+
assert response.status_code == 400
30+
assert response.json()['message'] == f'Invalid sku {unknown_sku}'

0 commit comments

Comments
 (0)