| 
          import unittest | 
        
        
           | 
          import random | 
        
        
           | 
          
 | 
        
        
           | 
          from checkout import * | 
        
        
           | 
          from items import * | 
        
        
           | 
          from discounts import * | 
        
        
           | 
          
 | 
        
        
           | 
          
 | 
        
        
           | 
          class DiscountTests(unittest.TestCase): | 
        
        
           | 
              def testDiscountOnNonCheckoutItem(self): | 
        
        
           | 
                  discount = ItemDiscount(amount=4.00, op="price_abs") | 
        
        
           | 
                  self.assertRaises(AttributeError, lambda: discount.calc("string")) | 
        
        
           | 
          
 | 
        
        
           | 
              def testDiscountWithNoMinOrCodeAlwaysApplied(self): | 
        
        
           | 
                  item = CheckoutItem("CODE", 1, 5.00) | 
        
        
           | 
                  discount = ItemDiscount(amount=4.00, op="price_abs") | 
        
        
           | 
                  self.assertNotEqual(item.linetotal(), discount.calc(item)) | 
        
        
           | 
          
 | 
        
        
           | 
              def testDiscountAppliedToAnyItemIfNoCode(self): | 
        
        
           | 
                  item1 = CheckoutItem("ITEM1", 1, 5.00) | 
        
        
           | 
                  item2 = CheckoutItem("ITEM2", 1, 5.00) | 
        
        
           | 
                  discount = ItemDiscount(amount=4.00, op="price_abs") | 
        
        
           | 
                  self.assertNotEqual(item1.linetotal(), discount.calc(item1)) | 
        
        
           | 
                  self.assertNotEqual(item2.linetotal(), discount.calc(item2)) | 
        
        
           | 
          
 | 
        
        
           | 
              def testDiscountAppliedToSpecificItemWhenCodeSupplied(self): | 
        
        
           | 
                  item1 = CheckoutItem("ITEM1", 1, 5.00) | 
        
        
           | 
                  item2 = CheckoutItem("ITEM12", 1, 5.00) | 
        
        
           | 
                  discount = ItemDiscount(amount=.9, code="ITEM1") | 
        
        
           | 
                  self.assertNotEqual(item1.linetotal(), discount.calc(item1)) | 
        
        
           | 
                  self.assertEqual(item2.linetotal(), discount.calc(item2)) | 
        
        
           | 
          
 | 
        
        
           | 
              def testDiscountHasNoEffectsOnItemPassed(self): | 
        
        
           | 
                  item = CheckoutItem("CODE", 1, 5.00) | 
        
        
           | 
                  discount = ItemDiscount(amount=.9) | 
        
        
           | 
                  linetotal_original = item.linetotal() | 
        
        
           | 
                  discount_total = discount.calc(item) | 
        
        
           | 
                  linetotal_after_discount = item.linetotal() | 
        
        
           | 
                  self.assertEqual(linetotal_original, linetotal_after_discount) | 
        
        
           | 
          
 | 
        
        
           | 
              def testDiscountOnQuantityApplied(self): | 
        
        
           | 
                  item = CheckoutItem("CODE", 2, 5.00) | 
        
        
           | 
                  discount = ItemDiscount(amount=2.00, op="qty_bulkdiscount") | 
        
        
           | 
                  self.assertEqual(item.price, discount.calc(item)) | 
        
        
           | 
          
 | 
        
        
           | 
              def testDiscountOnQuantityForSpecificItemCodeApplied(self): | 
        
        
           | 
                  item1 = CheckoutItem("ITEM1", 3, 5.00) | 
        
        
           | 
                  item2 = CheckoutItem("ITEM2", 4, 5.00) | 
        
        
           | 
                  discount = ItemDiscount(amount=2, code="ITEM1", | 
        
        
           | 
                                          op="qty_bulkdiscount") | 
        
        
           | 
                  self.assertEqual(item1.price*2, discount.calc(item1)) | 
        
        
           | 
                  self.assertNotEqual(item1.linetotal(), discount.calc(item1)) | 
        
        
           | 
                  self.assertEqual(item2.linetotal(), discount.calc(item2)) | 
        
        
           | 
          
 | 
        
        
           | 
              def testDiscountPerItemPricePercent(self): | 
        
        
           | 
                  item = CheckoutItem("CODE", 1, 5.00) | 
        
        
           | 
                  discount = ItemDiscount(amount=.9) | 
        
        
           | 
                  self.assertNotEqual(item.linetotal(), discount.calc(item)) | 
        
        
           | 
                  self.assertEqual(item.linetotal()*.9, discount.calc(item)) | 
        
        
           | 
          
 | 
        
        
           | 
              def testDiscountPerItemPriceAbsolute(self): | 
        
        
           | 
                  item = CheckoutItem("CODE", 4, 5.00) | 
        
        
           | 
                  discount = ItemDiscount(amount=1, op="price_abs") | 
        
        
           | 
                  self.assertNotEqual(item.linetotal(), discount.calc(item)) | 
        
        
           | 
                  self.assertEqual(item.linetotal()-4, discount.calc(item)) | 
        
        
           | 
          
 | 
        
        
           | 
          
 | 
        
        
           | 
          class InterviewTests(unittest.TestCase): | 
        
        
           | 
              def setUp(self): | 
        
        
           | 
                  self.checkout = Checkout() | 
        
        
           | 
          
 | 
        
        
           | 
              def testAddSingleStrawberryItemCorrectTotal(self): | 
        
        
           | 
                  item = Strawberry() | 
        
        
           | 
                  self.checkout.addItem(item) | 
        
        
           | 
                  self.assertEqual(item.price, self.checkout.total()) | 
        
        
           | 
          
 | 
        
        
           | 
              def testAddSingleItemCorrectTotal(self): | 
        
        
           | 
                  item = CheckoutItem(code="ST1", price=5) | 
        
        
           | 
                  self.checkout.registerOffer(ItemDiscount(code="ST1", amount=.5, min=3, | 
        
        
           | 
                                                           op="price_abs")) | 
        
        
           | 
                  self.checkout.addItem(item) | 
        
        
           | 
                  self.assertEqual(item.price, self.checkout.total()) | 
        
        
           | 
                  self.checkout.addItem(item) | 
        
        
           | 
                  self.checkout.addItem(item) | 
        
        
           | 
                  self.assertEqual(13.5, self.checkout.total()) | 
        
        
           | 
          
 | 
        
        
           | 
              def testMultiDiscountLeadsToLowestTotal(self): | 
        
        
           | 
                  # setup items | 
        
        
           | 
                  item1 = CheckoutItem(code="ST1", price=5) | 
        
        
           | 
                  item2 = CheckoutItem(code="FT1", price=2.5) | 
        
        
           | 
          
 | 
        
        
           | 
                  # setup competing offers | 
        
        
           | 
                  self.checkout.registerOffer(ItemDiscount(code="ST1", amount=.8, min=2, | 
        
        
           | 
                                                           op="price")) | 
        
        
           | 
                  self.checkout.registerOffer(ItemDiscount(code="ST1", amount=.5, min=3, | 
        
        
           | 
                                                           op="price_abs")) | 
        
        
           | 
          
 | 
        
        
           | 
                  # initial setup and checks | 
        
        
           | 
                  self.checkout.addItem(item1) | 
        
        
           | 
                  self.assertEqual(item1.price, self.checkout.total()) | 
        
        
           | 
                  self.checkout.addItem(item1) | 
        
        
           | 
                  self.checkout.addItem(item1) | 
        
        
           | 
          
 | 
        
        
           | 
                  # verify cart total | 
        
        
           | 
                  self.assertEqual(12, self.checkout.total()) | 
        
        
           | 
          
 | 
        
        
           | 
                  # start with new checkout object to confirm same result | 
        
        
           | 
                  self.checkout = Checkout() | 
        
        
           | 
                  self.checkout.registerOffer(ItemDiscount(code="ST1", amount=.8, min=2, | 
        
        
           | 
                                                           op="price")) | 
        
        
           | 
                  self.checkout.addItem(item1) | 
        
        
           | 
                  self.checkout.addItem(item1) | 
        
        
           | 
                  self.checkout.addItem(item1) | 
        
        
           | 
                  self.assertEqual(12, self.checkout.total()) | 
        
        
           | 
          
 | 
        
        
           | 
              def testAddSingleFruitTeaItemCorrectTotal(self): | 
        
        
           | 
                  item = FruitTea() | 
        
        
           | 
                  self.checkout.addItem(item) | 
        
        
           | 
                  self.assertEqual(item.price, self.checkout.total()) | 
        
        
           | 
          
 | 
        
        
           | 
              def testAddSingleCoffeeItemCorrectTotal(self): | 
        
        
           | 
                  item = Coffee() | 
        
        
           | 
                  self.checkout.addItem(item) | 
        
        
           | 
                  self.assertEqual(item.price, self.checkout.total()) | 
        
        
           | 
          
 | 
        
        
           | 
              def testAddMultipleStrawberrySingleObjCorrectTotal(self): | 
        
        
           | 
                  item = Strawberry(2) | 
        
        
           | 
                  self.checkout.addItem(item) | 
        
        
           | 
                  self.assertEqual((item.price * 2), self.checkout.total()) | 
        
        
           | 
          
 | 
        
        
           | 
              def testAddMultipleStrawberryMultiObjCorrectTotal(self): | 
        
        
           | 
                  item = Strawberry(2) | 
        
        
           | 
                  self.checkout.addItem(item) | 
        
        
           | 
                  self.checkout.addItem(Strawberry()) | 
        
        
           | 
                  self.checkout.addItem(Strawberry()) | 
        
        
           | 
                  self.assertEqual((item.offer_price * 4), self.checkout.total()) | 
        
        
           | 
          
 | 
        
        
           | 
              def testAddMultipleStrawberryOfferCorrectTotal(self): | 
        
        
           | 
                  item = Strawberry(3) | 
        
        
           | 
                  self.checkout.addItem(item) | 
        
        
           | 
                  self.assertEqual((item.offer_price * 3), self.checkout.total()) | 
        
        
           | 
          
 | 
        
        
           | 
              def testAddSingleFruitTeaItemCorrectTotal(self): | 
        
        
           | 
                  item = FruitTea() | 
        
        
           | 
                  self.checkout.addItem(item) | 
        
        
           | 
                  self.assertEqual(item.price, self.checkout.total()) | 
        
        
           | 
          
 | 
        
        
           | 
              def testAddMultiFruitTeaSingleObjCorrectTotalWithOffer(self): | 
        
        
           | 
                  item = FruitTea(2) | 
        
        
           | 
                  self.checkout.addItem(item) | 
        
        
           | 
                  self.assertEqual(item.price, self.checkout.total()) | 
        
        
           | 
          
 | 
        
        
           | 
              def testAddMultiFruitTeaMultiObjCorrectTotalWithOffer(self): | 
        
        
           | 
                  item = FruitTea(2) | 
        
        
           | 
                  self.checkout.addItem(item) | 
        
        
           | 
                  self.checkout.addItem(FruitTea()) | 
        
        
           | 
                  self.assertEqual(item.price*2, self.checkout.total()) | 
        
        
           | 
                  self.checkout.addItem(FruitTea()) | 
        
        
           | 
                  self.assertEqual(item.price*2, self.checkout.total()) | 
        
        
           | 
          
 | 
        
        
           | 
              def testAddSingleCoffeeItemCorrectTotal(self): | 
        
        
           | 
                  item = Coffee() | 
        
        
           | 
                  self.checkout.addItem(item) | 
        
        
           | 
                  self.assertEqual(item.price, self.checkout.total()) | 
        
        
           | 
          
 | 
        
        
           | 
              def testInterview1(self): | 
        
        
           | 
                  strawberry = Strawberry() | 
        
        
           | 
                  fruit_tea = FruitTea() | 
        
        
           | 
                  coffee = Coffee() | 
        
        
           | 
                  self.checkout.addItem(strawberry) | 
        
        
           | 
                  self.checkout.addItem(fruit_tea) | 
        
        
           | 
                  self.checkout.addItem(strawberry) | 
        
        
           | 
                  self.checkout.addItem(fruit_tea) | 
        
        
           | 
                  self.checkout.addItem(fruit_tea) | 
        
        
           | 
                  self.checkout.addItem(coffee) | 
        
        
           | 
                  self.assertEqual( | 
        
        
           | 
                      (strawberry.price * 2) + (fruit_tea.price * 2) + (coffee.price), | 
        
        
           | 
                      self.checkout.total()) | 
        
        
           | 
          
 | 
        
        
           | 
          
 | 
        
        
           | 
          if __name__ == "__main__": | 
        
        
           | 
              unittest.main(verbosity=2) |