def test_make_change(self): # give 100+2x25+2x10+2x5 # price 85, 90, 95 => impossible to give back change for price in [ 5, 10, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170, 175, 180 ]: rack = Rack("A", "Biscuit", price) machine = Machine([rack], 0) machine.refill("A", 1) machine.insert(Coin.DOLLAR) machine.insert(Coin.QUARTER) machine.insert(Coin.QUARTER) machine.insert(Coin.DIME) machine.insert(Coin.DIME) machine.insert(Coin.NICKEL) machine.insert(Coin.NICKEL) self.assertEqual(machine.amount, 180) machine.press("A") if price not in [85, 90, 95]: self.assertEqual( sum([k.value * v for k, v in machine.coins.items()]), price) self.assertEqual(machine.racks['A'].quantity, 0) else: self.assertEqual( sum([k.value * v for k, v in machine.coins.items()]), 0) self.assertEqual(machine.racks['A'].quantity, 1) self.assertEqual(machine.amount, 0)
def test_can_refill_biscuits(self): racks = [Rack("A", "Biscuits", 100)] machine = Machine(racks) machine.refill("A", 5) # machine.racks: A => Rack, B => Rack self.assertEqual(machine.racks["A"].quantity, 5)
def test_buy_biscuit_no_quantity(self): rack = Rack("A", "Biscuit", 100) machine = Machine([rack]) for i in range(4): machine.insert(Coin.QUARTER) machine.press("A") self.assertEqual(machine.racks['A'].quantity, 0) self.assertEqual(machine.coins[Coin.QUARTER], 0)
def test_user_can_buy_item_A(self): racks = [Rack("A", "Biscuits", 100)] machine = Machine(racks) machine.refill("A", 1) machine.insertCoin(Coin.DOLLAR) machine.pressButton("A") self.assertEqual(machine.racks["A"].quantity, 0) self.assertEqual(machine.coins[Coin.DOLLAR], 1)
def test_buy_biscuit_not_enough_coins(self): rack = Rack("A", "Biscuit", 100) machine = Machine([rack]) machine.refill("A", 1) for i in range(3): machine.insert(Coin.QUARTER) machine.press("A") self.assertEqual(machine.racks['A'].quantity, 1) self.assertEqual(machine.coins[Coin.QUARTER], 3)
def test_user_get_its_change_back(self): racks = [Rack("C", "", 85)] machine = Machine(racks, 10) # Ten coins each machine.refill("C", 1) machine.insert(Coin.DOLLAR) outcome = machine.press("C") self.assertTrue(outcome) self.assertEqual(machine.coins[Coin.DIME], 9) self.assertEqual(machine.coins[Coin.NICKEL], 9)
def test_change_return_after_purchase(self): racks = [Rack("A", "Biscuits", 100)] machine = Machine(racks) machine.refill("A", 1) for i in range(5): machine.insertCoin(Coin.QUARTER) machine.pressButton("A") self.assertEqual(machine.coins[Coin.QUARTER], 4)
def test_user_can_buy_item_a(self): racks = [Rack("A", "", 100)] machine = Machine(racks, 0) machine.refill("A", 1) machine.insert(Coin.DOLLAR) outcome = machine.press("A") self.assertTrue(outcome) self.assertEqual(machine.racks["A"].quantity, 0) self.assertEqual(machine.amount, 0) self.assertEqual(machine.coins[Coin.DOLLAR], 1)
def test_buy_biscuit_too_many_coins(self): rack = Rack("A", "Biscuit", 100) machine = Machine([rack], 10) machine.refill("A", 1) for i in range(3): machine.insert(Coin.QUARTER) for i in range(3): machine.insert(Coin.DIME) machine.press("A") self.assertEqual(machine.racks['A'].quantity, 0) self.assertEqual(machine.coins[Coin.QUARTER], 13) self.assertEqual(machine.coins[Coin.DIME], 13) self.assertEqual(machine.coins[Coin.NICKEL], 9)
def test_user_can_buy_item_A_with_too_much_money(self): racks = [Rack("A", "Biscuits", 100)] machine = Machine(racks) machine.refill("A", 1) machine.insertCoin(Coin.QUARTER) machine.insertCoin(Coin.QUARTER) machine.insertCoin(Coin.QUARTER) machine.insertCoin(Coin.QUARTER) machine.insertCoin(Coin.QUARTER) machine.pressButton("A") self.assertEqual(machine.racks["A"].quantity, 0) self.assertEqual(machine.coins[Coin.QUARTER], 4) self.assertEqual(machine.amount, 0)
def test_make_change_fail(self): # give 25+10, buy for 30 # no change available should not give product and return all money machine = Machine([], 0) rack = Rack("A", "Biscuit", 30) machine = Machine([rack], 0) machine.refill("A", 1) machine.insert(Coin.QUARTER) machine.insert(Coin.DIME) machine.press("A") #print(machine.coins) self.assertEqual(machine.racks['A'].quantity, 1) self.assertEqual(sum([k.value * v for k, v in machine.coins.items()]), 0)
def test_buy_biscuit_too_many_coins2(self): # give 25+10x10 => 125 # buy for 95 # machine should return 30, and have 95 rack = Rack("A", "Biscuit", 95) machine = Machine([rack], 0) machine.refill("A", 1) machine.insert(Coin.QUARTER) for i in range(10): machine.insert(Coin.DIME) machine.press("A") #print(machine.coins) self.assertEqual(machine.racks['A'].quantity, 0) self.assertEqual(sum([k.value * v for k, v in machine.coins.items()]), 95)
def test_refill(self): rack = Rack("A", "Biscuit", 100) machine = Machine([rack]) machine.refill("A", 10) self.assertEqual(machine.racks['A'].quantity, 10)
def test_can_refill_biscuits(self): racks = [Rack("A", "", 100)] machine = Machine(racks) machine.refill("A", 3) self.assertEqual(machine.racks["A"].quantity, 3)