|
1 | | -import uuid |
2 | 1 | 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 |
27 | 4 |
|
28 | 5 |
|
29 | 6 | @pytest.mark.usefixtures('postgres_db') |
30 | 7 | @pytest.mark.usefixtures('restart_api') |
31 | 8 | def test_happy_path_returns_201_and_allocated_batch(): |
32 | 9 | sku, othersku = random_sku(), random_sku('other') |
33 | 10 | 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 |
42 | 18 |
|
43 | 19 |
|
44 | 20 | @pytest.mark.usefixtures('postgres_db') |
45 | 21 | @pytest.mark.usefixtures('restart_api') |
46 | 22 | def test_unhappy_path_returns_400_and_error_message(): |
47 | 23 | 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}' |
0 commit comments