def test_id_unconsecutive_and_unordered(self, product_cls): file_obj = sentinel.file_obj product_id_seq = (3, 6, 10, 15) stub_seq = ProductFactory.stub_batch( len(product_id_seq), product_id=factory.Iterator(product_id_seq, cycle=False), ) product_seq = tuple( Product(s.product_id, s.name, s.price) for s in stub_seq) from_json = product_cls.from_json from_json.return_value.__iter__.return_value = sample( product_seq, k=len(product_id_seq), ) expected = tuple(chain( repeat(None, 3), (product_seq[0],), repeat(None, 2), (product_seq[1],), repeat(None, 3), (product_seq[2],), repeat(None, 4), (product_seq[3],), )) actual = get_products_from_json(file_obj) self.assertEqual(expected, actual)
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 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)
def setUp(self): self.product_stub_seq = ProductFactory.stub_batch(8) self.product_seq = tuple( map( ProductFactory.stub_to_obj, self.product_stub_seq, )) class LocalFractionOfPriceFactory( FractionOfPriceFactory, ): discounted_product = factory.Faker( 'random_element', elements=self.product_seq, ) class LocalFractionOfPricePerQuantityFactory( FractionOfPricePerQuantityFactory, ): trigger_product = factory.Faker( 'random_element', elements=self.product_seq, ) discounted_product = factory.Faker( 'random_element', elements=self.product_seq, ) self.special_offer_factory_cls_by_type = { SpecialOfferType.FRACTION_OF_PRICE: LocalFractionOfPriceFactory, SpecialOfferType.FRACTION_OF_PRICE_PER_QUANTITY: LocalFractionOfPricePerQuantityFactory, } self.special_offer_stub_seq_by_type = { t: f.stub_batch(4) for t, f in self.special_offer_factory_cls_by_type.items() } self.special_offer_dict_seq_by_type = { t: map( f.stub_to_dict, self.special_offer_stub_seq_by_type[t], ) for t, f in self.special_offer_factory_cls_by_type.items() } self.product_id_getter_by_special_offer_type = { SpecialOfferType.FRACTION_OF_PRICE: attrgetter("discounted_product.product_id", ), SpecialOfferType.FRACTION_OF_PRICE_PER_QUANTITY: attrgetter( "trigger_product.product_id", "discounted_product.product_id", ), }
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)
def test_success(self, product_cls): file_obj = sentinel.file_obj product_id_seq = range(1, 9) stub_seq = ProductFactory.stub_batch( len(product_id_seq), product_id=factory.Iterator(product_id_seq, cycle=False), ) product_seq = tuple( Product(s.product_id, s.name, s.price) for s in stub_seq) from_json = product_cls.from_json from_json.return_value.__iter__.return_value = product_seq expected = (None, ) + product_seq actual = get_products_from_json(file_obj) self.assertEqual(expected, actual)