Esempio n. 1
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)
Esempio n. 2
0
    def test_allocate_unknown_sku(self) -> None:
        desired_quantity = 10
        order_line = create_order_line("Unknown Sku", desired_quantity)

        products_repository = init_product_repository(self.sku,
                                                      self.purchased_quantity,
                                                      1)

        uow = MockedUnitOfWork(products_repository)

        event = Allocate(order_line.order_ref, order_line.sku,
                         order_line.quantity)

        with self.assertRaises(UnknownSku):
            services.allocate(event, uow)
Esempio n. 3
0
    def test_allocate_out_of_stock(self) -> None:
        desired_quantity = 20
        order_line = create_order_line(self.sku, desired_quantity)

        products_repository = init_product_repository(self.sku,
                                                      self.purchased_quantity,
                                                      1)

        uow = MockedUnitOfWork(products_repository)

        event = Allocate(order_line.order_ref, order_line.sku,
                         order_line.quantity)

        with self.assertRaises(OutOfStock):
            services.allocate(event, uow)
Esempio n. 4
0
    def test_allocate_duplicated_order_line(self) -> None:
        desired_quantity = 10
        order_line = create_order_line(self.sku, desired_quantity)

        products_repository = init_product_repository(self.sku,
                                                      self.purchased_quantity,
                                                      1)

        uow = MockedUnitOfWork(products_repository)

        event = Allocate(order_line.order_ref, order_line.sku,
                         order_line.quantity)

        services.allocate(event, uow)

        with self.assertRaises(OrderLineAlreadyAllocatedConflict):
            services.allocate(event, uow)
Esempio n. 5
0
    def test_allocate(self) -> None:
        desired_quantity = 5
        order_line = create_order_line(self.sku, desired_quantity)

        products_repository = init_product_repository(self.sku,
                                                      self.purchased_quantity,
                                                      1)

        uow = MockedUnitOfWork(products_repository)

        event = Allocate(order_line.order_ref, order_line.sku,
                         order_line.quantity)

        batchref = services.allocate(event, uow)

        product = products_repository.products[0]

        batch = next(batch for batch in product.batches
                     if batch.ref == batchref)

        self.assertEqual(self.purchased_quantity - desired_quantity,
                         batch.available_quantity)
Esempio n. 6
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