def setUp(self): self.discount_repository = Mock() self.discount_repository.find = Mock() discounts = { ("MANF-A", "GRADE-A"): Discount(0.3, "MANF-A", "GRADE-A"), ("MANF-B", "GRADE-A"): Discount(0.1, "MANF-B", "GRADE-A") } self.discount_repository.find = Mock(side_effect=lambda category, tier: discounts.get((category, tier))) self.pricing_service = PricingService(self.discount_repository) customers = { "Customer": CustomerFactory.build(name="Customer"), } self.customer_repository = Mock() self.customer_repository.find = Mock(side_effect=lambda sku: customers.get(sku)) tax_rate = Mock() tax_rate.rate = 0.1 self.tax_repository = Mock() self.tax_repository.find = Mock(return_value=tax_rate) prod1 = ProductFactory.build(sku="PROD001", price_category="MANF-A") prod1.set_price(PriceValueFactory.build(price=100.00)) prod2 = ProductFactory.build(sku="PROD002", price_category="MANF-B") prod2.set_price(PriceValueFactory.build(price=20.00)) self.products = { "PROD001": prod1, "PROD002": prod2, } self.product_repository = Mock() self.product_repository.find = Mock(side_effect=lambda sku: self.products.get(sku)) inv_prod1 = InventoryItemFactory.build(sku="PROD001") inv_prod1.enter_stock_on_hand(10) inv_prod2 = InventoryItemFactory.build(sku="PROD002", on_hand_buffer=7) inv_prod2.enter_stock_on_hand(10) self.inventory = { "PROD001": inv_prod1, "PROD002": inv_prod2, } self.inventory_repository = Mock() self.inventory_repository.find = Mock(side_effect=lambda sku: self.inventory.get(sku)) self.order_descriptors = { "ORD001": [ ("PROD001", 1), ("PROD002", 3), ], "ORD002": [ ("PROD001", 2), ("PROD002", 4), ] } self.order_repository = Mock() order_ids = ["ORD002", "ORD001"] self.order_repository.next_id = Mock(side_effect=lambda: order_ids.pop())
def test_discount_nonexistent(self): with self.assertRaises(PricingError): self.service.get_customer_discount(ProductFactory.build(price_category="MANF-A"), CustomerFactory.build(discount_tier="FAKE")) with self.assertRaises(PricingError): self.service.get_customer_discount(ProductFactory.build(price_category="FAKE"), CustomerFactory.build(discount_tier="GRADE-A"))
def test_product_collection_replacement_group(self): collection = ProductCollectionFactory.build(name="PROD000 Variants") master = ProductFactory.build(sku="PROD000") replacements = [ ProductFactory.build(sku="REPLACEMENT-A"), ProductFactory.build(sku="REPLACEMENT-B"), ] master.set_as_master_of_collection(collection) map(lambda p: p.join_collection(collection), replacements) self.assertEquals(2, len(collection), "Incorrect number of products in collection") self.assertIsNotNone(collection.master, "Collection master should not be set") for p in replacements: self.assertEquals(1, len(p.collections), "%s is not aware it's in a collection" % p.sku)
def test_product_collection_empty_with_master(self): collection = ProductCollectionFactory.build(name="Empty Collection") p = ProductFactory.build() p.set_as_master_of_collection(collection) self.assertEquals(0, len(collection), "Collection is not empty") self.assertIsNotNone(collection.master, "Collection Master should be set")
def test_product_current_price_with_history(self): now = datetime.datetime.now() p = ProductFactory.build( price=PriceValueFactory.build(price=100.00, date=now)) yesterday = now - datetime.timedelta(days=1) p.set_price(PriceValueFactory.build(price=150.00, date=yesterday)) last_week = now - datetime.timedelta(weeks=1) p.set_price(PriceValueFactory.build(price=130.00, date=last_week)) self.assertEquals(100.00, p.get_price(), "Current price incorrect") self.assertEquals(150.00, p.get_price(date=yesterday), "Yesterday price incorrect") self.assertEquals(150.00, p.get_price(date=now - datetime.timedelta(hours=1)), "Price an hour ago incorrect") self.assertEquals(130.00, p.get_price(date=now - datetime.timedelta(days=3)), "Price 3 days ago incorrect") # Update price again, delta should be small p.set_price( PriceValueFactory.build(price=90.00, date=datetime.datetime.now())) self.assertEquals(90.00, p.get_price(), "Updated current price incorrect") self.assertIsNone(p.get_price(date=now - datetime.timedelta(weeks=2)), "Price 2 weeks ago no undefined")
def test_customer_discount(self): product = ProductFactory.build(price_category="MANF-A") customer = CustomerFactory.build(discount_tier="GRADE-A") discount = self.service.get_customer_discount(product, customer) self.assertEquals(0.3, discount, "Wrong discount calculated") self.repo.find.assert_called_with("MANF-A", "GRADE-A")
def test_product_collection_variant_group(self): products = [ ProductFactory.build(sku="PROD000-A"), ProductFactory.build(sku="PROD000-B"), ProductFactory.build(sku="PROD000-C"), ] collection = ProductCollectionFactory.build(name="PROD000 Variants") map(lambda p: p.join_collection(collection), products) self.assertEquals(3, len(collection), "Incorrect number of products in collection") self.assertIsNone(collection.master, "Collection master should not be set") for p in products: self.assertEquals(1, len(p.collections), "%s is not aware it's in a collection" % p.sku) products[0].leave_collection(collection) self.assertEquals(2, len(collection), "Incorrect number of products in collection") self.assertEquals(0, len(products[0].collections), "PROD000-A did not leave collection")
def test_set_flag(self): p = ProductFactory.build() p.set_flag("Sale", True) p.set_flag("Hot", 1) self.assertTrue(p.get_flag("Sale"), "Sale flag was not set") self.assertTrue(p.get_flag("Hot"), "Hot flag was not set") self.assertFalse(p.get_flag("Deprecated"), "Deprecated flag should not be set") p.set_flag("Hot", False) self.assertFalse(p.get_flag("Hot"), "Hot flag was not cleared")
def test_product_current_price_with_history(self): now = datetime.datetime.now() p = ProductFactory.build(price=PriceValueFactory.build(price=100.00, date=now)) yesterday = now - datetime.timedelta(days=1) p.set_price(PriceValueFactory.build(price=150.00, date=yesterday)) last_week = now - datetime.timedelta(weeks=1) p.set_price(PriceValueFactory.build(price=130.00, date=last_week)) self.assertEquals(100.00, p.get_price(), "Current price incorrect") self.assertEquals(150.00, p.get_price(date=yesterday), "Yesterday price incorrect") self.assertEquals(150.00, p.get_price(date=now - datetime.timedelta(hours=1)), "Price an hour ago incorrect") self.assertEquals(130.00, p.get_price(date=now - datetime.timedelta(days=3)), "Price 3 days ago incorrect") # Update price again, delta should be small p.set_price(PriceValueFactory.build(price=90.00, date=datetime.datetime.now())) self.assertEquals(90.00, p.get_price(), "Updated current price incorrect") self.assertIsNone(p.get_price(date=now - datetime.timedelta(weeks=2)), "Price 2 weeks ago no undefined")
def test_product_creation(self): p = ProductFactory.build() self.assertIsNotNone(p.sku, "Product was created without an SKU")
def test_product_price_non_value_type(self): ProductFactory.build(price=100.00)
def test_product_current_price(self): p = ProductFactory.build(price=PriceValueFactory.build(price=100.00)) self.assertEquals(100.00, p.get_price(), "Current price incorrect")
def test_product_must_have_price(self): ProductFactory.build(price=None)
def setUp(self): self.discount_repository = Mock() self.discount_repository.find = Mock() discounts = { ("MANF-A", "GRADE-A"): Discount(0.3, "MANF-A", "GRADE-A"), ("MANF-B", "GRADE-A"): Discount(0.1, "MANF-B", "GRADE-A") } self.discount_repository.find = Mock( side_effect=lambda category, tier: discounts.get((category, tier))) self.pricing_service = PricingService(self.discount_repository) customers = { "Customer": CustomerFactory.build(name="Customer"), } self.customer_repository = Mock() self.customer_repository.find = Mock( side_effect=lambda sku: customers.get(sku)) tax_rate = Mock() tax_rate.rate = 0.1 self.tax_repository = Mock() self.tax_repository.find = Mock(return_value=tax_rate) prod1 = ProductFactory.build(sku="PROD001", price_category="MANF-A") prod1.set_price(PriceValueFactory.build(price=100.00)) prod2 = ProductFactory.build(sku="PROD002", price_category="MANF-B") prod2.set_price(PriceValueFactory.build(price=20.00)) self.products = { "PROD001": prod1, "PROD002": prod2, } self.product_repository = Mock() self.product_repository.find = Mock( side_effect=lambda sku: self.products.get(sku)) inv_prod1 = InventoryItemFactory.build(sku="PROD001") inv_prod1.enter_stock_on_hand(10) inv_prod2 = InventoryItemFactory.build(sku="PROD002", on_hand_buffer=7) inv_prod2.enter_stock_on_hand(10) self.inventory = { "PROD001": inv_prod1, "PROD002": inv_prod2, } self.inventory_repository = Mock() self.inventory_repository.find = Mock( side_effect=lambda sku: self.inventory.get(sku)) self.order_descriptors = { "ORD001": [ ("PROD001", 1), ("PROD002", 3), ], "ORD002": [ ("PROD001", 2), ("PROD002", 4), ] } self.order_repository = Mock() order_ids = ["ORD002", "ORD001"] self.order_repository.next_id = Mock( side_effect=lambda: order_ids.pop())