示例#1
0
 def test_can_give_smart_change(self):
     machine = Machine(slots=3, slot_depth=10)
     products = {
         ProductName("coca-cola"):
         Product(name=ProductName("coca-cola"),
                 quantity=7,
                 price=Decimal('2.1')),
         ProductName("mars"):
         Product(name=ProductName("mars"), quantity=5,
                 price=Decimal('1.9')),
         ProductName("orbit"):
         Product(name=ProductName("orbit"),
                 quantity=6,
                 price=Decimal('1.4')),
     }
     machine.load_products(products)
     money = Coins({Decimal('0.2'): 3, Decimal('0.5'): 1})
     machine.load_coins(money)
     slot_code, _ = machine.get_available_products()[ProductName("orbit")]
     product, change = machine.choose_product(slot_code,
                                              Coins({Decimal(2): 1}))
     self.assertIsNotNone(product)
     self.assertIsNotNone(change)
     self.assertEqual(product.name, ProductName("orbit"))
     self.assertEqual(change, Coins({Decimal('0.2'): 3}))
示例#2
0
 def test_can_buy_available_product_and_different_change(self):
     machine = Machine(slots=3, slot_depth=10)
     products = {
         ProductName("coca-cola"):
         Product(name=ProductName("coca-cola"),
                 quantity=7,
                 price=Decimal(2.1)),
         ProductName("mars"):
         Product(name=ProductName("mars"), quantity=5, price=Decimal(1.9)),
         ProductName("orbit"):
         Product(name=ProductName("orbit"), quantity=6, price=Decimal(2.3)),
     }
     money = Coins({Decimal(2): 1, Decimal(0.5): 1})
     machine.load_products(products)
     machine.load_coins(Coins({Decimal(0.2): 2}))
     slot_code, _ = machine.get_available_products()[ProductName(
         "coca-cola")]
     product, change = machine.choose_product(slot_code, money)
     self.assertIsNotNone(product)
     self.assertIsNotNone(change)
     self.assertEqual(product.name, ProductName("coca-cola"))
     self.assertEqual(change, Coins({Decimal(0.2): 2}))
示例#3
0
 def test_loading_coins(self):
     machine = Machine(slots=2, slot_depth=10)
     money = Coins({(Decimal(1) / Decimal(5)): 5, Decimal(1): 1})
     machine.load_coins(money)
     self.assertEqual(machine.get_balance(), Decimal(2))