Beispiel #1
0
    def test_remove_item_from_slot_no_items(self):
        vending_machine = VendingMachine()
        removed = vending_machine.remove_item_from_slot(1)

        self.assertFalse(removed)
        self.assertEqual(9, vending_machine.available_slots)
        self.assertIsNone(vending_machine.slot_items[1])
Beispiel #2
0
    def test_remove_money_success(self):
        vending_machine = VendingMachine()
        vending_machine.insert_money(238)
        removed, total_balance = vending_machine.remove_money(213)

        self.assertTrue(removed)
        self.assertEqual(25, total_balance)
Beispiel #3
0
    def test_get_abstract_request_exception(self):
        vending_machine = VendingMachine()

        with patch(
                'requests.get',
                MagicMock(side_effect=requests.exceptions.RequestException)):
            self.assertEqual('', vending_machine._get_abstract('test'))
Beispiel #4
0
    def test_move_item_to_slot_empty_target_slot(self):
        vending_machine = VendingMachine()
        vending_machine.add_item_to_slot(1, Item('Soda', 1.25, 20))

        self.assertTrue(vending_machine.move_item_to_slot(1, 2))
        self.assertIsNone(vending_machine.slot_items[1])
        self.assertEqual('Soda', vending_machine.slot_items[2].name)
Beispiel #5
0
    def test_remove_item_from_slot_invalid_slot(self):
        vending_machine = VendingMachine()
        removed = vending_machine.remove_item_from_slot(11)

        self.assertFalse(removed)
        self.assertEqual(9, vending_machine.available_slots)
        self.assertEqual('Invalid slot',
                         vending_machine.slot_items.get(11, 'Invalid slot'))
Beispiel #6
0
    def test_remove_money_amount_higher_than_balance(self):
        vending_machine = VendingMachine()
        inserted, total_balance = vending_machine.insert_money(23)
        removed, total_balance_after_removal = vending_machine.remove_money(
            total_balance * 2)

        self.assertTrue(removed)
        self.assertEqual(0, total_balance_after_removal)
Beispiel #7
0
    def test_decrease_stock_amount_success(self):
        vending_machine = VendingMachine()
        vending_machine.add_item_to_slot(1, Item('Coffee', 1.75, 6))
        decreased, new_stock = vending_machine.decrease_stock(1, 4)

        self.assertTrue(decreased)
        self.assertEqual(2, new_stock)
        self.assertEqual(2, vending_machine.slot_items[1].stock)
Beispiel #8
0
    def test_decrease_stock_amount_higher_than_current_stock(self):
        vending_machine = VendingMachine()
        vending_machine.add_item_to_slot(1, Item('Coffee', 1.75, 6))
        decreased, new_stock = vending_machine.decrease_stock(1, 25)

        self.assertTrue(decreased)
        self.assertEqual(0, new_stock)
        self.assertEqual(0, vending_machine.slot_items[1].stock)
Beispiel #9
0
    def test_select_and_vend_empty_slot(self):
        vending_machine = VendingMachine()
        vended, item_summary, vend_result, total_balance = vending_machine.select_and_vend(
            1)

        self.assertFalse(vended)
        self.assertEqual('', item_summary)
        self.assertEqual('Empty Slot', vend_result)
Beispiel #10
0
    def test_increase_stock_success(self):
        vending_machine = VendingMachine()
        vending_machine.add_item_to_slot(1, Item('Coffee', 1.75, 6))
        increased, new_stock = vending_machine.increase_stock(1, 15)

        self.assertTrue(increased)
        self.assertEqual(21, new_stock)
        self.assertEqual(21, vending_machine.slot_items[1].stock)
Beispiel #11
0
def test_insert_invalid_money():
    """無効なお金が投入された物がそのまま返却されるか"""

    vending_machine = VendingMachine()
    insert_money = [1, 5, 10000, 3]

    actual = vending_machine.insert(insert_money)
    expected = insert_money
    assert actual == expected
