def test_unhappy_path_returns_400_and_error_message(): unknown_sku, orderid = random_sku(), random_orderid() data = {'orderid': orderid, 'sku': unknown_sku, 'qty': 20} url = config.get_api_url() r = requests.post(f'{url}/allocate', json=data) assert r.status_code == 400 assert r.json()['message'] == f'Invalid sku {unknown_sku}'
def post_list_batches(ref, local: bool = False): url = config.get_api_url(local) r = requests.post(f'{url}/batches', json={ 'ref': ref, }) print(r) assert r.status_code == 200 return r
def post_to_allocate(orderid, sku, qty, expect_success=True): url = config.get_api_url() r = requests.post( f"{url}/allocate", json={"orderid": orderid, "sku": sku, "qty": qty,} ) if expect_success: assert r.status_code == 202 return r
def wait_for_webapp_to_come_up(): deadline = time.time() + 10 url = config.get_api_url() while time.time() < deadline: try: return requests.get(url) except ConnectionError: time.sleep(0.5) pytest.fail('API never came up')
def post_to_add_batch(ref, sku, qty, eta): url = config.get_api_url() r = requests.post(f'{url}/add_batch', json={ 'ref': ref, 'sku': sku, 'qty': qty, 'eta': eta, }) assert r.status_code == 201
def test_400_message_for_out_of_stock(): sku = random_sku() small_batch = random_batchref(1) large_order = random_orderid(1) post_to_add_batch(small_batch, sku, 100, '2011-01-02') data = {'orderid': large_order, 'sku': sku, 'qty': 120} url = config.get_api_url() r = requests.post(f'{url}/allocate', json=data) assert r.status_code == 400 assert r.json()['message'] == f'Out of stock for sku {sku}'
def test_happy_path_returns_201_and_allocated_batch(): sku, othersku = random_sku(), random_sku('other') earlybatch = random_batchref(1) laterbatch = random_batchref(2) otherbatch = random_batchref(3) post_to_add_batch(laterbatch, sku, 100, '2011-01-02') post_to_add_batch(earlybatch, sku, 100, '2011-01-01') post_to_add_batch(otherbatch, othersku, 100, None) data = {'orderid': random_orderid(), 'sku': sku, 'qty': 3} url = config.get_api_url() r = requests.post(f'{url}/allocate', json=data) assert r.status_code == 201 assert r.json()['batchref'] == earlybatch
def post_to_allocate(orderid, sku, qty, expect_success=True, local: bool = False): url = config.get_api_url(local) r = requests.post(f'{url}/allocate', json={ 'orderid': orderid, 'sku': sku, 'qty': qty, }) if expect_success: assert r.status_code == 202 return r
def test_api_returns_allocation(add_stock): sku, othersku = random_sku(), random_sku('other') earlybatch = random_batchref(1) laterbatch = random_batchref(2) otherbatch = random_batchref(3) add_stock([ (laterbatch, sku, 100, '2011-01-02'), (earlybatch, sku, 100, '2011-01-01'), (otherbatch, sku, 100, None), ]) data = {'orderid': random_orderid(), 'sku': sku, 'qty': 3} url = config.get_api_url() r = requests.post(f'{url}/allocate', json=data) assert r.status_code == 201 assert r.json()['batchref'] == earlybatch
def test_happy_path_returns_202_and_batch_is_allocated(): orderid = random_orderid() sku, othersku = random_sku(), random_sku('other') early_batch = random_batchref(1) later_order = random_orderid(1) other_order = random_orderid(1) post_to_add_batch(later_order, sku, 100, '2011-01-02') post_to_add_batch(early_batch, sku, 100, '2011-01-01') post_to_add_batch(other_order, othersku, 100, None) data = {'orderid': orderid, 'sku': sku, 'qty': 3} url = config.get_api_url() r = requests.post(f'{url}/allocate', json=data) assert r.status_code == 202 print(orderid) r = requests.get(f'{url}/allocations/{orderid}') assert r.ok assert r.json() == [{'sku': sku, 'batchref': early_batch}]
def test_deallocate(): sku, order1, order2 = random_sku(), random_orderid(), random_orderid() batch = random_batchref() post_to_add_batch(batch, sku, 100, '2011-01-02') url = config.get_api_url() # fully allocate r = requests.post(f'{url}/allocate', json={ 'orderid': order1, 'sku': sku, 'qty': 100 }) assert r.json()['batchid'] == batch # cannot allocate second order r = requests.post(f'{url}/allocate', json={ 'orderid': order2, 'sku': sku, 'qty': 100 }) assert r.status_code == 400 # deallocate r = requests.post(f'{url}/deallocate', json={ 'orderid': order1, 'sku': sku, }) assert r.ok # now we can allocate second order r = requests.post(f'{url}/allocate', json={ 'orderid': order2, 'sku': sku, 'qty': 100 }) assert r.ok assert r.json()['batchid'] == batch
def test_happy_path_returns_201_and_allocated_batch(add_stock): sku, othersku = random_sku(), random_sku('other') earlybatch = random_batchref(1) laterbatch = random_batchref(2) otherbatch = random_batchref(3) add_stock([ (laterbatch, sku, 100, '2011-01-02'), (earlybatch, sku, 100, '2011-01-01'), (otherbatch, othersku, 100, None), ]) data = {'orderid': random_orderid(), 'sku': sku, 'qty': 3} url = config.get_api_url() r = requests.post(f'{url}/allocate', json=data) assert r.status_code == 201 assert r.json()['batchref'] == earlybatch @pytest.mark.usefixtures('restart_api') def test_unhappy_path_returns_400_and_error_message(): unknown_sku, orderid = random_sku(), random_orderid() data = {'orderid': orderid, 'sku': sku, 'qty': 10} url = config.get_api_url() r = requests.post(f'{url}/allocate', json=data) assert r.status_code == 400 assert r.json()['message'] == f'Invalid sku {unknown_sku}'
def init_labeler(username=None, password=None): config_ca_file = config.get_ca_file() ca_file = config_ca_file if config_ca_file else None username = username if username is not None else config.get_api_username() password = password if password is not None else config.get_api_password() api.init_connection(config.get_api_url(), username, password, ca_file)
def test_list_batches(): url = config.get_api_url() r = requests.get(f'{url}/batches') print(r) print(r.text) assert r.status_code == 200
def get_allocation(orderid): url = config.get_api_url() return requests.get(f"{url}/allocations/{orderid}")
def wait_for_webapp_to_come_up(): return requests.get(config.get_api_url())
def post_to_add_batch(ref, sku, qty, eta): url = config.get_api_url() r = requests.post( f"{url}/add_batch", json={"ref": ref, "sku": sku, "qty": qty, "eta": eta} ) assert r.status_code == 201