11import 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" )
3725def 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