Beispiel #12
0
def test_get_juice_info():
    """自販機で管理されているジュースの情報を取得する"""

    vending_machine = VendingMachine()

    actual = vending_machine.get_juice_info()
    expected = {"coke": {"price": 120, "num": 5}}

    assert actual == expected
Beispiel #13
0
    def test_remove_item_from_slot_success(self):
        sparkling_water = Item('Sparkling Water', 1.25, 6)

        vending_machine = VendingMachine()
        vending_machine.add_item_to_slot(1, sparkling_water)
        removed = vending_machine.remove_item_from_slot(1)

        self.assertTrue(removed)
        self.assertEqual(9, vending_machine.available_slots)
        self.assertIsNone(vending_machine.slot_items[1])
Beispiel #14
0
    def test_add_item_to_slot_invalid_slot(self):
        sparkling_water = Item('Sparkling Water', 1.25, 6)

        vending_machine = VendingMachine()
        added = vending_machine.add_item_to_slot(10, sparkling_water)

        self.assertFalse(added)
        self.assertEqual(9, vending_machine.available_slots)
        self.assertEqual('Invalid slot',
                         vending_machine.slot_items.get(10, 'Invalid slot'))
Beispiel #15
0
class TestMenu(unittest.TestCase):
    def setUp(self):
        self.vm = VendingMachine(MenuImp(items))
        self.menu = test_menu

    def test_print_menu(self):
        expected = "A1: lays $3.75 Out of Stock\n" + \
                   "B3: pepsi $1.75\n" + \
                   "C4: payday $0.99\n"
        self.assertEqual(expected, self.vm.print_menu())

    def test_invalid_selection(self):
        with self.assertRaises(PyVendingMachineError) as context:
            self.vm.make_selection("XX")
        self.assertEqual(context.exception.message, Message.INVALID_SELECTION)

    def test_insufficient_funds(self):
        with self.assertRaises(PyVendingMachineError) as context:
            self.vm.make_selection(drink)
        self.assertEqual(context.exception.message, Message.INSUFFICIENT_FUNDS)

    def test_out_of_stock(self):
        with self.assertRaises(PyVendingMachineError) as context:
            self.vm.insert_five()
            self.vm.make_selection(chips)
        self.assertEqual(context.exception.message, Message.OUT_OF_STOCK)
Beispiel #16
0
def test_get_amount():
    """自販機に投入した金額の総計が正しく出力されるか"""

    vending_machine = VendingMachine()
    insert_money = [10, 50, 100, 500, 1000]
    vending_machine.insert(insert_money)

    actual = vending_machine.get_amount()
    expected = sum(insert_money)

    assert actual == expected
Beispiel #17
0
    def test_select_and_vend_not_enough_money(self):
        sparkling_water = Item('Sparkling Water', 1.25, 6)

        vending_machine = VendingMachine()
        vending_machine.add_item_to_slot(1, sparkling_water)
        vended, item_summary, vend_result, total_balance = vending_machine.select_and_vend(
            1)

        self.assertFalse(vended)
        self.assertEqual('Sparkling Water', item_summary)
        self.assertEqual('Insufficient Balance', vend_result)
Beispiel #18
0
    def test_insert_money_success(self):
        vending_machine = VendingMachine()
        inserted, total_balance = vending_machine.insert_money(12)

        self.assertTrue(inserted)
        self.assertEqual(12, total_balance)

        inserted, total_balance = vending_machine.insert_money(53.5)

        self.assertTrue(inserted)
        self.assertEqual(65.5, total_balance)
Beispiel #19
0
    def test_get_abstract_success(self):
        vending_machine = VendingMachine()

        with patch('requests.models.Response.json',
                   MagicMock(return_value={'AbstractText': ''})):
            self.assertEqual('', vending_machine._get_abstract('test'))

        with patch('requests.models.Response.json',
                   MagicMock(return_value={'AbstractText': 'Test text'})):
            self.assertEqual('Test text',
                             vending_machine._get_abstract('test'))
