-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_vaccine_reservation_scheduler.py
313 lines (261 loc) · 14.3 KB
/
test_vaccine_reservation_scheduler.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
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
import unittest
import os
from sql_connection_manager import SqlConnectionManager
from enums import *
from utils import *
from vaccine_reservation_scheduler import VaccineReservationScheduler as scheduler
from vaccine_caregiver import VaccineCaregiver
from COVID19_vaccine import COVID19Vaccine as covid
from VaccinePatient import VaccinePatient as patient
class TestReservationScheduler(unittest.TestCase):
def test_init(self):
scheduler()
def test_put_on_hold1_returns_neg2(self):
"""PutOnHold returns -2"""
with SqlConnectionManager(Server=os.getenv("Server"),
DBname=os.getenv("DBName"),
UserId=os.getenv("UserID"),
Password=os.getenv("Password")) as sqlClient:
clear_tables(sqlClient)
# get a cursor from the SQL connection
with sqlClient.cursor(as_dict=True) as cursor:
self.assertEqual(scheduler().PutHoldOnAppointment1(cursor), -2)
# check no updates made to table
get_schedule_sql = "SELECT * FROM CareGiverSchedule WHERE SlotStatus = 1"
cursor.execute(get_schedule_sql)
rows = cursor.fetchall()
self.assertTrue(len(rows) < 1)
clear_tables(sqlClient)
def test_put_on_hold1(self):
"""PutOnHold returns id"""
with SqlConnectionManager(Server=os.getenv("Server"),
DBname=os.getenv("DBName"),
UserId=os.getenv("UserID"),
Password=os.getenv("Password")) as sqlClient:
clear_tables(sqlClient)
# get a cursor from the SQL connection
with sqlClient.cursor(as_dict=True) as cursor:
vc = VaccineCaregiver('Carrie Nation', cursor)
self.assertEqual(scheduler().PutHoldOnAppointment1(cursor), 0)
# check 1 update made to table
get_schedule_sql = "SELECT * FROM CareGiverSchedule WHERE SlotStatus = 1"
cursor.execute(get_schedule_sql)
rows = cursor.fetchall()
self.assertTrue(len(rows) == 1)
clear_tables(sqlClient)
def test_put_on_hold2(self):
"""PutOnHold2 returns id"""
with SqlConnectionManager(Server=os.getenv("Server"),
DBname=os.getenv("DBName"),
UserId=os.getenv("UserID"),
Password=os.getenv("Password")) as sqlClient:
clear_tables(sqlClient)
# get a cursor from the SQL connection
with sqlClient.cursor(as_dict=True) as cursor:
vc = VaccineCaregiver('Carrie Nation', cursor)
scheduler().PutHoldOnAppointment2(1, 21, cursor)
# check 1 update made to table
get_schedule_sql = "SELECT * FROM CareGiverSchedule WHERE SlotStatus = 1"
cursor.execute(get_schedule_sql)
rows = cursor.fetchall()
self.assertTrue(len(rows) == 1)
clear_tables(sqlClient)
def test_put_on_hold2_returns_neg2(self):
"""PutOnHold2 returns -2"""
with SqlConnectionManager(Server=os.getenv("Server"),
DBname=os.getenv("DBName"),
UserId=os.getenv("UserID"),
Password=os.getenv("Password")) as sqlClient:
clear_tables(sqlClient)
# get a cursor from the SQL connection
with sqlClient.cursor(as_dict=True) as cursor:
vc = VaccineCaregiver('Carrie Nation', cursor)
self.assertEqual(scheduler().PutHoldOnAppointment2(1, 100, cursor), -2)
# check no updates made to table
get_schedule_sql = "SELECT * FROM CareGiverSchedule WHERE SlotStatus = 1"
cursor.execute(get_schedule_sql)
rows = cursor.fetchall()
self.assertTrue(len(rows) < 1)
clear_tables(sqlClient)
def test_schedule_appointment_returns_neg2(self):
"""ScheduleAppointmentSlot returns -2"""
with SqlConnectionManager(Server=os.getenv("Server"),
DBname=os.getenv("DBName"),
UserId=os.getenv("UserID"),
Password=os.getenv("Password")) as sqlClient:
clear_tables(sqlClient)
with sqlClient.cursor(as_dict=True) as cursor:
vc = VaccineCaregiver('Carrie Nation', cursor)
self.assertEqual(scheduler().ScheduleAppointmentSlot(-1, 1, cursor), -2)
self.assertEqual(scheduler().ScheduleAppointmentSlot("Not a valid id", 1, cursor), -2)
get_schedule_sql = "SELECT * FROM CareGiverSchedule WHERE SlotStatus = 2"
cursor.execute(get_schedule_sql)
rows = cursor.fetchall()
self.assertTrue(len(rows) < 1)
clear_tables(sqlClient)
def test_schedule_appointment(self):
"""ScheduleAppointmentSlot returns id"""
with SqlConnectionManager(Server=os.getenv("Server"),
DBname=os.getenv("DBName"),
UserId=os.getenv("UserID"),
Password=os.getenv("Password")) as sqlClient:
clear_tables(sqlClient)
with sqlClient.cursor(as_dict=True) as cursor:
vc = VaccineCaregiver('Carrie Nation', cursor)
vaccine = covid('Moderna', 'Moderna', 10, 10, 28, 2, cursor)
sqlCreatePatient = "INSERT INTO Patients (PatientName, VaccineStatus) VALUES ("
sqlCreatePatient += "'Patrick Render', 0)"
cursor.execute(sqlCreatePatient)
sqlCreateAppt = "INSERT INTO VaccineAppointments ("
sqlCreateAppt += "VaccineName, PatientId, CaregiverId, SlotStatus) VALUES ("
sqlCreateAppt += "'Moderna', "
sqlCreateAppt += "1, "
sqlCreateAppt += str(vc.caregiverId) + ", "
sqlCreateAppt += "1)"
cursor.execute(sqlCreateAppt)
self.assertEqual(scheduler().ScheduleAppointmentSlot(1, 0, cursor), 1)
# Caregiverschedule is updated once
get_schedule_sql = "SELECT * FROM CareGiverSchedule WHERE SlotStatus = 2"
cursor.execute(get_schedule_sql)
rows = cursor.fetchall()
self.assertTrue(len(rows) == 1)
# VaccineAppointments table is updated once
get_appointments_sql = "SELECT * FROM VaccineAppointments WHERE SlotStatus = 2"
cursor.execute(get_appointments_sql)
rows = cursor.fetchall()
self.assertTrue(len(rows) == 1)
clear_tables(sqlClient)
class TestScenarios(unittest.TestCase):
def test_main_two_patients(self):
'''
Add 2 caregivers, 2 patients, and one vaccine
Try to schedule patients in order added to db
Successfully schedule first appointment
Successfuly hold second appointment
Successfully reduce vaccine reserve by 4
'''
with SqlConnectionManager(Server=os.getenv("Server"),
DBname=os.getenv("DBName"),
UserId=os.getenv("UserID"),
Password=os.getenv("Password")) as sqlClient:
clear_tables(sqlClient)
with sqlClient.cursor(as_dict=True) as dbcursor:
vrs = scheduler()
caregiversList = []
caregiversList.append(VaccineCaregiver('Carrie Nation', dbcursor))
caregiversList.append(VaccineCaregiver('Clare Barton', dbcursor))
vaccine = covid('Moderna', 'Moderna', 0, 0, 28, 2, dbcursor)
covid.add_doses('Moderna', 5, dbcursor)
# Add patients
patientList = []
patientList.append(patient('Spongebob Squarepants', 0, dbcursor))
patientList.append(patient('Sandy Cheeks', 0, dbcursor))
# Schedule the patients
for p in patientList:
try:
# PutHoldOn
cg_first_slot = vrs.PutHoldOnAppointment1(dbcursor)
# Reserve appointment
first_appt = p.ReserveAppointment(cg_first_slot, vaccine, dbcursor)
# Schedule appointment
p.ScheduleAppointment(cg_first_slot, first_appt, vaccine, dbcursor)
# commit
dbcursor.connection.commit()
except Exception as e:
err_str = "Oops! An exception occurred. The transaction for patient "
err_str += str(p.patientId) + ": " + str(p.patientName) + " "
err_str += "was rolled back."
print(err_str)
print(e)
dbcursor.connection.rollback()
self.fail()
# Check that transactions went through
check_patients = "SELECT * FROM Patients"
check_vaccines = "SELECT * FROM Vaccines"
check_cgs = "SELECT CaregiverSlotSchedulingId, CaregiverId, WorkDay, SlotStatus, VaccineAppointmentId "
check_cgs += "FROM CaregiverSchedule where SlotStatus IN (1, 2)"
check_appts = "SELECT * FROM VaccineAppointments"
# exactly 2 patients
dbcursor.execute(check_patients)
rows = dbcursor.fetchall()
self.assertEqual(len(rows), 2)
for row in rows:
self.assertEqual(row['VaccineStatus'], 2)
# exactly 1 vaccine, 4 reserved doses, 2 per patient
dbcursor.execute(check_vaccines)
rows = dbcursor.fetchall()
self.assertEqual(len(rows), 1) # exactly 1 vaccine
self.assertEqual(rows[0]['ReservedDoses'], 4)
# 4 caregiver slots with slotstatus 1 or 2
dbcursor.execute(check_cgs)
rows = dbcursor.fetchall()
self.assertEqual(len(rows), 4)
# 4 vaccine appointments
dbcursor.execute(check_appts)
rows = dbcursor.fetchall()
self.assertEqual(len(rows), 4)
clear_tables(sqlClient)
def test_main_five_patients(self):
with SqlConnectionManager(Server=os.getenv("Server"),
DBname=os.getenv("DBName"),
UserId=os.getenv("UserID"),
Password=os.getenv("Password")) as sqlClient:
clear_tables(sqlClient)
with sqlClient.cursor(as_dict=True) as dbcursor:
vrs = scheduler()
caregiversList = []
caregiversList.append(VaccineCaregiver('Carrie Nation', dbcursor))
caregiversList.append(VaccineCaregiver('Clare Barton', dbcursor))
vaccine = covid('Moderna', 'Moderna', 0, 0, 28, 2, dbcursor)
covid.add_doses('Moderna', 5, dbcursor)
# Add patients
patientList = []
patientList.append(patient('Spongebob Squarepants', 0, dbcursor))
patientList.append(patient('Sandy Cheeks', 0, dbcursor))
patientList.append(patient('Squidward', 0, dbcursor))
patientList.append(patient('Patrick Star', 0, dbcursor))
patientList.append(patient('Mr. Krabs', 0, dbcursor))
# Schedule the patients
for p in patientList:
try:
# PutHoldOn
cg_first_slot = vrs.PutHoldOnAppointment1(dbcursor)
# Reserve appointment
first_appt = p.ReserveAppointment(cg_first_slot, vaccine, dbcursor)
# Schedule appointment
p.ScheduleAppointment(cg_first_slot, first_appt, vaccine, dbcursor)
# commit
dbcursor.connection.commit()
except Exception as e:
err_str = "Oops! An exception occurred. The transaction for patient "
err_str += str(p.patientId) + ": " + str(p.patientName) + " "
err_str += "was rolled back."
print(err_str)
print(e)
dbcursor.connection.rollback()
# Check that transactions went through
check_patients = "SELECT * FROM Patients"
check_vaccines = "SELECT * FROM Vaccines"
check_cgs = "SELECT CaregiverSlotSchedulingId, CaregiverId, WorkDay, SlotStatus, VaccineAppointmentId "
check_cgs += "FROM CaregiverSchedule where SlotStatus IN (1, 2)"
check_appts = "SELECT * FROM VaccineAppointments"
# exactly 5 patients
dbcursor.execute(check_patients)
rows = dbcursor.fetchall()
self.assertEqual(len(rows), 5)
# exactly 1 vaccine, 4 reserved doses, 2 per successful patient
dbcursor.execute(check_vaccines)
rows = dbcursor.fetchall()
self.assertEqual(len(rows), 1) # exactly 1 vaccine
self.assertEqual(rows[0]['ReservedDoses'], 4)
# 4 caregiver slots with slotstatus 1 or 2
dbcursor.execute(check_cgs)
rows = dbcursor.fetchall()
self.assertEqual(len(rows), 4)
# 4 vaccine appointments
dbcursor.execute(check_appts)
rows = dbcursor.fetchall()
self.assertEqual(len(rows), 4)
clear_tables(sqlClient)
if __name__ == "__main__":
unittest.main()