|
| 1 | +import json |
| 2 | +import pytest |
| 3 | +from tenacity import Retrying, RetryError, stop_after_delay |
| 4 | +from . import api_client, redis_client |
| 5 | +from ..random_refs import random_batchref, random_orderid, random_sku |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +@pytest.mark.usefixtures('postgres_db') |
| 10 | +@pytest.mark.usefixtures('restart_api') |
| 11 | +@pytest.mark.usefixtures('restart_redis_pubsub') |
| 12 | +def test_change_batch_quantity_leading_to_reallocation(): |
| 13 | + # start with two batches and an order allocated to one of them |
| 14 | + orderid, sku = random_orderid(), random_sku() |
| 15 | + earlier_batch, later_batch = random_batchref('old'), random_batchref('newer') |
| 16 | + api_client.post_to_add_batch(earlier_batch, sku, qty=10, eta='2011-01-02') |
| 17 | + api_client.post_to_add_batch(later_batch, sku, qty=10, eta='2011-01-02') |
| 18 | + response = api_client.post_to_allocate(orderid, sku, 10) |
| 19 | + assert response.json()['batchref'] == earlier_batch |
| 20 | + |
| 21 | + subscription = redis_client.subscribe_to('line_allocated') |
| 22 | + |
| 23 | + # change quantity on allocated batch so it's less than our order |
| 24 | + redis_client.publish_message('change_batch_quantity', { |
| 25 | + 'batchref': earlier_batch, 'qty': 5 |
| 26 | + }) |
| 27 | + |
| 28 | + # wait until we see a message saying the order has been reallocated |
| 29 | + messages = [] |
| 30 | + for attempt in Retrying(stop=stop_after_delay(3), reraise=True): |
| 31 | + with attempt: |
| 32 | + message = subscription.get_message(timeout=1) |
| 33 | + if message: |
| 34 | + messages.append(message) |
| 35 | + print(messages) |
| 36 | + data = json.loads(messages[-1]['data']) |
| 37 | + assert data['orderid'] == orderid |
| 38 | + assert data['batchref'] == later_batch |
0 commit comments