Beispiel #20
0
    def test_add_item_to_slot_success(self):
        sparkling_water = Item('Sparkling Water', 1.25, 6)

        vending_machine = VendingMachine()
        added = vending_machine.add_item_to_slot(1, sparkling_water)

        self.assertTrue(added)
        self.assertEqual(8, vending_machine.available_slots)
        self.assertEqual('Sparkling Water', vending_machine.slot_items[1].name)
        self.assertEqual(1.25, vending_machine.slot_items[1].price)
        self.assertEqual(6, vending_machine.slot_items[1].stock)
Beispiel #21
0
    def test_add_item_to_slot_already_occupied_with_other_item(self):
        sparkling_water = Item('Sparkling Water', 1.25, 6)
        regular_water = Item('Water', 1.00, 2)

        vending_machine = VendingMachine()
        vending_machine.add_item_to_slot(1, sparkling_water)
        added = vending_machine.add_item_to_slot(1, regular_water)

        self.assertFalse(added)
        self.assertEqual(8, vending_machine.available_slots)
        self.assertNotEqual('Water', vending_machine.slot_items[1].name)
        self.assertIsNotNone(vending_machine.slot_items[1])
Beispiel #22
0
def test_check_multiple_insert():
    """複数のお金が挿入できるか"""

    vending_machine = VendingMachine()
    insert_money = [10, 50, 100, 500, 1000]
    actual_insert = vending_machine.insert(insert_money)

    assert actual_insert == []

    actual_money_box = vending_machine.money_box
    expected = {10: 1, 50: 1, 100: 1, 500: 1, 1000: 1}

    assert actual_money_box == expected
Beispiel #23
0
    def test_add_item_to_slot_already_occupied_with_same_item(self):
        sparkling_water = Item('Sparkling Water', 1.25, 6)

        vending_machine = VendingMachine()
        vending_machine.add_item_to_slot(1, sparkling_water)

        sparkling_water2 = Item('Sparkling Water', 1.5, 21)
        added = vending_machine.add_item_to_slot(1,
                                                 sparkling_water2,
                                                 replace=True)

        self.assertTrue(added)
        self.assertEqual(8, vending_machine.available_slots)
        self.assertEqual('Sparkling Water', vending_machine.slot_items[1].name)
        self.assertEqual(1.5, vending_machine.slot_items[1].price)
        self.assertEqual(27, vending_machine.slot_items[1].stock)
Beispiel #24
0
    def test_move_item_to_slot_target_slot_has_item(self):
        vending_machine = VendingMachine()
        vending_machine.add_item_to_slot(1, Item('Soda', 1.25, 20))
        vending_machine.add_item_to_slot(2, Item('Coffee', 1.75, 6))

        self.assertFalse(vending_machine.move_item_to_slot(1, 2))
        self.assertEqual('Soda', vending_machine.slot_items[1].name)
        self.assertEqual('Coffee', vending_machine.slot_items[2].name)
Beispiel #25
0
    def test_move_item_to_slot_replace_item_in_target_slot(self):
        vending_machine = VendingMachine()
        vending_machine.add_item_to_slot(1, Item('Soda', 1.25, 20))
        vending_machine.add_item_to_slot(2, Item('Coffee', 1.75, 6))

        self.assertTrue(vending_machine.move_item_to_slot(1, 2, replace=True))
        self.assertIsNone(vending_machine.slot_items[1])
        self.assertEqual('Soda', vending_machine.slot_items[2].name)
Beispiel #26
0
    def test_select_and_vend_out_of_stock(self):
        orange_juice = Item('Orange Juice', 1.50, 0)

        vending_machine = VendingMachine()
        vending_machine.add_item_to_slot(1, orange_juice)
        vending_machine.insert_money(12)
        vended, item_summary, vend_result, total_balance = vending_machine.select_and_vend(
            1)

        self.assertFalse(vended)
        self.assertEqual('Orange Juice', item_summary)
        self.assertEqual('Out of Stock', vend_result)
