Exemplo n.º 1
0
    def test_decrease_quantity_reallocation_needed(self):
        allocated_quantity = 20

        # We need first to allocate the orders
        allocation_events = [
            batch_allocation.domain.commands.Allocate(
                order_ref=str(uuid.uuid4()),
                sku=self.sku,
                qty=int(allocated_quantity / 2)),
            batch_allocation.domain.commands.Allocate(
                order_ref=str(uuid.uuid4()),
                sku=self.sku,
                qty=int(allocated_quantity / 2))
        ]

        allocated_events_count = len(allocation_events)

        for event in allocation_events:
            messagebus.handle(event, self.uow)

        changed_batch = self.uow.products.get(self.sku).get_batch(
            self.batchref_to_be_changed)

        # The quantity that is allocated is correct
        self.assertEqual(allocated_quantity, changed_batch.allocated_quantity)
        # All the order lines are allocated to the same batch
        self.assertEqual(allocated_events_count,
                         len(changed_batch.allocated_order_lines))

        # Now we can test our quantity change
        new_quantity = 10

        allocation_change_event = batch_allocation.domain.commands.ChangeBatchQuantity(
            ref=self.batchref_to_be_changed, sku=self.sku, qty=new_quantity)

        messagebus.handle(allocation_change_event, self.uow)

        changed_batch = self.uow.products.get(self.sku).get_batch(
            self.batchref_to_be_changed)

        # The quantity that is allocated is correct
        self.assertEqual(new_quantity, changed_batch.purchased_quantity)

        self.assertEqual(1, len(changed_batch.allocated_order_lines))

        self.assertEqual(new_quantity - int(allocated_quantity / 2),
                         changed_batch.available_quantity)

        second_batch = self.uow.products.get(self.sku).get_batch(
            self.other_batchref)

        self.assertEqual(1, len(second_batch.allocated_order_lines))

        self.assertEqual(int(allocated_quantity / 2),
                         second_batch.allocated_quantity)
Exemplo n.º 2
0
    def test_add_batch_for_existing_product(self):
        ref = str(uuid.uuid4())
        sku = "New Shiny Product"
        quantity = 10

        product = Product(sku=sku)

        mocked_repo = MockedRepository((product, ))
        mocked_uow = MockedUnitOfWork(mocked_repo)

        event = CreateBatch(ref=ref, sku=sku, qty=quantity, eta=None)

        batchref = messagebus.handle(event, mocked_uow)[0]

        self.assertEqual(ref, batchref)

        self.assertEqual(len(mocked_repo.products), 1)

        repo_product = mocked_repo.products[0]
        self.assertEqual(product, repo_product)

        self.assertEqual(len(repo_product.batches), 1)

        new_batch = repo_product.batches[0]
        self.assertEqual(new_batch.ref, batchref)
Exemplo n.º 3
0
    def test_decrease_quantity_no_re_allocation(self):
        new_quantity = 5
        event = batch_allocation.domain.commands.ChangeBatchQuantity(
            sku=self.sku, ref=self.batchref_to_be_changed, qty=new_quantity)

        results = messagebus.handle(event, self.uow)

        self.assertGreater(len(results), 0)
        self.assertEqual(new_quantity, results[0])

        product = self.uow.products.get(self.sku)
        self.assertEqual(product.batches[0].available_quantity, new_quantity)
Exemplo n.º 4
0
def allocate_endpoint(order_line_dto: OrderLineDTO):
    uow = UnitOfWork()
    event = Allocate(
        order_ref=order_line_dto.order_ref,
        sku=order_line_dto.sku,
        qty=order_line_dto.quantity
    )

    try:
        results = messagebus.handle(event, uow)
        return BatchRefResponse(batchref=results.pop(0))
    except services.OutOfStock:
        raise HTTPException(status_code=400, detail="Out of stock")
Exemplo n.º 5
0
    def setUp(self) -> None:
        batchref_to_be_changed = str(uuid.uuid4())
        other_batchref = str(uuid.uuid4())
        sku = 'Red Shoes'
        original_quantity = 20

        mocked_repo = MockedRepository(())
        mocked_unit_of_work = MockedUnitOfWork(mocked_repo)

        events_history = [
            batch_allocation.domain.commands.CreateBatch(
                ref=batchref_to_be_changed, sku=sku, qty=original_quantity),
            batch_allocation.domain.commands.CreateBatch(
                ref=other_batchref, sku=sku, qty=original_quantity),
        ]

        for event in events_history:
            messagebus.handle(event, uow=mocked_unit_of_work)

        self.uow = mocked_unit_of_work
        self.batchref_to_be_changed = batchref_to_be_changed
        self.other_batchref = other_batchref
        self.sku = sku
        self.original_quantity = original_quantity