Пример #1
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)
Пример #2
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)