def test_allocate(self):
        sku = "Red shoes"

        batch = create_batch(sku, 10)

        order_line = create_order_line(sku, 1)

        product = Product(sku, [batch])

        batchref = product.allocate(order_line)

        self.assertEqual(batchref, batch)
    def test_allocate_out_of_stock(self):
        sku = "Red shoes"

        batch = create_batch(sku, 10)

        order_line = create_order_line(sku, 20)

        product = Product(sku, [batch])

        with self.assertRaises(OutOfStockError):
            batchref = product.allocate(order_line)

        self.assertEqual(product.events[-1], events.OutOfStock(sku=sku))
    def test_add_product(self):
        new_sku = "Black pants"

        product = Product(new_sku, [])
        self.repository.add(product)
        retrieved_product = self.repository.get(new_sku)
        self.assertEqual(product, retrieved_product)
Exemplo n.º 4
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.º 5
0
def init_product_repository(sku, purchased_quantity,
                            batches_number) -> MockedRepository:
    batches = [
        create_batch(sku, purchased_quantity)
        for i in range(0, batches_number)
    ]
    product = Product(sku, batches)
    return MockedRepository((product, ))
Exemplo n.º 6
0
    def test_allocate_order_line_on_product(self):
        session = OrmMappingIntegrationTestCase.get_session()
        sku = 'RED-CHAIR1'
        batch = Batch("batch-1", sku, 20, date.today())
        order_line = OrderLine("order1", sku, 12)

        product = Product(sku=sku, batches=[batch])

        product.allocate(order_line)

        session.add(product)

        loaded_product = session.query(Product).get(sku)

        loaded_batch = loaded_product.batches[0]

        self.assertEqual(batch, loaded_batch)

        self.assertEqual(order_line, loaded_batch.allocated_order_lines[0])
Exemplo n.º 7
0
    def test_store_and_retrieve_product_with_batches(self):
        session = OrmMappingIntegrationTestCase.get_session()
        sku = 'RED-CHAIR'
        batch = Batch(str(uuid.uuid4()), sku, 20, date.today())

        product = Product(sku=sku, batches=[batch])

        session.add(product)

        loaded_product = session.query(Product).get(sku)

        self.assertEqual(product, loaded_product)

        self.assertEqual(product.batches, [batch])
    def setUp(self) -> None:
        self.refs = ["batch-1", "batch-2", "batch-3"]
        self.skus = ["Red Socks", "Blue pants", "Black chair"]
        self.products = []

        for sku in self.skus:
            batches = [
                Batch(
                    ref=f'{sku}_{ref}',
                    sku=sku,
                    purchased_quantity=random.randrange(0, 100, 1),
                    eta=date.today(),
                ) for ref in self.refs
            ]

            self.products.append(Product(sku, batches))

        self.repository = MockedRepository(tuple(self.products))
Exemplo n.º 9
0
def add_batch(event: CreateBatch, uow: AbstractUnitOfWork) -> str:
    ref = event.ref
    sku = event.sku
    quantity = event.qty
    eta = event.eta

    new_batch = Batch(ref=ref, sku=sku, purchased_quantity=quantity, eta=eta)

    with uow:
        try:
            product = uow.products.get(sku)
        except UnknownSkuError:
            product = Product(sku=sku)
            uow.products.add(product)

        product.batches.append(new_batch)

    return new_batch.ref
Exemplo n.º 10
0
    def test_add_product(self):
        session = RepositoryTestCase.get_session()

        repository = SQLAlchemyRepository(session)

        product = Product('NEW-SKU')

        repository.add(product)

        rows = list(
            session.execute(
                "SELECT sku FROM products WHERE sku = 'NEW-SKU'"
            )
        )

        self.assertEqual(len(rows), 1)

        self.assertEqual(rows[0]['sku'], product.sku)

        session.execute('DELETE FROM products where sku ="NEW-SKU"')