def test_success(
        self,
        trigger_product_quota,
        trigger_product_quantity,
        discountable_product_quota,
        discountable_product_quantity,
        expected_num_discounts,
    ):
        quantity_seq = (
            fake.random_int(min=1, max=8),
            trigger_product_quantity,
            discountable_product_quantity,
            fake.random_int(min=1, max=8),
        )

        product_stub_seq = ProductFactory.stub_batch(
            4,
            quantity=factory.Iterator(quantity_seq, cycle=False),
        )
        product_seq = tuple(
            Product(s.product_id, s.name, s.price) for s in product_stub_seq)

        quantity_by_product = Counter(
            chain.from_iterable(
                repeat(p, s.quantity)
                for p, s in zip(product_seq, product_stub_seq)))

        trigger_product = product_seq[1]
        discounted_product = product_seq[2]

        fraction_of_price_str = f"0.{fake.random_int(min=1, max=99):02d}"
        fraction_of_price = Decimal(fraction_of_price_str)

        product_seq = (trigger_product, discounted_product)

        value_matrix = (
            FractionOfPricePerQuantityProduct(
                quantity=trigger_product_quota, ),
            FractionOfPricePerQuantityProduct(
                quantity=discountable_product_quota, ),
        )

        shared_values = FractionOfPricePerQuantityShared(
            fraction_of_price=fraction_of_price, )

        special_offer = FractionOfPricePerQuantity(
            product_seq,
            value_matrix,
            shared_values,
        )

        actual = special_offer.get_discount(quantity_by_product)

        expected_value = (expected_num_discounts * discounted_product.price *
                          (Decimal('1.00') - fraction_of_price))

        self.assertEqual(expected_value, actual.value)
Ejemplo n.º 2
0
    def test_many_products(self):
        stub_seq = ProductFactory.stub_batch(fake.random_int(2, 8))

        with io.StringIO() as file_obj:
            product_obj_seq = [
                {
                    "product_id": stub.product_id,
                    "name": stub.name,
                    "price": str(stub.price)
                }
                for stub in stub_seq
            ]
            json.dump(product_obj_seq, file_obj)
            file_obj.seek(0)

            products = tuple(Product.from_json(file_obj))

        expected = tuple(map(
            attrgetter("product_id", "name", "price"),
            stub_seq,
        ))

        actual = tuple(map(
            attrgetter("product_id", "name", "price"),
            products,
        ))

        self.assertEqual(expected, actual)
    def setUp(self):
        product_stub_seq = ProductFactory.stub_batch(
            fake.random_int(min=2, max=8), )

        self.quantity_by_product = Counter(
            chain.from_iterable(
                repeat(
                    Product(s.product_id, s.name, s.price),
                    s.quantity,
                ) for s in product_stub_seq))
    def setUp(self):
        product_stub_seq = ProductFactory.stub_batch(
            fake.random_int(min=2, max=8), )

        self.product_by_id = create_sparse_list((
            s.product_id,
            Product(
                product_id=s.product_id,
                name=s.name,
                price=s.price,
            ),
        ) for s in product_stub_seq)
Ejemplo n.º 5
0
    def test_one_product(self):
        stub = ProductFactory.stub(product_id=fake.random_int())

        with io.StringIO() as file_obj:
            product_obj = {
                "product_id": stub.product_id,
                "name": stub.name,
                "price": str(stub.price)
            }
            json.dump([product_obj], file_obj)
            file_obj.seek(0)

            product, = Product.from_json(file_obj)

        self.assertEqual(stub.product_id, product.product_id)
        self.assertEqual(stub.name, product.name)
        self.assertEqual(stub.price, product.price)
    def test_one_special_offer(self):
        trigger_product = fake.random_element(
            tuple(filter(
                partial(is_not, None),
                self.product_by_id,
            )))

        trigger_product_quota = fake.random_int(min=1, max=4)

        discounted_product = fake.random_element(
            tuple(filter(
                partial(is_not, None),
                self.product_by_id,
            )))

        discounted_product_quota = fake.random_int(min=1, max=4)

        fraction_of_price_str = f"0.{fake.random_int(min=1, max=99):02d}"
        fraction_of_price = Decimal(fraction_of_price_str)

        with io.StringIO() as file_obj:
            special_offer_obj = {
                "special_offer_type":
                "fraction_of_price_per_quantity",
                "product_matrix": [
                    [
                        trigger_product.product_id,
                        discounted_product.product_id,
                    ],
                    [
                        trigger_product_quota,
                        discounted_product_quota,
                    ],
                ],
                "shared_values": [fraction_of_price_str]
            }
            json.dump([special_offer_obj], file_obj)
            file_obj.seek(0)

            special_offer, = get_special_offers_from_json(
                file_obj,
                self.product_by_id,
            )

        self.assertEqual(trigger_product, special_offer.trigger_product)

        self.assertEqual(
            trigger_product_quota,
            special_offer.trigger_product_quantity,
        )

        self.assertEqual(
            discounted_product,
            special_offer.discounted_product,
        )

        self.assertEqual(
            discounted_product_quota,
            special_offer.discounted_product_quantity,
        )

        self.assertEqual(
            fraction_of_price,
            special_offer.fraction_of_price,
        )