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 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_add_contact_phone_non_enum(self): customer = CustomerFactory.build(name="Customer") contact = ContactFactory.build() contact.add_phone("FOO", "1111111") customer.add_contact(contact) self.repository.store(customer)
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_add_contact(self): customer = CustomerFactory.build(name="Customer") self.repository.store(customer) contact = ContactFactory.build() contact.add_role("SALES") contact.add_phone("OFFICE", "+6129000000") customer.add_contact(contact)
def test_add_customer(self): customer = CustomerFactory.build(name="Customer") self.repository.store(customer) c = self.repository.find("Customer") self.assertIsNotNone(c) self.assertEquals("Customer", c.name)
def test_customer_sales_history(self): customer = CustomerFactory.build() customer.submit_order("ORD001") customer.submit_order("ORD002") customer.submit_invoice("INV001") self.assertTrue("ORD001" in customer.orders, "ORD001 not found in customer sales history") self.assertTrue("ORD002" in customer.orders, "ORD002 not found in customer sales history") self.assertTrue("INV001" in customer.invoices, "INV001 not found in customer sales history") self.assertFalse("INVXXX" in customer.invoices, "Non-existant invoice found in customer sales history")
def test_customer_addresses(self): customer = CustomerFactory.build() shipping = AddressFactory.build(type="SHIPPING") billing = AddressFactory.build(type="BILLING") customer.add_address(shipping) customer.add_address(billing) customer.add_address("1 Main St, City") shipping_addresses = customer.get_addresses("SHIPPING") billing_addresses = customer.get_addresses("BILLING") self.assertEquals(1, len(shipping_addresses), "No shipping addresses found") self.assertEquals(1, len(billing_addresses), "No billing addresses found")
def test_add_address(self): customer = CustomerFactory.build(name="Customer") self.repository.store(customer) billing_address = AddressFactory.build(type="BILLING") customer.add_address(billing_address) shipping_address = AddressFactory.build(type="SHIPPING") customer.add_address(shipping_address) c = self.repository.find("Customer") a = c.get_addresses("BILLING") self.assertIsNotNone(c) self.assertEquals(1, len(a), "Customer should only have 1 billing address") self.assertEqual(billing_address, a[0], "Billing addresses don't match") a = c.get_addresses("SHIPPING") self.assertEquals(1, len(a), "Customer should only have 1 shipping addresses") self.assertEqual(shipping_address, a[0], "Shipping addresses don't match")
def test_customer_one_contact_per_role(self): customer = CustomerFactory.build() sales_contact = ContactFactory.build(firstname="Sales") sales_contact.add_role("SALES") acct_contact = ContactFactory.build(firstname="Accounts") acct_contact.add_role("ACCOUNTS") customer.add_contact(sales_contact) customer.add_contact(acct_contact) customer.add_contact("XXX") self.assertEquals(2, len(customer.contacts), "Wrong number of contacts added to customer") sales_contacts = customer.get_contacts("SALES") self.assertEquals(1, len(sales_contacts), "Could not find any sales contacts") self.assertEquals("Sales", sales_contacts[0].firstname, "Found wrong sales contacts") acct_contacts = customer.get_contacts("ACCOUNTS") self.assertEquals(1, len(acct_contacts), "Could not find any accounts contacts") self.assertEquals("Accounts", acct_contacts[0].firstname, "Found wrong accounts contacts")
def setUp(self): customer = CustomerFactory.build(name="Customer") customer.orders = ["ORD001", "ORD002", "ORD00X"] self.customer_repository = Mock() self.customer_repository.find = Mock(return_value=customer) tax_rate = Mock() tax_rate.rate = 0.1 self.tax_repository = Mock() self.tax_repository.find = Mock(return_value=tax_rate) order1 = OrderFactory.build(order_id="ORD001", customer_reference="CUST-PO001") order1.customer = "Customer" order1.add_line_item("PROD001", 1, 100.00, 0.10) order1.add_line_item("PROD002", 3, 10.00, 0.00) order1.is_acknowledged = Mock(return_value=True) order2 = OrderFactory.build(order_id="ORD002", customer_reference="CUST-PO002") order2.customer = "Customer" order2.add_line_item("PROD001", 2, 100.00, 0.10) order2.add_line_item("PROD002", 4, 10.00, 0.00) order2.is_acknowledged = Mock(return_value=True) order3 = OrderFactory.build(order_id="ORD003", customer_reference="CUST-PO003") order3.customer = "Customer" order3.add_line_item("PROD004", 1, 100.00, 0.10) order3.is_acknowledged = Mock(return_value=True) orderx = OrderFactory.build(order_id="ORD00X", customer_reference="CUST-PO00X") orderx.customer = "Fake Customer" orderx.is_acknowledged = Mock(return_value=False) self.orders = { "ORD001": order1, "ORD002": order2, "ORD003": order3, "ORD00X": orderx, # An unacknowledged order with bad customer } self.order_repository = Mock() self.order_repository.find = Mock(side_effect=lambda order_id: self.orders.get(order_id)) prod1 = InventoryItemFactory.build(sku="PROD001") prod1.enter_stock_on_hand(10) prod1.commit(1, "ORD001") prod1.commit(2, "ORD002") prod2 = InventoryItemFactory.build(sku="PROD002") prod2.enter_stock_on_hand(10) prod2.commit(3, "ORD001") prod2.commit(4, "ORD002") # Product with no commitments prod3 = InventoryItemFactory.build(sku="PROD003") prod3.enter_stock_on_hand(10) # Always failing commitment fulfillment prod4 = InventoryItemFactory.build(sku="PROD004") prod4.enter_stock_on_hand(10) prod4.commit(3, "ORD003") prod4.fulfill_commitment = Mock(side_effect=lambda x, y, z: False) inventory = { "PROD001": prod1, "PROD002": prod2, "PROD003": prod3, "PROD004": prod4, } self.inventory_repository = Mock() self.inventory_repository.find = Mock(side_effect=lambda sku: inventory.get(sku)) self.invoice_repository = Mock() invoice_ids = ["INV002", "INV001"] self.invoice_repository.next_id = Mock(side_effect=lambda: invoice_ids.pop()) self.order_descriptors = { "ORD001": [ {"sku": "PROD001", "quantity": 1}, {"sku": "PROD002", "quantity": 3} ], "ORD002": [ {"sku": "PROD001", "quantity": 2}, {"sku": "PROD002", "quantity": 2}, {"sku": "PROD002", "quantity": 2} ], "ORD003": [ {"sku": "PROD004", "quantity": 3}, ], "ORD00X": [ {"sku": "PROD001", "quantity": 1}, ] }
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())