def main(): coke = Product('Coke', 1.00) validator = CoinValidator() vending_machine = VendingMachine(coke, validator, coke.price) coin = Coin(5.670) vending_machine.insert_coins(coin) print(vending_machine.DISPLAY) vending_machine.insert_coins(coin) vending_machine.insert_coins(coin) print(vending_machine.DISPLAY) vending_machine.insert_coins(coin) print(vending_machine.DISPLAY)
class VendingMachineTest(unittest.TestCase): def setUp(self): self.vending_machine = VendingMachine() def test_vending_display(self): # amount with nothing selected self.assertEqual(self.vending_machine.vending_display(), "INSERT COIN") self.vending_machine.coin_accept.accept_coins('quarter') self.assertEqual(self.vending_machine.vending_display(), "$0.25") # amount with selection self.vending_machine.product_select.select_product('chips') self.assertEqual(self.vending_machine.vending_display(), "PRICE $0.25") # thank you message after enough inserted self.vending_machine.coin_accept.accept_coins('quarter') self.assertEqual(self.vending_machine.vending_display(), "THANK YOU") self.assertEqual(self.vending_machine.vending_display(), "INSERT COIN") def test_sold_out(self): self.vending_machine.product_select.product_options = { 'cola': (1, False), 'chips': (.5, False), 'candy': (.65, False) } # Test to see insert coin after sold out self.vending_machine.product_select.select_product('chips') self.assertEqual(self.vending_machine.vending_display(), "SOLD OUT") self.assertEqual(self.vending_machine.vending_display(), "INSERT COIN") # Test to see amount inserted after sold out self.vending_machine.coin_accept.accept_coins('quarter') self.vending_machine.product_select.select_product('chips') self.assertEqual(self.vending_machine.vending_display(), "SOLD OUT") self.assertEqual(self.vending_machine.vending_display(), "$0.25") def test_exact_change_only(self): self.vending_machine.change_in_machine = 0 self.assertEqual(self.vending_machine.vending_display(), "EXACT CHANGE ONLY")
class TestVendingMachine(object): def setup(self): self.coin = Quarter() self.product = Product('Coke', .50) self.validator = CoinValidator() self.vending_machine = VendingMachine(self.product, self.validator, .50) def test_input_coins_updates_display_when_payment_is_suffice(self): product = Product('Coke', .25) vending_machine = VendingMachine(product, self.validator, .25) vending_machine.insert_coins(self.coin) assert vending_machine.DISPLAY == THANK_YOU def test_input_coins_does_not_update_display_when_payment_is_insufficient( self): self.vending_machine.insert_coins(self.coin) assert self.vending_machine.DISPLAY == INSERT_COINS def test_payment_attribute_is_zero_when_no_payment_has_been_made(self): assert self.vending_machine.PAYMENT == 0 def test_input_coins_tracks_payment_amount(self): self.vending_machine.PAYMENT = .25 self.vending_machine.insert_coins(self.coin) assert self.vending_machine.DISPLAY == THANK_YOU def test_input_coins_returns_change(self): expected_change = .05 self.vending_machine.PAYMENT = .30 actual_change = self.vending_machine.insert_coins(self.coin) assert actual_change == expected_change def test_input_coins_does_not_allow_pennies(self): expected_change = .01 penny = Penny() actual_change = self.vending_machine.insert_coins(penny) assert actual_change == expected_change assert self.vending_machine.DISPLAY == INSERT_COINS
def test_input_coins_updates_display_when_payment_is_suffice(self): product = Product('Coke', .25) vending_machine = VendingMachine(product, self.validator, .25) vending_machine.insert_coins(self.coin) assert vending_machine.DISPLAY == THANK_YOU
def setup(self): self.coin = Quarter() self.product = Product('Coke', .50) self.validator = CoinValidator() self.vending_machine = VendingMachine(self.product, self.validator, .50)
def order_item(context, price): validator = CoinValidator() product = Product('Item', float(price)) context.vending_machine = VendingMachine(product, validator, float(price))
def step_impl(context, product, price): product = Product(product, float(price)) validator = CoinValidator() context.vending_machine = VendingMachine(product, validator, product.price)
class TestSuite(unittest.TestCase): VendingMachine = VendingMachine() def test_when_vending_machine_is_passed_a_coin_it_returns_that_coin(self): coin = quarter self.assertEqual(self.VendingMachine.accept_coins(coin), quarter) self.VendingMachine.vending_machine_reset() def test_when_vending_machine_is_passed_a_valid_coin_it_updates_current_amount( self): coin1 = quarter self.VendingMachine.accept_coins(coin1) self.assertEqual(self.VendingMachine.current_amount, 0.25) self.assertEqual(self.VendingMachine.coin_return, []) coin3 = dime self.VendingMachine.accept_coins(coin3) self.assertEqual(self.VendingMachine.current_amount, 0.35) self.assertEqual(self.VendingMachine.coin_return, []) def test_when_vending_machine_is_passed_an_invalid_coin_it_is_added_to_return( self): coin2 = penny self.VendingMachine.accept_coins(coin2) self.assertEqual(self.VendingMachine.current_amount, 0.35) self.assertEqual(self.VendingMachine.coin_return, [{ 'size': 19, 'weight': 2.5, 'value': 0.01 }]) def test_when_product_is_selected_price_is_set(self): self.VendingMachine.button_press("cola") self.assertEqual(self.VendingMachine.price, 1.00) self.VendingMachine.button_press("chips") self.assertEqual(self.VendingMachine.price, 0.50) self.VendingMachine.button_press("candy") self.assertEqual(self.VendingMachine.price, 0.65) def test_check_transaction_for_correct_balance(self): self.VendingMachine.vending_machine_reset() coin = quarter self.VendingMachine.accept_coins(coin) self.assertEqual(self.VendingMachine.current_amount, 0.25) self.VendingMachine.button_press("cola") self.assertEqual(self.VendingMachine.price, 1.00) self.VendingMachine._check_transaction() self.assertEqual(self.VendingMachine.balance, 0.75) def test_insert_coins_then_press_button_if_cola_transaction_amount_correct_dispense_cola( self): self.VendingMachine.vending_machine_reset() coin = quarter self.VendingMachine.accept_coins(coin) self.VendingMachine.accept_coins(coin) self.VendingMachine.accept_coins(coin) self.VendingMachine.accept_coins(coin) self.VendingMachine.button_press("cola") self.assertEqual(self.VendingMachine.dispensed_product, 'cola') def test_press_button_then_insert_coins_if_cola_transaction_amount_correct_dispense_cola( self): self.VendingMachine.vending_machine_reset() self.VendingMachine.button_press("cola") coin = quarter self.VendingMachine.accept_coins(coin) self.VendingMachine.accept_coins(coin) self.VendingMachine.accept_coins(coin) self.VendingMachine.accept_coins(coin) self.assertEqual(self.VendingMachine.dispensed_product, 'cola') def test_insert_coins_then_press_button_if_chips_transaction_amount_correct_dispense_chips( self): self.VendingMachine.vending_machine_reset() coin1 = quarter coin2 = quarter self.VendingMachine.accept_coins(coin1) self.VendingMachine.accept_coins(coin2) self.VendingMachine.button_press("chips") self.assertEqual(self.VendingMachine.dispensed_product, 'chips') def test_press_button_then_insert_coins_if_cola_transaction_amount_correct_dispense_chips( self): self.VendingMachine.vending_machine_reset() self.VendingMachine.button_press("chips") coin1 = quarter coin2 = quarter self.VendingMachine.accept_coins(coin1) self.VendingMachine.accept_coins(coin2) self.assertEqual(self.VendingMachine.dispensed_product, 'chips') def test_insert_coins_then_press_button_if_cola_transaction_amount_correct_dispense_candy( self): self.VendingMachine.vending_machine_reset() coin1 = quarter coin2 = quarter coin3 = dime coin4 = nickel self.VendingMachine.accept_coins(coin1) self.VendingMachine.accept_coins(coin2) self.VendingMachine.accept_coins(coin3) self.VendingMachine.accept_coins(coin4) self.VendingMachine.button_press("candy") self.assertEqual(self.VendingMachine.dispensed_product, 'candy') def test_press_button_then_insert_coins_if_cola_transaction_amount_correct_dispense_candy( self): self.VendingMachine.vending_machine_reset() self.VendingMachine.button_press("candy") coin1 = quarter coin2 = quarter coin3 = dime coin4 = nickel self.VendingMachine.accept_coins(coin1) self.VendingMachine.accept_coins(coin2) self.VendingMachine.accept_coins(coin3) self.VendingMachine.accept_coins(coin4) self.assertEqual(self.VendingMachine.dispensed_product, 'candy') def test_after_complete_transaction_reset_transaction(self): self.VendingMachine.vending_machine_reset() coin = quarter self.VendingMachine.accept_coins(coin) self.VendingMachine.accept_coins(coin) self.VendingMachine.accept_coins(coin) self.VendingMachine.accept_coins(coin) self.VendingMachine.button_press("cola") self.assertEqual(self.VendingMachine.selected_product, 'cola') self.assertEqual(self.VendingMachine.price, 1.00) self.assertEqual(self.VendingMachine.dispensed_product, 'cola') self.VendingMachine.accept_coins(coin) self.assertEqual(self.VendingMachine.selected_product, None) self.assertEqual(self.VendingMachine.price, 0) self.assertEqual(self.VendingMachine.dispensed_product, None) def test_machine_updates_display_for_current_amount_when_coins_are_accepted( self): self.VendingMachine.vending_machine_reset() coin = quarter self.VendingMachine.accept_coins(coin) self.VendingMachine.accept_coins(coin) self.assertEqual(self.VendingMachine.display, '0.5') def test_machine_displays_PRICE_if_not_enough_money_is_inserted(self): self.VendingMachine.vending_machine_reset() self.VendingMachine.button_press("candy") self.assertEqual(self.VendingMachine.display, 'PRICE:0.65') def test_make_change_correctly_reaches_zero_change_due(self): self.VendingMachine.vending_machine_reset() self.VendingMachine._make_change(-0.65) self.assertEqual(self.VendingMachine.change_due, 0) self.VendingMachine.vending_machine_reset() self.VendingMachine._make_change(-0.75) self.assertEqual(self.VendingMachine.change_due, 0) self.VendingMachine.vending_machine_reset() self.VendingMachine._make_change(-0.05) self.assertEqual(self.VendingMachine.change_due, 0) self.VendingMachine.vending_machine_reset() self.VendingMachine._make_change(-0.10) self.assertEqual(self.VendingMachine.change_due, 0) self.VendingMachine.vending_machine_reset() self.VendingMachine._make_change(-0.1005) self.assertEqual(self.VendingMachine.change_due, 0) def test_make_change_sums_correct_number_of_each_coin_in_change(self): self.VendingMachine.vending_machine_reset() self.VendingMachine._make_change(-0.65) # quarter, quarter, dime, nickle self.assertEqual(self.VendingMachine.coin_return, [{ 'size': 24.2, 'weight': 5.6, 'value': 0.25 }, { 'size': 24.2, 'weight': 5.6, 'value': 0.25 }, { 'size': 17.9, 'weight': 2.2, 'value': 0.10 }, { 'size': 21.2, 'weight': 5, 'value': 0.05 }]) self.VendingMachine.vending_machine_reset() self.VendingMachine._make_change(-0.75) # quarter, quarter, quarter self.assertEqual(self.VendingMachine.coin_return, [{ 'size': 24.2, 'weight': 5.6, 'value': 0.25 }, { 'size': 24.2, 'weight': 5.6, 'value': 0.25 }, { 'size': 24.2, 'weight': 5.6, 'value': 0.25 }]) self.VendingMachine.vending_machine_reset() self.VendingMachine._make_change(-0.05) # nickle self.assertEqual(self.VendingMachine.coin_return, [{ 'weight': 5, 'size': 21.2, 'value': 0.05 }]) self.VendingMachine.vending_machine_reset() self.VendingMachine._make_change(-0.10) # dime self.assertEqual(self.VendingMachine.coin_return, [{ 'weight': 2.2, 'size': 17.9, 'value': 0.10 }]) self.VendingMachine.vending_machine_reset() self.VendingMachine._make_change(-25000) # 100,000 quarters # self.assertEqual(len(self.VendingMachine.coin_return), 100000) def test_when_the_item_selected_by_the_customer_is_out_of_stock_the_machine_displays_SOLD_OUT( self): self.VendingMachine.vending_machine_reset() for sale_index in range(6): coin = quarter self.VendingMachine.accept_coins(coin) self.VendingMachine.accept_coins(coin) self.VendingMachine.accept_coins(coin) self.VendingMachine.accept_coins(coin) self.VendingMachine.button_press("cola") self.assertEqual(self.VendingMachine.display, "SOLD OUT") def test_vending_machine_correctly_checks_change_making_ability(self): self.VendingMachine.vending_machine_reset() self.VendingMachine.dime_quantity = 1 self.VendingMachine.nickel_quantity = 1 self.assertEqual(self.VendingMachine.display, "INSERT COIN") # candy = 0.65 self.VendingMachine.button_press("candy") coin = quarter self.VendingMachine.accept_coins(coin) self.VendingMachine.accept_coins(coin) self.VendingMachine.accept_coins(coin) self.assertEqual(self.VendingMachine.display, "THANK YOU") self.assertEqual(self.VendingMachine.dime_quantity, 0) self.VendingMachine.new_transaction() self.assertEqual(self.VendingMachine.display, "EXACT CHANGE ONLY") def test_return_coins(self): self.VendingMachine.vending_machine_reset() self.VendingMachine.print_and_break_on_this_test = True coin1 = quarter self.VendingMachine.accept_coins(coin1) self.VendingMachine.accept_coins(coin1) self.VendingMachine.return_coins() self.assertEqual(self.VendingMachine.display, "INSERT COIN") self.assertEqual(self.VendingMachine.coin_return, [{ 'weight': 5.6, 'size': 24.2, 'value': 0.25 }, { 'weight': 5.6, 'size': 24.2, 'value': 0.25 }]) self.VendingMachine.new_transaction() coin2 = dime self.VendingMachine.accept_coins(coin2) self.VendingMachine.return_coins() self.assertEqual(self.VendingMachine.display, "INSERT COIN") self.assertEqual(self.VendingMachine.coin_return, [{ 'weight': 2.2, 'size': 17.9, 'value': 0.10 }]) self.VendingMachine.new_transaction() coin3 = nickel self.VendingMachine.accept_coins(coin3) self.VendingMachine.accept_coins(coin3) self.VendingMachine.accept_coins(coin3) self.VendingMachine.return_coins() self.assertEqual(self.VendingMachine.display, "INSERT COIN") self.assertEqual(self.VendingMachine.coin_return, [{ 'weight': 5, 'size': 21.2, 'value': 0.05 }, { 'weight': 5, 'size': 21.2, 'value': 0.05 }, { 'weight': 5, 'size': 21.2, 'value': 0.05 }])
def setUp(self): self.vending_machine = VendingMachine()
def given_vending_machine_with_change(self, change): self.vending_machine = VendingMachine( TestsFixture.DISPLAY, MoneyManager(CoinInventory(change, TestsFixture.COIN_CONVERTER)), ProductManager(TestsFixture.PRODUCT_INVENTORY), ReturnManager())
class VendingMachineTests(unittest.TestCase): def setUp(self): self.given_vending_machine() self.expected_money = 0 def given_vending_machine(self): self.given_vending_machine_with_change(TestsFixture.CURRENT_CHANGE) def given_vending_machine_with_change(self, change): self.vending_machine = VendingMachine( TestsFixture.DISPLAY, MoneyManager(CoinInventory(change, TestsFixture.COIN_CONVERTER)), ProductManager(TestsFixture.PRODUCT_INVENTORY), ReturnManager()) def test_should_display_correct_amount_for_one_valid_coin_inserted(self): for valid_coin in TestsFixture.VALID_COINS: self.given_vending_machine() value = TestsFixture.MONEY_MAP[ valid_coin.get_measure()].get_value() self.assertEqual(self.vending_machine.insert_coin(valid_coin), TestsFixture.AMOUNT_TPL % value) def test_should_display_no_coin_msg_when_invalid_coin_inserted(self): for invalid_coin in TestsFixture.INVALID_COINS: self.given_vending_machine() self.assertEqual(self.vending_machine.insert_coin(invalid_coin), TestsFixture.NO_COINS_MSG) def test_should_display_added_amount_of_inserted_coins(self): self.given_vending_machine_with_coins() self.expected_money += TestsFixture.MONEY_VALUE_1 self.assertEqual(self.vending_machine.insert_coin(TestsFixture.COIN_1), self.get_display_of_expected_money()) def given_vending_machine_with_coins(self): self.given_vending_machine() self.vending_machine.insert_coin(TestsFixture.COIN_1) self.vending_machine.insert_coin(TestsFixture.COIN_1) self.vending_machine.insert_coin(TestsFixture.COIN_2) self.vending_machine.insert_coin(TestsFixture.COIN_3) self.vending_machine.insert_coin(TestsFixture.COIN_3) self.expected_money = 2 * TestsFixture.MONEY_VALUE_1 + TestsFixture.MONEY_VALUE_2 + 2 * TestsFixture.MONEY_VALUE_3 def test_should_display_amount_when_valid_coins_inserted(self): self.vending_machine.insert_coin(TestsFixture.COIN_1) self.assertEqual(self.vending_machine.display(), TestsFixture.AMOUNT_TPL % TestsFixture.MONEY_VALUE_1) def test_should_display_amount_when_more_than_one_valid_coins_inserted( self): self.given_vending_machine_with_coins() self.assertEqual(self.vending_machine.display(), self.get_display_of_expected_money()) def get_display_of_expected_money(self): return TestsFixture.AMOUNT_TPL % self.expected_money def test_should_display_insert_coin_msg_when_no_coins_inserted(self): self.assertEqual(self.vending_machine.display(), TestsFixture.NO_COINS_MSG) def test_should_display_insert_coin_msg_when_no_valid_coins_inserted(self): self.vending_machine.insert_coin(TestsFixture.INVALID_COIN) self.assertEqual(self.vending_machine.display(), TestsFixture.NO_COINS_MSG) def test_should_place_rejected_coins_in_coin_return(self): self.given_vending_machine_with_coins() self.vending_machine.insert_coin(TestsFixture.INVALID_COIN) self.vending_machine.insert_coin(TestsFixture.INVALID_COIN) self.assertEqual( self.vending_machine.check_return(), [TestsFixture.INVALID_COIN, TestsFixture.INVALID_COIN]) def test_should_display_thanks_msg_when_button_is_pressed_and_enough_money( self): self.given_vending_machine_with_coins() self.assertEqual( self.vending_machine.push_product_button( TestsFixture.PRODUCT_NAME_3), TestsFixture.THANKS_MSG) def test_should_display_price_msg_when_button_is_pressed_and_not_enough_money( self): self.given_vending_machine_with_coins() self.assertEqual( self.vending_machine.push_product_button( TestsFixture.PRODUCT_NAME_1), TestsFixture.PRICE_MSG) def test_should_display_insert_coin_when_button_is_pressed_and_enough_money_and_display_checked_again( self): self.given_vending_machine_with_coins_and_user_pushes_button( TestsFixture.PRODUCT_NAME_3) self.assertEqual(self.vending_machine.display(), TestsFixture.NO_COINS_MSG) def given_vending_machine_with_coins_and_user_pushes_button( self, product_name): self.given_vending_machine_with_coins() self.vending_machine.push_product_button(product_name) def test_should_display_amount_when_button_is_pressed_and_not_enough_money_and_there_is_amount( self): self.given_vending_machine_with_coins_and_user_pushes_button( TestsFixture.PRODUCT_NAME_1) self.assertEqual(self.vending_machine.display(), self.get_display_of_expected_money()) def test_should_display_insert_coin_when_button_is_pressed_and_not_enough_money_and_there_is_nothing( self): self.vending_machine.push_product_button(TestsFixture.PRODUCT_NAME_1) self.assertEqual(self.vending_machine.display(), TestsFixture.NO_COINS_MSG) def test_should_show_empty_dispenser_when_button_is_pressed_and_not_enough_money( self): self.given_vending_machine() self.vending_machine.push_product_button(TestsFixture.PRODUCT_NAME_1) self.assertEqual(self.vending_machine.check_dispenser(), []) def test_should_display_return_with_remaining_amount_when_product_is_bought( self): self.given_vending_machine_with_coins() self.vending_machine.push_product_button(TestsFixture.PRODUCT_NAME_3) self.assertEqual(self.vending_machine.check_return(), [TestsFixture.COIN_1]) def test_should_show_product_in_dispenser_when_product_is_bought(self): self.given_vending_machine_with_coins() self.vending_machine.push_product_button(TestsFixture.PRODUCT_NAME_3) self.assertEqual(self.vending_machine.check_dispenser(), [TestsFixture.PRODUCT_3]) def test_should_display_insert_coin_when_return_button_is_pressed(self): self.vending_machine.insert_coin(TestsFixture.COIN_1) self.assertEqual(self.vending_machine.push_return_button(), TestsFixture.NO_COINS_MSG) def test_should_return_inserted_money_when_return_button_is_pressed(self): self.vending_machine.insert_coin(TestsFixture.COIN_1) self.vending_machine.push_return_button() self.assertEqual(self.vending_machine.check_return(), [TestsFixture.COIN_1]) def test_should_say_sold_out_when_stock_of_selected_product_is_zero(self): self.given_vending_machine_with_coins() actual = self.vending_machine.push_product_button( TestsFixture.PRODUCT_NAME_2) self.assertEqual(actual, TestsFixture.SOLD_OUT_MSG) def test_should_say_insert_coin_when_stock_and_amount_is_zero_and_display_is_checked_again( self): self.given_vending_machine() self.vending_machine.push_product_button(TestsFixture.PRODUCT_NAME_2) self.assertEqual(self.vending_machine.display(), TestsFixture.NO_COINS_MSG) def test_should_say_amount_when_stock_is_zero_and_amount_not_zero_and_display_is_checked_again( self): self.vending_machine.insert_coin(TestsFixture.COIN_1) self.vending_machine.push_product_button(TestsFixture.PRODUCT_NAME_2) self.assertEqual(self.vending_machine.display(), TestsFixture.AMOUNT_TPL % TestsFixture.MONEY_VALUE_1) def test_should_display_exact_change_when_not_enough_money_for_product( self): self.given_vending_machine_with_change( [TestsFixture.COIN_3, TestsFixture.COIN_3]) self.assertEqual(self.vending_machine.display(), TestsFixture.EXACT_CHANGE_MSG) if __name__ == '__main__': unittest.main()