Ejemplo n.º 1
0
 def test_for_existing_product(self, messagebus: MessageBus):
     messagebus.handle(commands.CreateBatch("b1", "GARISH-RUG", 100, None))
     messagebus.handle(commands.CreateBatch("b2", "GARISH-RUG", 99, None))
     assert "b2" in [
         b.reference
         for b in messagebus.uow[Product].get("GARISH-RUG").items
     ]
Ejemplo n.º 2
0
    def test_changes_available_quantity(self, messagebus: MessageBus):
        messagebus.handle(
            commands.CreateBatch("batch1", "ADORABLE-SETTEE", 100, None))
        [batch] = messagebus.uow[Product].get("ADORABLE-SETTEE").items
        assert batch.available_quantity == 100

        messagebus.handle(commands.ChangeBatchQuantity("batch1", 50))

        assert batch.available_quantity == 50
Ejemplo n.º 3
0
def post_allocate_batch(req: BatchAllocateSchema):
    """``POST /allocate`` 엔트포인트 요청을 처리합니다."""
    try:
        event = commands.Allocate(req.orderid, req.sku, req.qty)
        results = messagebus.handle(event)
        return {"batchref": results.pop(0)}
    except InvalidSku as e:
        return {"batchref": None, "error": "InvalidSku"}
Ejemplo n.º 4
0
    def test_reallocates_if_necessary(self, messagebus: MessageBus):
        message_history = [
            commands.CreateBatch("batch1", "INDIFFERENT-TABLE", 50, None),
            commands.CreateBatch("batch2", "INDIFFERENT-TABLE", 50,
                                 datetime.today()),
            commands.Allocate("order1", "INDIFFERENT-TABLE", 20),
            commands.Allocate("order2", "INDIFFERENT-TABLE", 20),
        ]

        for e in message_history:
            messagebus.handle(e)

        [batch1,
         batch2] = messagebus.uow[Product].get("INDIFFERENT-TABLE").items
        assert batch1.available_quantity == 10
        assert batch2.available_quantity == 50

        messagebus.handle(commands.ChangeBatchQuantity("batch1", 25))

        # order1 or order2 will be deallocated and, so we'll have 25 - 20
        assert batch1.available_quantity == 5
        # and 20 will be reallocated to the next batch
        assert batch2.available_quantity == 30
Ejemplo n.º 5
0
def add_batch(batch: BatchAddSchema):
    """``POST /batches`` 요청을 처리하여 새로운 배치를 저장소에 추가합니다."""
    event = commands.CreateBatch(batch.ref, batch.sku, batch.qty, batch.eta)
    messagebus.handle(event)
Ejemplo n.º 6
0
 def test_commits(self, messagebus: MessageBus):
     messagebus.handle(
         commands.CreateBatch("b1", "OMINOUS-MIRROR", 100, None))
     messagebus.handle(commands.Allocate("o1", "OMINOUS-MIRROR", 10))
     assert messagebus.uow.committed
Ejemplo n.º 7
0
    def test_errors_for_invalid_sku(self, messagebus: MessageBus):
        messagebus.handle(commands.CreateBatch("b1", "AREALSKU", 100, None))

        with pytest.raises(allocation.InvalidSku,
                           match="Invalid sku NONEXISTENTSKU"):
            messagebus.handle(commands.Allocate("o1", "NONEXISTENTSKU", 10))
Ejemplo n.º 8
0
 def test_returns_allocation(self, messagebus: MessageBus):
     messagebus.handle(
         commands.CreateBatch("batch1", "COMPLICATED-LAMP", 100, None))
     result = messagebus.handle(
         commands.Allocate("o1", "COMPLICATED-LAMP", 10))
     assert ["batch1"] == result
Ejemplo n.º 9
0
 def test_for_new_product(self, messagebus: MessageBus):
     messagebus.handle(
         commands.CreateBatch("b1", "CRUNCHY-ARMCHAIR", 100, None))
     assert messagebus.uow[Product].get("CRUNCHY-ARMCHAIR") is not None
     assert messagebus.uow.committed
Ejemplo n.º 10
0
def on_change_batch_quantity(client: AbstractPubsubClient, data: dict[str,
                                                                      Any]):
    cmd = commands.ChangeBatchQuantity(ref=data["batchref"], qty=data["qty"])
    messagebus.handle(cmd)