Beispiel #27
0
    def test_select_and_vend_success(self):
        soda = Item('Soda', 1.25, 20)

        vending_machine = VendingMachine()
        vending_machine.insert_money(10.0)
        vending_machine.add_item_to_slot(3, soda)
        vended, item_summary, vend_result, total_balance = vending_machine.select_and_vend(
            3)

        self.assertTrue(vended)
        self.assertEqual('Soda', item_summary)
        self.assertEqual('Vended: Soda', vend_result)
        self.assertEqual(8.75, total_balance)
Beispiel #28
0
def test_refund_money():
    """自販機に入っているお金が正しく返金されるか"""

    vending_machine = VendingMachine()
    insert_money = [10, 50, 100, 500, 1000]
    vending_machine.insert(insert_money)

    actual_refunded_money = vending_machine.refund_money()
    expected_refunded_money = {10: 1, 50: 1, 100: 1, 500: 1, 1000: 1}

    assert actual_refunded_money == expected_refunded_money

    actual_amount = vending_machine.get_amount()
    expected_amount = 0

    assert actual_amount == expected_amount
Beispiel #29
0
 def setUp(self):
     self.vm = VendingMachine(MenuImp(items))
     self.vm.menu.items.__getitem__(drink).__setitem__('stock', 10)
Beispiel #30
0
class TestPurchase(unittest.TestCase):
    def setUp(self):
        self.vm = VendingMachine(MenuImp(items))
        self.vm.menu.items.__getitem__(drink).__setitem__('stock', 10)

    def test_stock_depletes_after_successful_purchase(self):
        self.vm.insert_five()
        self.vm.make_selection(drink)
        self.assertEqual(9,
                         self.vm.menu.items.__getitem__(drink).info['stock'])

    def test_balance_depletes_after_successful_purchase(self):
        self.vm.insert_five()
        self.vm.make_selection(drink)
        self.assertEqual('$3.25', self.vm.balance.get_amount())

    def test_stock_does_not_deplete_if_purchase_fails(self):
        with self.assertRaises(PyVendingMachineError) as context:
            self.vm.insert_dollar()
            self.vm.make_selection(drink)
        self.assertEqual(10,
                         self.vm.menu.items.__getitem__(drink).info['stock'])

    def test_balance_does_not_deplete_if_purchase_fails(self):
        with self.assertRaises(PyVendingMachineError) as context:
            self.vm.insert_dollar()
            self.vm.make_selection(drink)
        self.assertEqual(100, self.vm.balance.amount)
class TestVendingMachine:
    def setUp(self):
        self.vending_machine = VendingMachine()
        self.vending_machine.reset()

    def test_release_change_when_no_payment_expects_0_change(self):
        # Arrange

        # Act
        result = self.vending_machine.release_change()

        # Assert
        assert_equals(0, result)

    def test_release_change_with_payment_expects_change_returned(self):
        # Arrange
        self.vending_machine.insert_coin(1)

        # Act
        result = self.vending_machine.release_change()

        # Assert
        assert_greater(result, 0)

    @unittest.skip("buy_product now returns an exception")
    def test_buy_product_with_no_payment_expects_nothing(self):
        # Arrange

        # Act
        result = self.vending_machine.buy_product()

        # Assert
        assert_is_none(result)

    def test_buy_product_with_payment_expects_product(self):
        # Arrange
        self.vending_machine.insert_coin(1)

        # Act
        result = self.vending_machine.buy_product()

        # Assert
        assert_is_not_none(result)

    @raises(RuntimeError)
    def test_buy_product_with_no_payment_expects_exception(self):
        # Arrange

        # Act
        result = self.vending_machine.buy_product()

        # Assert

    @raises(RuntimeError)
    def test_buy_multiple_products_with_no_additional_payment(self):
        # Arrange

        # Act
        self.vending_machine.insert_coin(1)
        self.vending_machine.buy_product()
        self.vending_machine.buy_product()
