-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_inventory.py
More file actions
290 lines (223 loc) · 10.7 KB
/
test_inventory.py
File metadata and controls
290 lines (223 loc) · 10.7 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import unittest
from product import Product
from supplier import Supplier
from order import Order
from inventory_manager import InventoryManager, HashMap, PriorityQueue
class TestProduct(unittest.TestCase):
"""Test cases for Product class"""
def setUp(self):
self.product = Product("P001", "Milk", "Dairy", 3.99, 50, 10, "S001")
def test_product_creation(self):
self.assertEqual(self.product.product_id, "P001")
self.assertEqual(self.product.name, "Milk")
self.assertEqual(self.product.price, 3.99)
self.assertEqual(self.product.quantity, 50)
def test_needs_reorder(self):
self.assertFalse(self.product.needs_reorder())
self.product.quantity = 5
self.assertTrue(self.product.needs_reorder())
def test_add_stock(self):
self.product.add_stock(20)
self.assertEqual(self.product.quantity, 70)
def test_remove_stock(self):
self.product.remove_stock(10)
self.assertEqual(self.product.quantity, 40)
def test_remove_stock_insufficient(self):
with self.assertRaises(ValueError):
self.product.remove_stock(100)
def test_negative_price(self):
with self.assertRaises(ValueError):
self.product.price = -5.00
def test_negative_quantity(self):
with self.assertRaises(ValueError):
self.product.quantity = -10
class TestSupplier(unittest.TestCase):
"""Test cases for Supplier class"""
def setUp(self):
self.supplier = Supplier("S001", "Fresh Foods Inc", "John Doe",
"555-1234", "john@freshfoods.com", "123 Main St")
def test_supplier_creation(self):
self.assertEqual(self.supplier.supplier_id, "S001")
self.assertEqual(self.supplier.name, "Fresh Foods Inc")
self.assertEqual(self.supplier.contact_person, "John Doe")
def test_empty_name(self):
with self.assertRaises(ValueError):
self.supplier.name = ""
def test_update_contact(self):
self.supplier.phone = "555-5678"
self.assertEqual(self.supplier.phone, "555-5678")
class TestOrder(unittest.TestCase):
"""Test cases for Order class"""
def setUp(self):
self.order1 = Order("P001", 100, priority=3)
self.order2 = Order("P002", 50, priority=1)
self.order3 = Order("P003", 75, priority=5)
def test_order_creation(self):
self.assertTrue(self.order1.order_id.startswith("ORD"))
self.assertEqual(self.order1.product_id, "P001")
self.assertEqual(self.order1.quantity, 100)
self.assertEqual(self.order1.priority, 3)
self.assertEqual(self.order1.status, "Pending")
def test_priority_comparison(self):
self.assertTrue(self.order2 < self.order1)
self.assertTrue(self.order1 < self.order3)
def test_invalid_priority(self):
with self.assertRaises(ValueError):
self.order1.priority = 15
def test_invalid_quantity(self):
with self.assertRaises(ValueError):
self.order1.quantity = -10
def test_status_update(self):
self.order1.status = "Shipped"
self.assertEqual(self.order1.status, "Shipped")
self.order1.status = "Received"
self.assertIsNotNone(self.order1.received_date)
class TestHashMap(unittest.TestCase):
"""Test cases for HashMap data structure"""
def setUp(self):
self.hashmap = HashMap()
def test_put_and_get(self):
self.hashmap.put("key1", "value1")
self.assertEqual(self.hashmap.get("key1"), "value1")
def test_update_value(self):
self.hashmap.put("key1", "value1")
self.hashmap.put("key1", "value2")
self.assertEqual(self.hashmap.get("key1"), "value2")
self.assertEqual(self.hashmap.size(), 1)
def test_remove(self):
self.hashmap.put("key1", "value1")
self.assertTrue(self.hashmap.remove("key1"))
self.assertIsNone(self.hashmap.get("key1"))
def test_contains(self):
self.hashmap.put("key1", "value1")
self.assertTrue(self.hashmap.contains("key1"))
self.assertFalse(self.hashmap.contains("key2"))
def test_keys_values_items(self):
self.hashmap.put("k1", "v1")
self.hashmap.put("k2", "v2")
self.assertEqual(len(self.hashmap.keys()), 2)
self.assertEqual(len(self.hashmap.values()), 2)
self.assertEqual(len(self.hashmap.items()), 2)
def test_resize(self):
for i in range(20):
self.hashmap.put(f"key{i}", f"value{i}")
self.assertEqual(self.hashmap.size(), 20)
for i in range(20):
self.assertEqual(self.hashmap.get(f"key{i}"), f"value{i}")
class TestPriorityQueue(unittest.TestCase):
"""Test cases for PriorityQueue data structure"""
def setUp(self):
self.pq = PriorityQueue()
self.order1 = Order("P001", 100, priority=5)
self.order2 = Order("P002", 50, priority=2)
self.order3 = Order("P003", 75, priority=8)
def test_enqueue_dequeue(self):
self.pq.enqueue(self.order1)
self.assertEqual(self.pq.dequeue(), self.order1)
def test_priority_order(self):
self.pq.enqueue(self.order1)
self.pq.enqueue(self.order2)
self.pq.enqueue(self.order3)
self.assertEqual(self.pq.dequeue(), self.order2)
self.assertEqual(self.pq.dequeue(), self.order1)
self.assertEqual(self.pq.dequeue(), self.order3)
def test_peek(self):
self.pq.enqueue(self.order1)
self.pq.enqueue(self.order2)
self.assertEqual(self.pq.peek(), self.order2)
self.assertEqual(self.pq.size(), 2)
def test_empty_queue(self):
self.assertTrue(self.pq.is_empty())
with self.assertRaises(IndexError):
self.pq.dequeue()
class TestInventoryManager(unittest.TestCase):
"""Test cases for InventoryManager"""
def setUp(self):
self.manager = InventoryManager()
self.product1 = Product("P001", "Milk", "Dairy", 3.99, 50, 10, "S001")
self.product2 = Product("P002", "Bread", "Bakery", 2.49, 30, 15, "S002")
self.product3 = Product("P003", "Apple", "Produce", 1.99, 100, 20, "S001")
self.supplier1 = Supplier("S001", "Fresh Foods", "John", "555-1234",
"john@ff.com", "123 Main")
def test_add_product(self):
self.manager.add_product(self.product1)
self.assertEqual(self.manager.get_product("P001"), self.product1)
def test_add_duplicate_product(self):
self.manager.add_product(self.product1)
with self.assertRaises(ValueError):
self.manager.add_product(self.product1)
def test_get_all_products(self):
self.manager.add_product(self.product1)
self.manager.add_product(self.product2)
self.assertEqual(len(self.manager.get_all_products()), 2)
def test_remove_product(self):
self.manager.add_product(self.product1)
self.assertTrue(self.manager.remove_product("P001"))
self.assertIsNone(self.manager.get_product("P001"))
def test_low_stock_products(self):
self.product1.quantity = 5
self.manager.add_product(self.product1)
self.manager.add_product(self.product2)
low_stock = self.manager.get_low_stock_products()
self.assertEqual(len(low_stock), 1)
self.assertEqual(low_stock[0].product_id, "P001")
def test_add_supplier(self):
self.manager.add_supplier(self.supplier1)
self.assertEqual(self.manager.get_supplier("S001"), self.supplier1)
def test_create_order(self):
self.manager.add_product(self.product1)
order = self.manager.create_order("P001", 100, priority=3)
self.assertIsNotNone(order)
self.assertEqual(order.product_id, "P001")
def test_process_order(self):
self.manager.add_product(self.product1)
self.manager.create_order("P001", 100, priority=5)
self.manager.create_order("P001", 50, priority=2)
order = self.manager.process_order()
self.assertEqual(order.priority, 2)
def test_receive_order(self):
self.manager.add_product(self.product1)
initial_qty = self.product1.quantity
order = self.manager.create_order("P001", 50)
self.manager.receive_order(order)
self.assertEqual(self.product1.quantity, initial_qty + 50)
self.assertEqual(order.status, "Received")
def test_sort_products_by_name(self):
self.manager.add_product(self.product2)
self.manager.add_product(self.product1)
self.manager.add_product(self.product3)
sorted_products = self.manager.sort_products_by_name()
self.assertEqual(sorted_products[0].name, "Apple")
self.assertEqual(sorted_products[1].name, "Bread")
self.assertEqual(sorted_products[2].name, "Milk")
def test_sort_products_by_price(self):
self.manager.add_product(self.product1)
self.manager.add_product(self.product2)
self.manager.add_product(self.product3)
sorted_products = self.manager.sort_products_by_price()
self.assertEqual(sorted_products[0].price, 1.99)
self.assertEqual(sorted_products[2].price, 3.99)
def test_search_by_category(self):
self.manager.add_product(self.product1)
self.manager.add_product(self.product2)
self.manager.add_product(self.product3)
dairy_products = self.manager.search_products_by_category("Dairy")
self.assertEqual(len(dairy_products), 1)
self.assertEqual(dairy_products[0].name, "Milk")
def test_search_by_name(self):
self.manager.add_product(self.product1)
self.manager.add_product(self.product2)
results = self.manager.search_products_by_name("mil")
self.assertEqual(len(results), 1)
self.assertEqual(results[0].name, "Milk")
def test_inventory_value(self):
self.manager.add_product(self.product1)
self.manager.add_product(self.product2)
expected_value = (3.99 * 50) + (2.49 * 30)
self.assertAlmostEqual(self.manager.get_total_inventory_value(), expected_value, places=2)
def test_inventory_count(self):
self.manager.add_product(self.product1)
self.manager.add_product(self.product2)
self.assertEqual(self.manager.get_inventory_count(), 2)
if __name__ == '__main__':
unittest.main()