Skip to content

Commit d1c70f8

Browse files
committed
refactor e2e tests, move random_refs and api_client out
1 parent cc1ecbb commit d1c70f8

5 files changed

Lines changed: 50 additions & 38 deletions

File tree

tests/__init__.py

Whitespace-only changes.

tests/e2e/__init__.py

Whitespace-only changes.

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 & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,30 @@
1-
import uuid
21
import pytest
3-
import requests
4-
5-
from allocation import config
6-
7-
def random_suffix():
8-
return uuid.uuid4().hex[:6]
9-
10-
def random_sku(name=''):
11-
return f'sku-{name}-{random_suffix()}'
12-
13-
def random_batchref(name=''):
14-
return f'batch-{name}-{random_suffix()}'
15-
16-
def random_orderid(name=''):
17-
return f'order-{name}-{random_suffix()}'
18-
19-
20-
def post_to_add_batch(ref, sku, qty, eta):
21-
url = config.get_api_url()
22-
r = requests.post(
23-
f'{url}/add_batch',
24-
json={'ref': ref, 'sku': sku, 'qty': qty, 'eta': eta}
25-
)
26-
assert r.status_code == 201
2+
from ..random_refs import random_batchref, random_orderid, random_sku
3+
from . import api_client
274

285

296
@pytest.mark.usefixtures('postgres_db')
307
@pytest.mark.usefixtures('restart_api')
318
def test_happy_path_returns_201_and_allocated_batch():
329
sku, othersku = random_sku(), random_sku('other')
3310
batch1, batch2, batch3 = random_batchref(1), random_batchref(2), random_batchref(3)
34-
post_to_add_batch(batch1, sku, 100, '2011-01-02')
35-
post_to_add_batch(batch2, sku, 100, '2011-01-01')
36-
post_to_add_batch(batch3, othersku, 100, None)
37-
data = {'orderid': random_orderid(), 'sku': sku, 'qty': 3}
38-
url = config.get_api_url()
39-
r = requests.post(f'{url}/allocate', json=data)
40-
assert r.status_code == 201
41-
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
4218

4319

4420
@pytest.mark.usefixtures('postgres_db')
4521
@pytest.mark.usefixtures('restart_api')
4622
def test_unhappy_path_returns_400_and_error_message():
4723
unknown_sku, orderid = random_sku(), random_orderid()
48-
data = {'orderid': orderid, 'sku': unknown_sku, 'qty': 20}
49-
url = config.get_api_url()
50-
r = requests.post(f'{url}/allocate', json=data)
51-
assert r.status_code == 400
52-
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}'

tests/random_refs.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import uuid
2+
3+
def random_suffix():
4+
return uuid.uuid4().hex[:6]
5+
6+
def random_sku(name=''):
7+
return f'sku-{name}-{random_suffix()}'
8+
9+
def random_batchref(name=''):
10+
return f'batch-{name}-{random_suffix()}'
11+
12+
def random_orderid(name=''):
13+
return f'order-{name}-{random_suffix()}'

0 commit comments

Comments
 (0)