-
Notifications
You must be signed in to change notification settings - Fork 0
/
unittests.py
384 lines (360 loc) · 13 KB
/
unittests.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
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
import numpy as np
from dlai_grader.grading import test_case, print_feedback
from types import FunctionType
from dlai_grader.io import suppress_stdout_stderr
import os
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey, Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker
persons_data = np.array([("Alice", "New York", "Non-binary", 30),
("Bob", "Los Angeles", "Male", 18),
("Charlie", "Chicago", "Male", 60),
("David", "Houston", "Male", 59),
("Eve", "Phoenix", "Non-binary", 18),
("Frank", "Los Angeles", "Non-binary", 72),
("Grace", "Chicago", "Female", 35),
("Henry", "Houston", "Male", 21),
("Ivy", "New York", "Female", 46),
("Elena", "Phoenix", "Female", 66)])
#locations = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]
#genders = ["Male", "Female", "Non-binary"]
club_descriptions = [
"Book Club", "Hiking Club", "Chess Club", "Photography Club", "Cooking Club",
"Music Club", "Gaming Club", "Fitness Club", "Art Club", "Travel Club"
]
def test_load_dataset(function):
def g():
function_name = function.__name__
cases = []
# Check if function is a function method exists
t = test_case()
if not isinstance(function, FunctionType):
t.failed = True
t.msg = f"{function_name} is not a function"
t.want = f"{function_name} must be a function"
t.got = f"Type of {function_name} is {type(function_name)}"
return [t]
if 'tmp' not in os.listdir("/"):
os.mkdir('/tmp')
# List all files in the directory
for filename in os.listdir('/tmp'):
# Create the full file path
file_path = os.path.join('/tmp', filename)
# Check if it is a file (and not a directory/subdirectory)
if os.path.isfile(file_path):
# Delete the file silently
os.remove(file_path)
with suppress_stdout_stderr():
session, Club, Person, friendships = function(path = '/tmp')
persons = session.query(Person).all()
t = test_case()
if len(persons) != 10:
t.failed = True
t.msg = "Incorrect number of persons in the database"
t.want = 10
t.got = len(persons)
return [t]
names = persons_data[:,0]
learner_names = [x.name for x in persons]
names.sort()
learner_names.sort()
t = test_case()
if list(names) != learner_names:
t.failed = True
t.msg = "Incorrect persons in dataset"
t.want = f"Set of persons: {names}"
t.got = f"Set of persons: {learner_names}"
return [t]
query = session.query(Person.name, Person.location, Person.gender, Person.age)
persons_list = query.all()
t = test_case()
for person in persons_list:
if not isinstance(person[-1], int):
t.failed = True
t.msg = "Incorrect type for persons age"
t.want = "Integer"
t.got = type(person[-1])
cases.append(t)
return [t]
persons_list = np.array(persons_list)
persons_list.sort(axis = 0)
persons_data.sort(axis = 0)
for person_learner, person_solution in zip(persons_list, persons_data):
name_learner,location_learner,gender_learner,age_learner = person_learner
name_solution,location_solution,gender_solution,age_solution = person_solution
t = test_case()
if location_learner != location_solution:
t.failed = True
t.msg = f"Incorrect location for person {name_solution}"
t.want = f"{location_solution}"
t.got = f"{location_learner}"
cases.append(t)
t = test_case()
if gender_learner != gender_solution:
t.failed = True
t.msg = f"Incorrect gender for person {name_solution}"
t.want = f"{gender_solution}"
t.got = f"{gender_learner}"
cases.append(t)
if age_learner != age_solution:
t.failed = True
t.msg = f"Incorrect age for person {name_solution}"
t.want = f"{age_solution}"
t.got = f"{age_learner}"
cases.append(t)
return cases
cases = g()
print_feedback(cases)
def test_get_club_members(load_dataset, function):
correct = {'Book Club': ['Eve', 'Alice', 'Grace', 'Frank', 'Charlie', 'Elena'],
'Hiking Club': ['Frank', 'Ivy', 'Eve', 'Alice', 'David', 'Elena'],
'Chess Club': ['Alice', 'Eve', 'Grace', 'Elena', 'Frank', 'David'],
'Photography Club': ['David', 'Elena', 'Charlie', 'Alice'],
'Cooking Club': ['David', 'Henry', 'Grace', 'Bob', 'Alice', 'Charlie'],
'Music Club': ['Alice', 'Charlie', 'Eve', 'Henry'],
'Gaming Club': ['Bob', 'Charlie', 'Grace', 'Alice'],
'Fitness Club': ['Henry', 'Elena', 'Bob', 'Charlie'],
'Art Club': ['Grace', 'David', 'Elena', 'Eve', 'Bob'],
'Travel Club': ['Henry', 'David', 'Ivy', 'Eve', 'Elena']}
def g():
function_name = function.__name__
cases = []
# Check if function is a function method exists
t = test_case()
if not isinstance(function, FunctionType):
t.failed = True
t.msg = f"{function_name} is not a function"
t.want = f"{function_name} must be a function"
t.got = f"Type of {function_name} is {type(function_name)}"
return [t]
if 'tmp' not in os.listdir("/"):
os.mkdir('/tmp')
# List all files in the directory
for filename in os.listdir('/tmp'):
# Create the full file path
file_path = os.path.join('/tmp', filename)
# Check if it is a file (and not a directory/subdirectory)
if os.path.isfile(file_path):
# Delete the file silently
os.remove(file_path)
with suppress_stdout_stderr():
session, Club, Person, friendships = load_dataset(path = '/tmp')
learner = {}
for c in correct.keys():
t = test_case()
try:
members = [x.name for x in function(session, c)]
learner[c] = members
except Exception as e:
t.failed = True
t.msg = f"Failed execution for club description {c}"
t.want = "Function must run properly"
t.got = f"Exception thrown: {e}"
return [t]
for c in correct.keys():
t = test_case()
learner_list = learner[c]
solution_list = correct[c]
learner_list.sort()
solution_list.sort()
if learner_list != solution_list:
t.failed = True
t.msg = f"Incorrect club members for club description {c}"
t.want = f"{solution_list}"
t.got = f"{learner_list}"
cases.append(t)
session.close()
return cases
cases = g()
print_feedback(cases)
def test_get_friends_of_person(load_dataset, function):
correct = {'Alice': ['Bob', 'Charlie', 'David', 'Eve', 'Henry', 'Ivy', 'Elena'],
'Bob': ['Alice', 'David', 'Eve', 'Frank', 'Henry', 'Ivy', 'Elena'],
'Charlie': ['Alice',
'Bob',
'David',
'Frank',
'Grace',
'Henry',
'Ivy',
'Elena'],
'David': ['Alice',
'Bob',
'Charlie',
'Eve',
'Frank',
'Grace',
'Henry',
'Ivy',
'Elena'],
'Eve': ['Alice', 'Charlie', 'David', 'Frank', 'Grace', 'Henry', 'Elena'],
'Frank': ['Alice',
'Bob',
'Charlie',
'David',
'Eve',
'Grace',
'Henry',
'Elena'],
'Grace': ['Alice', 'Bob', 'David', 'Eve', 'Frank', 'Henry', 'Ivy', 'Elena'],
'Henry': ['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Grace', 'Elena'],
'Ivy': ['Alice',
'Bob',
'Charlie',
'David',
'Eve',
'Frank',
'Grace',
'Henry',
'Elena'],
'Elena': ['Alice',
'Charlie',
'David',
'Eve',
'Frank',
'Grace',
'Henry',
'Ivy']}
def g():
function_name = function.__name__
cases = []
# Check if function is a function method exists
t = test_case()
if not isinstance(function, FunctionType):
t.failed = True
t.msg = f"{function_name} is not a function"
t.want = f"{function_name} must be a function"
t.got = f"Type of {function_name} is {type(function_name)}"
return [t]
if 'tmp' not in os.listdir("/"):
os.mkdir('/tmp')
# List all files in the directory
for filename in os.listdir('/tmp'):
# Create the full file path
file_path = os.path.join('/tmp', filename)
# Check if it is a file (and not a directory/subdirectory)
if os.path.isfile(file_path):
# Delete the file silently
os.remove(file_path)
with suppress_stdout_stderr():
session, Club, Person, friendships = load_dataset(path = '/tmp')
learner = {}
for c in correct.keys():
t = test_case()
try:
members = [x.name for x in function(session, c)]
learner[c] = members
except Exception as e:
t.failed = True
t.msg = f"Failed execution for club description {c}"
t.want = "Function must run properly"
t.got = f"Exception thrown: {e}"
return [t]
for c in correct.keys():
t = test_case()
learner_list = learner[c]
solution_list = correct[c]
learner_list.sort()
solution_list.sort()
if learner_list != solution_list:
t.failed = True
t.msg = f"Incorrect friends for person {c}"
t.want = f"{solution_list}"
t.got = f"{learner_list}"
cases.append(t)
return cases
cases = g()
print_feedback(cases)
def test_get_persons_who_consider_them_friend(load_dataset, function):
correct = {'Alice': ['Eve',
'Ivy',
'Charlie',
'Henry',
'Bob',
'Frank',
'David',
'Grace',
'Elena'],
'Bob': ['Frank', 'Ivy', 'David', 'Grace', 'Alice', 'Charlie', 'Henry'],
'Charlie': ['Henry', 'Elena', 'Ivy', 'Eve', 'Frank', 'Alice', 'David'],
'David': ['Grace',
'Bob',
'Henry',
'Alice',
'Frank',
'Ivy',
'Elena',
'Eve',
'Charlie'],
'Eve': ['Henry', 'Frank', 'Grace', 'Elena', 'Bob', 'Alice', 'David', 'Ivy'],
'Frank': ['Elena', 'Bob', 'David', 'Charlie', 'Ivy', 'Eve', 'Grace'],
'Grace': ['Charlie', 'Ivy', 'Elena', 'David', 'Frank', 'Henry', 'Eve'],
'Henry': ['David',
'Bob',
'Grace',
'Ivy',
'Charlie',
'Frank',
'Elena',
'Alice',
'Eve'],
'Ivy': ['Elena', 'Grace', 'Alice', 'David', 'Charlie', 'Bob'],
'Elena': ['Grace',
'Frank',
'Bob',
'Henry',
'Eve',
'Ivy',
'David',
'Charlie',
'Alice']}
def g():
function_name = function.__name__
cases = []
# Check if function is a function method exists
t = test_case()
if not isinstance(function, FunctionType):
t.failed = True
t.msg = f"{function_name} is not a function"
t.want = f"{function_name} must be a function"
t.got = f"Type of {function_name} is {type(function_name)}"
return [t]
if 'tmp' not in os.listdir("/"):
os.mkdir('/tmp')
# List all files in the directory
for filename in os.listdir('/tmp'):
# Create the full file path
file_path = os.path.join('/tmp', filename)
# Check if it is a file (and not a directory/subdirectory)
if os.path.isfile(file_path):
# Delete the file silently
os.remove(file_path)
with suppress_stdout_stderr():
session, Club, Person, friendships = load_dataset(path = '/tmp')
learner = {}
for c in correct.keys():
t = test_case()
try:
members = [x.name for x in function(session, c)]
learner[c] = members
except Exception as e:
t.failed = True
t.msg = f"Failed execution for club description {c}"
t.want = "Function must run properly"
t.got = f"Exception thrown: {e}"
return [t]
for c in correct.keys():
t = test_case()
learner_list = learner[c]
solution_list = correct[c]
learner_list.sort()
solution_list.sort()
if learner_list != solution_list:
t.failed = True
t.msg = f"Incorrect persons who consider {c} a friend"
t.want = f"{solution_list}"
t.got = f"{learner_list}"
cases.append(t)
return cases
cases = g()
print_feedback(cases)