Exemplo n.º 1
0
def test_increments_version_number():
    line = OrderLine("oref", "SCANDI-PEN", 10)
    product = Product(sku="SCANDI-PEN",
                      items=[Batch("b1", "SCANDI-PEN", 100, eta=None)])
    product.version_number = 7
    product.allocate(line)
    assert product.version_number == 8
Exemplo n.º 2
0
def test_records_out_of_stock_event_if_cannot_allocate():
    batch = Batch("batch1", "SMALL-FORK", 10, eta=today)
    product = Product(sku="SMALL-FORK", items=[batch])
    product.allocate(OrderLine("order1", "SMALL-FORK", 10))

    allocation = product.allocate(OrderLine("order2", "SMALL-FORK", 1))
    assert product.messages[-1] == events.OutOfStock(sku="SMALL-FORK")
    assert allocation is None
Exemplo n.º 3
0
def test_prefers_warehouse_batches_to_shipments():
    in_stock_batch = Batch("in-stock-batch", "RETRO-CLOCK", 100, eta=None)
    shipment_batch = Batch("shipment-batch", "RETRO-CLOCK", 100, eta=tomorrow)
    product = Product(sku="RETRO-CLOCK",
                      items=[in_stock_batch, shipment_batch])
    line = OrderLine("oref", "RETRO-CLOCK", 10)

    product.allocate(line)

    assert in_stock_batch.available_quantity == 90
    assert shipment_batch.available_quantity == 100
Exemplo n.º 4
0
def test_prefers_earlier_batches():
    earliest = Batch("speedy-batch", "MINIMALIST-SPOON", 100, eta=today)
    medium = Batch("normal-batch", "MINIMALIST-SPOON", 100, eta=tomorrow)
    latest = Batch("slow-batch", "MINIMALIST-SPOON", 100, eta=later)
    product = Product(sku="MINIMALIST-SPOON", items=[medium, earliest, latest])
    line = OrderLine("order1", "MINIMALIST-SPOON", 10)

    product.allocate(line)

    assert earliest.available_quantity == 90
    assert medium.available_quantity == 100
    assert latest.available_quantity == 100
Exemplo n.º 5
0
def test_returns_allocated_batch_ref():
    in_stock_batch = Batch("in-stock-batch-ref",
                           "HIGHBROW-POSTER",
                           100,
                           eta=None)
    shipment_batch = Batch("shipment-batch-ref",
                           "HIGHBROW-POSTER",
                           100,
                           eta=tomorrow)
    line = OrderLine("oref", "HIGHBROW-POSTER", 10)
    product = Product(sku="HIGHBROW-POSTER",
                      items=[in_stock_batch, shipment_batch])
    allocation = product.allocate(line)
    assert allocation == in_stock_batch.reference
Exemplo n.º 6
0
def test_error_for_invalid_sku() -> None:
    batch = Batch("b1", "AREALSKU", 100, eta=None)
    product = Product(batch.sku, [batch])
    uow = FakeUnitOfWork({Product: "sku"}, [product])
    with pytest.raises(services.allocation.InvalidSku,
                       match="Invalid sku NONEXISTENTSKU"):
        services.allocation.allocate("o1", "NONEXISTENTSKU", 10, uow)
Exemplo n.º 7
0
def add_batch(e: commands.CreateBatch, uow: AbstractUnitOfWork):
    """UOW를 이용해 배치를 추가합니다."""
    with uow:
        product = uow[Product].get(e.sku)

        if not product:
            product = Product(e.sku, items=[])
            uow[Product].add(product)
        product.items.append(Batch(e.ref, e.sku, e.qty, e.eta))
        uow.commit()
Exemplo n.º 8
0
def test_repository_can_save_a_batch(session: Session) -> None:
    batch = Batch("batch1", "RUSTY-SOAPDISH", 100, eta=None)
    product = Product(batch.sku, [batch])

    repo = SqlAlchemyRepository(Product, session)
    repo.add(product)
    session.commit()

    rows = list(
        session.execute(
            "SELECT reference, sku, _purchased_quantity, eta FROM batch"))
    assert rows == [("batch1", "RUSTY-SOAPDISH", 100, None)]
Exemplo n.º 9
0
def add_batch(
    ref: str, sku: str, qty: int, eta: Optional[datetime], uow: AbstractUnitOfWork
) -> None:
    """UOW를 이용해 배치를 추가합니다."""
    with uow:
        product = uow[Product].get(sku)

        if not product:
            product = Product(sku, items=[])
            uow[Product].add(product)
        product.items.append(Batch(ref, sku, qty, eta))
        uow.commit()
Exemplo n.º 10
0
def test_commits() -> None:
    batch = Batch("b1", "OMINOUS-MIRROR", 100, eta=None)
    product = Product(batch.sku, [batch])
    uow = FakeUnitOfWork({Product: "sku"}, [product])
    services.allocation.allocate("o1", "OMINOUS-MIRROR", 10, uow)
    assert uow.committed is True
Exemplo n.º 11
0
def test_returns_allocation() -> None:
    batch = Batch("b1", "COMPLICATED-LAMP", 100, eta=None)
    product = Product(batch.sku, [batch])
    uow = FakeUnitOfWork({Product: "sku"}, [product])
    result = services.allocation.allocate("o1", "COMPLICATED-LAMP", 10, uow)
    assert result == "b1"