def test_change_batch_quantity_leading_to_reallocation(): # start with two batches and an order allocated to one of them orderid, sku = random_orderid(), random_sku() earlier_batch, later_batch = random_batchref('old'), random_batchref( 'newer') api_client.post_to_add_batch(earlier_batch, sku, qty=10, eta='2011-01-01') api_client.post_to_add_batch(later_batch, sku, qty=10, eta='2011-01-02') response = api_client.post_to_allocate(orderid, sku, 10) assert response.ok response = api_client.get_allocation(orderid) assert response.json()[0]['batchref'] == earlier_batch subscription = redis_client.subscribe_to('line_allocated') # change quantity on allocated batch so it's less than our order redis_client.publish_message('change_batch_quantity', { 'batchref': earlier_batch, 'qty': 5 }) # wait until we see a message saying the order has been reallocated messages = [] for attempt in Retrying(stop=stop_after_delay(3), reraise=True): with attempt: message = subscription.get_message(timeout=1) if message: messages.append(message) logger.info("%s", messages) data = json.loads(messages[-1]['data']) assert data['orderid'] == orderid assert data['batchref'] == later_batch
def test_change_batch_qty_leading_to_reallocation(): ''' 测试改变批次数量 导致重新分配 ''' orderid, sku = random_orderid(), random_sku() earlier_batch, later_batch = random_batchref('old'), random_batchref('new') api_client.post_to_add_batch(earlier_batch, sku, 10, str(today)) api_client.post_to_add_batch(later_batch, sku, 10, str(tomorrow)) api_client.post_to_allocte(orderid, sku, 10) r = api_client.get_allocation(orderid) assert r.json()[0]['batchref'] == earlier_batch subscription = redis_client.subscribe_to('line_allocated') # 发布一个命令 终端的事件消费者接收到 调用hander 这个逻辑里 成功完成分配订单 发布了一个line_allocated redis_client.publish_message('change_batch_quantity', { 'batchref': earlier_batch, 'qty': 5 }) messages = [] for attempt in Retrying(stop=stop_after_delay(3), reraise=True): with attempt: message = subscription.get_message(timeout=1) if message: messages.append(message) print(message) data = json.loads(messages[-1]['data']) assert data['orderid'] == orderid assert data['batchref'] == later_batch
def test_unhappy_path_returns_400_and_error_msg(): sku, orderid = random_sku(), random_orderid() r = api_client.post_to_allocte(orderid, sku, 200, expect_success=False) assert r.status_code == 400 assert r.json()['message'] == f'Invalid sku {sku}' r = api_client.get_allocation(orderid) assert r.status_code == 404
def test_happy_path_returns_202_and_allocated_batch(): sku, othersku = random_sku(), random_sku('other') earlybatch = random_batchref(1) laterbatch = random_batchref(2) otherbatch = random_batchref(3) api_client.post_to_add_batch(laterbatch, sku, 100, '2011-01-02') api_client.post_to_add_batch(earlybatch, sku, 100, '2011-01-01') api_client.post_to_add_batch(otherbatch, othersku, 100, None) orderid = random_orderid() r = api_client.post_to_allocte(orderid, sku, 3) assert r.status_code == 202 r = api_client.get_allocation(orderid=orderid) assert r.ok assert r.json() == [{'sku': sku, 'batchref': earlybatch}]