-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
142 lines (117 loc) · 3.81 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import sys
import unittest
from vendingmachine import VendingMachine, NotEnoughMoneyException, OutOfStockException, Product
class TestVendingMachine(unittest.TestCase):
def test_exists(self):
vm = 3
self.assertNotIsInstance(vm, VendingMachine)
vm = VendingMachine()
self.assertIsInstance(vm, VendingMachine)
def test_getInventory(self):
vm = VendingMachine()
expected = [1, 2, 3]
self.assertNotEquals(vm.getInventory(), expected)
expected = [[]]
self.assertEquals(vm.getInventory(), expected)
expected = [
[Product('cola'), Product('fanta')],
[Product('sprite'), Product('applejuice')]
]
vm._VendingMachine__inventory = expected
self.assertEquals(vm.getInventory(), expected)
def test_setInventory(self):
vm = VendingMachine()
expected = ['a']
vm.setInventory(expected)
self.assertEquals(vm.getInventory(), expected)
expected = [
[Product('cola'), Product('fanta')],
[Product('sprite'), Product('applejuice')]
]
vm.setInventory(expected)
self.assertEquals(vm.getInventory(), expected)
def test_getMoney(self):
vm = VendingMachine()
expected = 3
self.assertEquals(vm.inputMoney(3), expected)
expected = 6
self.assertEquals(vm.inputMoney(3), expected)
def test_getItem(self):
vm = VendingMachine()
inventory = [
[Product('cola', 2), Product('fanta', 3)],
[Product('fristi', 2), Product('applejuice')]
]
vm.setInventory(inventory)
expected = Product('fristi', 2)
print(expected)
print(vm.getItem(2, 1))
self.assertEqual(vm.getItem(2, 1), expected)
expected = Product('fanta', 3)
self.assertEqual(vm.getItem(1, 2), expected)
def test_getPrice(self):
vm = VendingMachine()
inventory = [
[Product('cola', 1), Product('fanta', 3)],
[Product('fristi', 2), Product('applejuice', 4)]
]
vm.setInventory(inventory)
expected = 1
self.assertEquals(vm.getPrice(1, 1), expected)
expected = 4
self.assertEquals(vm.getPrice(2, 2), expected)
def test_getStock(self):
vm = VendingMachine()
inventory = [
[Product('cola', stock=1), Product('fanta', stock=3)],
[Product('fristi', stock=2), Product('applejuice', stock=4)]
]
vm.setInventory(inventory)
expected = 1
self.assertEquals(vm.getStock(1, 1), expected)
expected = 4
self.assertEquals(vm.getStock(2, 2), expected)
def test_hasPaidEnough(self):
vm = VendingMachine()
inventory = [
[Product('cola', 1), Product('fanta', 3)],
[Product('fristi', 2), Product('applejuice', 4)]
]
vm.setInventory(inventory)
vm._VendingMachine__money = 1;
self.assertTrue(vm.hasPaidEnough(1, 1))
self.assertFalse(vm.hasPaidEnough(1, 2))
def test_inStock(self):
vm = VendingMachine()
inventory = [
[Product('cola', stock=1), Product('fanta', stock=3)],
[Product('fristi', stock=0), Product('applejuice', stock=4)]
]
vm.setInventory(inventory)
self.assertFalse(vm.inStock(2, 1))
self.assertTrue(vm.inStock(2, 2))
def test_deliverItem(self):
vm = VendingMachine()
inventory = [
[Product('cola', 1, 1), Product('fanta', 2, stock=3)],
[Product('fristi', 1, stock=0), Product('sprite', 4, 4)]
]
vm.setInventory(inventory)
self.assertRaises(OutOfStockException, vm.deliverItem, 2, 1)
# can't deliver if you haven't inputted money
self.assertRaises(NotEnoughMoneyException, vm.deliverItem, 1, 2)
vm.inputMoney(1)
self.assertRaises(NotEnoughMoneyException, vm.deliverItem, 1, 2)
vm.inputMoney(1)
# inputted money = 2, fanta price = 2
self.assertEquals(vm.deliverItem(1, 2), 'fanta')
self.assertRaises(NotEnoughMoneyException, vm.deliverItem, 1, 1)
vm.inputMoney(6)
# money = 6
self.assertEquals(vm.deliverItem(2, 2), 'sprite')
# money = 2
self.assertEquals(vm.deliverItem(1, 1), 'cola')
# money = 1, but cola is now out of stock
self.assertRaises(OutOfStockException, vm.deliverItem, 1, 1)
if __name__ == "__main__":
unittest.main()