Example #1
0
def test_orderline_mapper_can_load_lines(session):
    session.execute('INSERT INTO order_lines(orderid, sku, qty) VALUES '
                    '("order1", "RED-CHAIR", 12), '
                    '("order1", "RED-TABLE", 13), '
                    '("order2", "BLUE-LIPSTICK", 14)')
    expected = [
        model.OrderLine("order1", "RED-CHAIR", 12),
        model.OrderLine("order2", "RED-TABLE", 13),
        model.OrderLine("order2", "BLUE-LIPSTICK", 14)
    ]

    assert session.query(model.OrderLine).all() == expected
Example #2
0
def test_orderline_mapper_can_save_lines(session):
    new_line = model.OrderLine("order1", "DECORATIVE-WIDGET", 12)
    session.add(new_line)
    session.commit()

    rows = list(session.execute('SELECT orderid. sku, qty FROM "order_lines"'))
    assert rows == [("order1", "DECORATIVE-WIDGET", 12)]
Example #3
0
def test_saving_allocations(session_factory):
    session = session_factory
    batch = model.Batch('batch1', 'sku1', 100, eta=None)
    line = model.OrderLine('order1', 'sku1', 10)
    batch.allocate(line)
    session.add(batch)
    session.commit()
    rows = list(
        session.execute('SELECT orderline_id, batch_id FROM "allocations"'))
    assert rows == [(batch.id, line.id)]
Example #4
0
def try_to_allocate(orderid, sku, exceptions, session_factory):
    line = model.OrderLine(orderid, sku, 10)
    try:
        with unit_of_work.SqlAlchemyUnitOfWork(session_factory) as uow:
            product = uow.products.get(sku=sku)
            product.allocate(line)
            time.sleep(0.2)
            uow.commit()
    except Exception as e:  # pylint: disable=broad-except
        print(traceback.format_exc())
        exceptions.append(e)
Example #5
0
def allocate_endpoint():
    session = get_session()
    repo = repository.SqlAlchemyRepository(session)
    line = model.OrderLine(request.json['orderid'], request.json['sku'],
                           request.json['qty'])

    try:
        batchref = services.deallocate(line, repo, session)
    except (model.NotAllocatedOrder, services.InvalidSku) as e:
        return jsonify({'message': str(e)}), 400

    session.commit()
    return jsonify({'batchref': batchref}), 201
def test_uow_can_retrieve_a_batch_and_allocate_to_it(session_factory):
    session = session_factory()
    insert_batch(session, 'batch1', 'HIPSTER-WORKBENCH', 100, None)
    session.commit()

    uow = unit_of_work.SqlAlchemyUnitOfWork(session_factory)
    with uow:
        batch = uow.batches.get(reference='batch1')
        line = model.OrderLine('o1', 'HIPSTER-WORKBENCH', 10)
        batch.allocate(line)
        uow.commit()

    batchref = get_allocated_batch_ref(session, 'o1', 'HIPSTER-WORKBENCH')
    assert batchref == 'batch1'
Example #7
0
def test_uow_can_retrieve_a_batch_and_allocate_to_it(postgres_session):
    batch_ref = random_batchref("2020")
    orderline_id = random_orderid("2020")
    sku = random_sku("HIPSTER-WORKBENCH")
    session = postgres_session
    insert_batch(session, batch_ref, sku, 100, None)
    session.commit()
    uow = unit_of_work.SqlAlchemyUnitOfWork()
    with uow:
        product = uow.products.get(sku=sku)
        line = model.OrderLine(orderline_id, sku, 10)
        product.allocate(line)
        uow.commit()
    batchref = get_allocated_batch_ref(session, orderline_id, sku)
    assert batchref == batch_ref
Example #8
0
def test_repository_can_retrieve_a_batch_with_allocations(session_factory):
    session = session_factory
    orderline_id = insert_order_line(session)
    batch1_id = insert_batch(session, "batch1")
    insert_batch(session, "batch2")
    insert_allocation(session, orderline_id, batch1_id)

    retrieved = repository.SqlAlchemyRepository(session).get("batch1")
    #retrieved = repo.get("batch1")

    expected = model.Batch("batch1", "GENERIC-SOFA", 100, eta=None)
    assert retrieved == expected  # Batch.__eq__ only compares reference
    assert retrieved.sku == expected.sku
    assert retrieved._purchased_quantity == expected._purchased_quantity
    assert retrieved._allocations == {
        model.OrderLine("order1", "GENERIC-SOFA", 12),
    }
Example #9
0
def test_repository_can_retrieve_a_batch_with_allocations(session):
    orderline_id = insert_order_line(session)
    batch1_id = insert_batch(session, "batch1")
    insert_batch(session, "batch2")
    insert_allocation(session, orderline_id, batch1_id)

    repo = repository.SqlAlchemyRepository(session)
    retrieved = repo.get("batch1")

    expected = model.Batch("batch1", "GENERIC-SOFA", 100, eta=None)

    assert retrieved == expected
    assert retrieved.sku == expected.sku
    assert retrieved._purchased_quantity == expected._purchased_quantity
    assert retrieved._allocations == {
        model.OrderLine("order1", "GENERIC-SOFA", 12)
    }
Example #10
0
def test_retrieving_allocations(session_factory):
    session = session_factory
    session.execute(
        'INSERT INTO order_lines (orderid, sku, qty) VALUES ("order1", "sku1", 12)'
    )
    [[olid]] = session.execute(
        'SELECT id FROM order_lines WHERE orderid=:orderid AND sku=:sku',
        dict(orderid='order1', sku='sku1'))
    session.execute(
        'INSERT INTO batches (reference, sku, _purchased_quantity, eta)'
        ' VALUES ("batch1", "sku1", 100, null)')
    [[bid]] = session.execute(
        'SELECT id FROM batches WHERE reference=:ref AND sku=:sku',
        dict(ref='batch1', sku='sku1'))
    session.execute(
        'INSERT INTO allocations (orderline_id, batch_id) VALUES (:olid, :bid)',
        dict(olid=olid, bid=bid))

    batch = session.query(model.Batch).one()

    assert batch._allocations == {model.OrderLine("order1", "sku1", 12)}