Ejemplo n.º 1
0
def test_allocations_view(sqlite_bus):

    orderid1 = random_orderid('1')
    other_orderid1 = random_orderid('other-1')
    sku1, sku2 = random_sku('1'), random_sku('2')

    batch1, batch2, other_batch1 = random_batchref('1'), random_batchref(
        '2'), random_batchref('other-1')
    sqlite_bus.handle(commands.CreateBatch(batch1, sku1, 50, None))
    sqlite_bus.handle(commands.CreateBatch(batch2, sku2, 50, None))
    sqlite_bus.handle(commands.Allocate(orderid1, sku1, 20))
    sqlite_bus.handle(commands.Allocate(orderid1, sku2, 20))

    sqlite_bus.handle(commands.CreateBatch(other_batch1, sku1, 50, today))

    sqlite_bus.handle(commands.Allocate(other_orderid1, sku1, 30))
    sqlite_bus.handle(commands.Allocate(other_orderid1, sku2, 10))

    assert views.allocations(orderid1, sqlite_bus.uow) == [
        {
            'sku': sku1,
            'batchref': batch1
        },
        {
            'sku': sku2,
            'batchref': batch2
        },
    ]
Ejemplo n.º 2
0
def test_happy_path_returns_201_and_allocated_batch():
    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': 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'] == early_batch
Ejemplo n.º 3
0
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}]
Ejemplo n.º 4
0
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}'
Ejemplo n.º 5
0
def test_unhappy_path_returns_400_and_error_message():
    unknown_sku = random_sku()
    order_id = random_orderid(1)

    data = {'orderid': order_id, '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}'

    r = requests.get(f'{url}/allocations/{order_id}')
    assert r.status_code == 404
Ejemplo n.º 6
0
def test_concurrent_updates_to_version_are_not_allowed(session_factory_real_db):
    sku = random_sku('1')
    batch = random_batchref('1')
    session = session_factory_real_db()

    batch = insert_batch(session, batch, sku, 100, eta=None)
    insert_product(session, sku,  [batch], version_number=1)
    session.commit()


    # sku = 'HIPSTER-WORKBENCH'
    # batch = 'b1'


    order1, order2 = 'o1', 'o2'
    exceptions = []  # type: List[Exception]

    try_to_allocate_order1 = lambda: try_to_allocate(order1, sku, exceptions)
    try_to_allocate_order2 = lambda: try_to_allocate(order2, sku, exceptions)

    thread1 = threading.Thread(target=try_to_allocate_order1)
    thread2 = threading.Thread(target=try_to_allocate_order2)

    thread1.start()
    thread2.start()
    thread1.join()
    thread2.join()

    [[version]] = session.execute(
        'SELECT version_number FROM products WHERE sku=:sku',
        dict(sku=sku)
    )

    assert version == 2
    #if len(exceptions) > 0:
    #    [exception] = exceptions
    #    assert 'could npt serialize access due to concurrent update' in str(exception)

    orders = list(session.execute(
        'SELECT orderid FROM allocations '
        '  JOIN batches ON allocations.batch_id=batches.id'
        '  JOIN order_lines ON allocations.orderline_id=order_lines.id'
        ' WHERE order_lines.sku=:sku',
        dict(sku=sku)
    ))
    assert len(orders) == 1
    with unit_of_work.SqlAlchemyUnitOfWork as uow:
        uow.session.execute('select 1')