class TestVendingMachine(unittest.TestCase):
    """Tests for vending machine"""
    def setUp(self):
        self.vending_machine = VendingMachine()

    def test_vending_machine_accepts_quarters(self):
        """should accept valid coins"""
        self.vending_machine.accept_coins(25)

        self.assertEqual(self.vending_machine.current_amount(), 25)

    def test_vending_machine_accepts_dimes(self):
        """should accept valid coins"""
        self.vending_machine.accept_coins(10)

        self.assertEqual(self.vending_machine.current_amount(), 10)

    def test_vending_machine_accepts_nickels(self):
        """should accept valid coins"""
        self.vending_machine.accept_coins(5)

        self.assertEqual(self.vending_machine.current_amount(), 5)

    def test_vending_machine_rejects_pennies_and_returns_them(self):
        """should reject and put pennies in coin return"""
        self.vending_machine.accept_coins(1)

        self.assertEqual(self.vending_machine.return_coins(), 1)

    def test_vending_machine_updates_display_when_valid_coin_accepted(self):
        """should update display when received valid coin"""
        self.vending_machine.accept_coins(25)

        self.assertEqual(self.vending_machine.display(), "Current Amount: %d" % self.vending_machine.current_amount())

    def test_vending_machine_displays_insert_coin_when_no_coins_inserted(self):
        """should display 'INSERT COIN'"""
        self.assertEqual(self.vending_machine.display(), "INSERT COINS")

    def test_vending_machine_has_stock(self):
        self.assertEqual(self.vending_machine.products, {"cola": {'price': 100, 'quantity': 5},
                                                         "chips": {'price': 50, 'quantity': 10},
                                                         "candy": {'price': 65, 'quantity': 10}})

    def test_product_is_dispensed_if_enough_money_is_received(self):
        """should dispense product if sufficient funds have been received"""
        for i in range(4):
            self.vending_machine.accept_coins(25)

        self.assertEqual(self.vending_machine.select_product("cola"), "cola")
        self.assertEqual(self.vending_machine.display(), "THANK YOU")

        self.assertEqual(self.vending_machine.display(), "INSERT COINS")
        self.assertEqual(self.vending_machine.current_amount(), 0)

    def test_machine_displays_price_if_insufficient_funds(self):
        """should display price of item if not enough funds have been received"""
        self.vending_machine.select_product("cola")

        self.assertEqual(self.vending_machine.display(), "PRICE: 100")
        self.assertEqual(self.vending_machine.display(), "INSERT COINS")

    def test_product_is_dispensed_and_money_is_returned_if_money_left_over(self):
        """should dispense products and give back any extra money"""
        for i in range(4):
            self.vending_machine.accept_coins(25)

        self.assertEqual(self.vending_machine.select_product("chips"), "chips")
        self.assertEqual(self.vending_machine.return_coins(), 50)

    def test_return_coins(self):
        """should return all coins"""
        self.vending_machine.accept_coins(25)
        self.vending_machine.refund_coins()

        self.assertEqual(self.vending_machine.return_coins(), 25)
        self.assertEqual(self.vending_machine.display(), "INSERT COINS")

    def test_machine_displays_sold_out_if_item_is_out_of_stock(self):
        """should display sold out if item is out of stock. Will display money left over"""
        self.vending_machine.products['cola']['quantity'] = 1

        for i in range(8):
            self.vending_machine.accept_coins(25)

        for i in range(2):
            self.vending_machine.select_product("cola")

        self.assertEqual(self.vending_machine.display(), "SOLD OUT")
        self.assertEqual(self.vending_machine.display(), "Current Amount: %d" % self.vending_machine.current_amount())

    def test_machine_displays_exact_change_only_when_not_able_to_make_change(self):
        """vending machine displays exact change if not able to make change"""
        self.vending_machine.change['nickels'] = 1

        for i in range(3):
            self.vending_machine.accept_coins(25)

        self.vending_machine.select_product("candy")
        self.vending_machine.make_change()

        self.assertEqual(self.vending_machine.display(), "EXACT CHANGE ONLY")

    def test_machine_should_update_adding_of_coins(self):
        """update coin quantity"""
        self.vending_machine.accept_coins(25)

        self.assertEqual(self.vending_machine.change['quarters']['quantity'], 6)
 def setUp(self):
     self.vending_machine = VendingMachine()