|
1 | | -import uuid |
2 | 1 | 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 |
16 | 4 |
|
17 | 5 |
|
18 | 6 | @pytest.mark.usefixtures('postgres_db') |
19 | 7 | @pytest.mark.usefixtures('restart_api') |
20 | 8 | def test_happy_path_returns_201_and_allocated_batch(): |
21 | 9 | sku, othersku = random_sku(), random_sku('other') |
22 | 10 | 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 |
31 | 18 |
|
32 | 19 |
|
33 | 20 | @pytest.mark.usefixtures('postgres_db') |
34 | 21 | @pytest.mark.usefixtures('restart_api') |
35 | 22 | def test_unhappy_path_returns_400_and_error_message(): |
36 | 23 | 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