-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpublic_tests.py
More file actions
408 lines (335 loc) · 14.3 KB
/
Copy pathpublic_tests.py
File metadata and controls
408 lines (335 loc) · 14.3 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
from unittest import TestCase, main
from datetime import date, timedelta
from importlib import reload
import db_handler as db
from MARIADB_CREDS import DB_CONFIG
from models.Item import Item
from models.Customer import Customer
# Fixed test IDs (16 chars each)
TEST_ITEM_ID = "PUBTEST_ITEM0000"
TEST_CUSTOMER_ID = "PUBTEST_CUST0000"
class PublicTests(TestCase):
@classmethod
def setUpClass(cls):
cls.db = reload(db)
@classmethod
def tearDownClass(cls):
try:
cls._reset(cls)
cls.db.cur.close()
cls.db.conn.close()
except Exception:
pass
def _reset(self):
self.db.cur.execute("DELETE FROM waitlist")
self.db.cur.execute("DELETE FROM rental")
self.db.cur.execute("DELETE FROM rental_history WHERE item_id = ?", (TEST_ITEM_ID,))
self.db.cur.execute("DELETE FROM item WHERE i_item_id = ?", (TEST_ITEM_ID,))
self.db.cur.execute("DELETE FROM customer WHERE c_customer_id = ?", (TEST_CUSTOMER_ID,))
self.db.conn.commit()
def setUp(self):
self._reset()
# -------------------------------------------------------------------------
# Static test data
# -------------------------------------------------------------------------
@staticmethod
def get_item():
return Item(
item_id=TEST_ITEM_ID,
product_name="Public Test Item",
brand="PublicBrand",
category="PublicCategory",
manufact="PublicManufact",
current_price=19.99,
start_year=2021,
num_owned=5,
)
@staticmethod
def get_customer():
return Customer(
customer_id=TEST_CUSTOMER_ID,
name="Public Tester",
email="public.tester@test.com",
address="5678 Test Ave, Gainesville, FL 32601",
)
def _insert_item(self):
item = self.get_item()
self.db.cur.execute(
"INSERT INTO item (i_item_sk, i_item_id, i_rec_start_date, i_product_name, "
"i_brand, i_class, i_category, i_manufact, i_current_price, i_num_owned) "
"VALUES ((SELECT COALESCE(MAX(i_item_sk), 0) + 1 FROM item AS tmp), "
"?, ?, ?, ?, NULL, ?, ?, ?, ?)",
(item.item_id, f"{item.start_year}-01-01", item.product_name,
item.brand, item.category, item.manufact, item.current_price, item.num_owned)
)
self.db.conn.commit()
return item
def _insert_customer(self):
customer = self.get_customer()
self.db.cur.execute(
"INSERT INTO customer_address "
"(ca_address_sk, ca_street_number, ca_street_name, ca_city, ca_state, ca_zip) "
"VALUES ((SELECT COALESCE(MAX(ca_address_sk), 0) + 1 FROM customer_address AS tmp), "
"?, ?, ?, ?, ?)",
("5678", "Test Ave", "Gainesville", "FL", "32601")
)
self.db.cur.execute("SELECT MAX(ca_address_sk) FROM customer_address")
addr_sk = self.db.cur.fetchone()[0]
self.db.cur.execute(
"INSERT INTO customer "
"(c_customer_sk, c_customer_id, c_first_name, c_last_name, c_email_address, c_current_addr_sk) "
"VALUES ((SELECT COALESCE(MAX(c_customer_sk), 0) + 1 FROM customer AS tmp), "
"?, ?, ?, ?, ?)",
(customer.customer_id, "Public", "Tester", customer.email, addr_sk)
)
self.db.conn.commit()
return customer
# -------------------------------------------------------------------------
# Tests
# -------------------------------------------------------------------------
def test_add_item(self):
new_item = self.get_item()
self.db.add_item(new_item=new_item)
self.db.cur.execute(
"SELECT i_item_id, i_product_name, i_brand, i_category, i_manufact, "
"i_current_price, YEAR(i_rec_start_date), i_num_owned "
"FROM item WHERE i_item_id = ?",
(new_item.item_id,)
)
row = self.db.cur.fetchone()
self.assertIsNotNone(row)
self.assertEqual(new_item.item_id, row[0].strip())
self.assertEqual(new_item.product_name, row[1].strip())
self.assertEqual(new_item.num_owned, row[7])
def test_add_customer(self):
new_customer = self.get_customer()
self.db.add_customer(new_customer=new_customer)
self.db.cur.execute(
"SELECT c_customer_id, TRIM(c_first_name), TRIM(c_last_name), TRIM(c_email_address) "
"FROM customer WHERE c_customer_id = ?",
(new_customer.customer_id,)
)
row = self.db.cur.fetchone()
self.assertIsNotNone(row)
self.assertEqual(new_customer.customer_id, row[0].strip())
self.assertEqual("Public", row[1])
self.assertEqual("Tester", row[2])
self.assertEqual(new_customer.email, row[3])
def test_edit_customer(self):
self._insert_customer()
updated = Customer(
customer_id="PUBTEST_EDIT0000",
name="Edited Name",
email="edited@test.com",
address="9999 New Rd, Tampa, FL 33601",
)
self.db.edit_customer(original_customer_id=TEST_CUSTOMER_ID, new_customer=updated)
# Old ID gone
self.db.cur.execute(
"SELECT c_customer_id FROM customer WHERE c_customer_id = ?", (TEST_CUSTOMER_ID,)
)
self.assertIsNone(self.db.cur.fetchone())
# New ID present
self.db.cur.execute(
"SELECT c_customer_id, TRIM(c_email_address) FROM customer WHERE c_customer_id = ?",
(updated.customer_id,)
)
row = self.db.cur.fetchone()
self.assertIsNotNone(row)
self.assertEqual(updated.customer_id, row[0].strip())
self.assertEqual(updated.email, row[1])
# Cleanup extra test customer
self.db.cur.execute(
"DELETE FROM customer WHERE c_customer_id = ?", (updated.customer_id,)
)
self.db.conn.commit()
def test_rent_item(self):
item = self._insert_item()
customer = self._insert_customer()
self.db.rent_item(item.item_id, customer.customer_id)
self.db.cur.execute(
"SELECT item_id, customer_id, rental_date, due_date FROM rental "
"WHERE item_id = ? AND customer_id = ?",
(item.item_id, customer.customer_id)
)
row = self.db.cur.fetchone()
self.assertIsNotNone(row)
today = date.today().isoformat()
due = (date.today() + timedelta(days=14)).isoformat()
self.assertEqual(item.item_id, row[0].strip())
self.assertEqual(customer.customer_id, row[1].strip())
self.assertEqual(today, str(row[2]))
self.assertEqual(due, str(row[3]))
def test_return_book(self):
item = self._insert_item()
customer = self._insert_customer()
today = date.today().isoformat()
due = (date.today() + timedelta(days=14)).isoformat()
self.db.cur.execute(
"INSERT INTO rental (item_id, customer_id, rental_date, due_date) VALUES (?, ?, ?, ?)",
(item.item_id, customer.customer_id, today, due)
)
self.db.conn.commit()
self.db.return_item(item_id=item.item_id, customer_id=customer.customer_id)
# Should be removed from rental
self.db.cur.execute(
"SELECT * FROM rental WHERE item_id = ? AND customer_id = ?",
(item.item_id, customer.customer_id)
)
self.assertIsNone(self.db.cur.fetchone())
# Should appear in rental_history
self.db.cur.execute(
"SELECT return_date FROM rental_history WHERE item_id = ? AND customer_id = ?",
(item.item_id, customer.customer_id)
)
row = self.db.cur.fetchone()
self.assertIsNotNone(row)
self.assertEqual(today, str(row[0]))
def test_grant_extension(self):
item = self._insert_item()
customer = self._insert_customer()
today = date.today().isoformat()
original_due = (date.today() + timedelta(days=14)).isoformat()
self.db.cur.execute(
"INSERT INTO rental (item_id, customer_id, rental_date, due_date) VALUES (?, ?, ?, ?)",
(item.item_id, customer.customer_id, today, original_due)
)
self.db.conn.commit()
self.db.grant_extension(item_id=item.item_id, customer_id=customer.customer_id)
self.db.cur.execute(
"SELECT due_date FROM rental WHERE item_id = ? AND customer_id = ?",
(item.item_id, customer.customer_id)
)
new_due = str(self.db.cur.fetchone()[0])
expected_due = (date.today() + timedelta(days=28)).isoformat()
self.assertEqual(expected_due, new_due)
def test_waitlist_customer(self):
item = self._insert_item()
customer = self._insert_customer()
place = self.db.waitlist_customer(item_id=item.item_id, customer_id=customer.customer_id)
self.assertEqual(1, place)
self.db.cur.execute(
"SELECT place_in_line FROM waitlist WHERE item_id = ? AND customer_id = ?",
(item.item_id, customer.customer_id)
)
row = self.db.cur.fetchone()
self.assertIsNotNone(row)
self.assertEqual(1, row[0])
def test_update_waitlist(self):
item = self._insert_item()
customer = self._insert_customer()
# Insert 2 entries
self.db.cur.execute(
"INSERT INTO waitlist (item_id, customer_id, place_in_line) VALUES (?, ?, ?)",
(item.item_id, customer.customer_id, 1)
)
self.db.cur.execute(
"INSERT INTO waitlist (item_id, customer_id, place_in_line) VALUES (?, ?, ?)",
(item.item_id, "PLACEHOLDER_CUST", 2) # 16 chars
)
self.db.conn.commit()
self.db.update_waitlist(item_id=item.item_id)
# Position 1 should be gone
self.db.cur.execute(
"SELECT customer_id FROM waitlist WHERE item_id = ?", (item.item_id,)
)
remaining = [row[0].strip() for row in self.db.cur.fetchall()]
self.assertNotIn(customer.customer_id, remaining)
self.assertIn("PLACEHOLDER_CUST", remaining)
# Remaining entry should now be at position 1
self.db.cur.execute(
"SELECT place_in_line FROM waitlist WHERE item_id = ? AND customer_id = ?",
(item.item_id, "PLACEHOLDER_CUST")
)
self.assertEqual(1, self.db.cur.fetchone()[0])
def test_get_filtered_items(self):
item = self._insert_item()
results = self.db.get_filtered_items(
filter_attributes=Item(item_id=item.item_id),
use_patterns=False
)
self.assertEqual(1, len(results))
self.assertEqual(item.item_id, results[0].item_id)
self.assertEqual(item.product_name, results[0].product_name)
self.assertEqual(item.num_owned, results[0].num_owned)
def test_get_filtered_items_patterns(self):
item = self._insert_item()
results = self.db.get_filtered_items(
filter_attributes=Item(product_name="Public%Item"),
use_patterns=True
)
actual_ids = [r.item_id for r in results]
self.assertIn(item.item_id, actual_ids)
def test_get_filtered_customers(self):
customer = self._insert_customer()
results = self.db.get_filtered_customers(
filter_attributes=Customer(customer_id=customer.customer_id)
)
self.assertEqual(1, len(results))
self.assertEqual(customer.customer_id, results[0].customer_id)
self.assertEqual(customer.email, results[0].email)
def test_get_filtered_customers_patterns(self):
customer = self._insert_customer()
results = self.db.get_filtered_customers(
filter_attributes=Customer(email="public.tester%"),
use_patterns=True
)
actual_ids = [r.customer_id for r in results]
self.assertIn(customer.customer_id, actual_ids)
def test_number_in_stock(self):
item = self._insert_item()
# No rentals — should equal num_owned
result = self.db.number_in_stock(item.item_id)
self.assertEqual(item.num_owned, result)
def test_place_in_line(self):
item = self._insert_item()
customer = self._insert_customer()
# Not on waitlist
self.assertEqual(-1, self.db.place_in_line(item.item_id, customer.customer_id))
# Add to waitlist
self.db.cur.execute(
"INSERT INTO waitlist (item_id, customer_id, place_in_line) VALUES (?, ?, ?)",
(item.item_id, customer.customer_id, 1)
)
self.db.conn.commit()
self.assertEqual(1, self.db.place_in_line(item.item_id, customer.customer_id))
def test_line_length(self):
item = self._insert_item()
customer = self._insert_customer()
self.assertEqual(0, self.db.line_length(item.item_id))
self.db.cur.execute(
"INSERT INTO waitlist (item_id, customer_id, place_in_line) VALUES (?, ?, ?)",
(item.item_id, customer.customer_id, 1)
)
self.db.conn.commit()
self.assertEqual(1, self.db.line_length(item.item_id))
def test_save_changes(self):
self.db.cur.execute(
"INSERT INTO customer (c_customer_sk, c_customer_id) "
"VALUES ((SELECT COALESCE(MAX(c_customer_sk), 0) + 1 FROM customer AS tmp), ?)",
(TEST_CUSTOMER_ID,)
)
self.db.save_changes()
self.db.cur.close()
self.db.conn.close()
self.db = reload(db)
self.db.cur.execute(
"SELECT c_customer_id FROM customer WHERE c_customer_id = ?", (TEST_CUSTOMER_ID,)
)
result = self.db.cur.fetchone()
self.assertEqual(TEST_CUSTOMER_ID, result[0].strip())
def test_close_connection(self):
from mariadb import connect
conn = connect(user=DB_CONFIG["username"], password=DB_CONFIG["password"],
host=DB_CONFIG["host"], database=DB_CONFIG["database"],
port=DB_CONFIG["port"])
cur = conn.cursor()
cur.execute("SHOW PROCESSLIST")
count_before = len(cur.fetchall())
self.db.close_connection()
cur.execute("SHOW PROCESSLIST")
count_after = len(cur.fetchall())
self.assertEqual(count_before - 1, count_after)
self.db = reload(db)
if __name__ == '__main__':
main()