forked from Code-Platoon-Assignments/django-school-api-V
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
502 lines (466 loc) · 17.6 KB
/
tests.py
File metadata and controls
502 lines (466 loc) · 17.6 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
from django.test import TestCase
from grade_app.models import Grade, Student, Subject
from django.core.exceptions import ValidationError
from django.db import IntegrityError, DataError
from .serializers import StudentAllSerializer, StudentSerializer
# Create your tests here.
## PART I
class Test_student(TestCase):
def test_001_student_with_improper_good_student_field(self):
try:
new_student = Student.objects.create(
name="John W. Watson",
student_email="thisIsAnEmail@school.com",
personal_email="thisIsAnEmail@gmail.com",
locker_number=13,
locker_combination="12-33-44",
good_student=None,
)
new_student.full_clean()
self.fail()
except IntegrityError as e:
# print(e)
self.assertIn(
'null value in column "good_student" of relation "student_app_student" violates not-null constraint',
str(e),
)
def test_002_student_with_improper_email_fields(self):
try:
new_student = Student.objects.create(
name="John W. Watson",
student_email="thisIsNotAnEmail",
personal_email=False,
locker_number=13,
locker_combination="23-33-44",
good_student=True,
)
new_student.full_clean()
self.fail()
except ValidationError as e:
# print(e.message_dict)
self.assert_(
"student_email" in e.message_dict and "personal_email" in e.message_dict
)
def test_003_student_with_improper_locker_number_fields(self):
try:
new_student = Student.objects.create(
name="John W. Watson",
student_email="thisIsAnEmail@school.com",
personal_email="thisIsAnEmail@gmail.com",
locker_number="None",
locker_combination="23-33-44",
good_student=True,
)
new_student.full_clean()
self.fail()
except Exception as e:
# print(e)
self.assert_(
"Field 'locker_number' expected a number but got 'None'" in str(e)
)
def test_004_student_with_improper_locker_combination_fields(self):
try:
new_student = Student.objects.create(
name="John W. Watson",
student_email="thisIsAnEmail@school.com",
personal_email="thisIsAnEmail@gmail.com",
locker_number=13,
locker_combination=None,
good_student=True,
)
new_student.full_clean()
self.fail()
except IntegrityError as e:
# print(e)
self.assert_('null value in column "locker_combination" ' in str(e))
def test_005_student_with_improper_name_field(self):
try:
new_student = Student.objects.create(
name=None,
student_email="thisIsAnEmail@school.com",
personal_email="thisIsAnEmail@gmail.com",
locker_number=13,
locker_combination="12-12-12",
good_student=True,
)
new_student.full_clean()
self.fail()
except Exception as e:
# print(e)
self.assert_('null value in column "name" ' in str(e))
def test_006_student_with_proper_fields(self):
new_student = Student.objects.create(
name="John W. Wally",
student_email="john@school.com",
personal_email="john@gmail.com",
locker_number=13,
locker_combination="12-12-12",
good_student=True,
)
new_student.full_clean()
self.assertIsNotNone(new_student)
# PART II
def test_007_student_with_repeated_student_email(self):
try:
Student.objects.create(
name="Johnny H. Harris",
student_email="thisIsMyEmail@school.com",
personal_email="myOtherEmail@gmail.com",
locker_number=119,
locker_combination="11-11-11",
good_student=False,
)
new_student = Student.objects.create(
name="Johnny H. Harris",
student_email="thisIsMyEmail@school.com",
personal_email="myEmail@gmail.com",
locker_number=109,
locker_combination="11-11-11",
good_student=False,
)
new_student.full_clean()
self.fail()
except IntegrityError as e:
# print("\n\n\n", e, "\n\n\n")
self.assert_(
'duplicate key value violates unique constraint "student_app_student_student_email'
in str(e)
)
def test_008_student_with_repeated_personal_email(self):
try:
Student.objects.create(
name="Johnny H. Harris",
student_email="thisIsMyOtherEmail@school.com",
personal_email="myEmail@gmail.com",
locker_number=119,
locker_combination="11-11-11",
good_student=False,
)
new_student = Student.objects.create(
name="Johnny H. Harris",
student_email="thisIsMyEmail@school.com",
personal_email="myEmail@gmail.com",
locker_number=109,
locker_combination="11-11-11",
good_student=False,
)
new_student.full_clean()
self.fail()
except IntegrityError as e:
# print("\n\n\n", e, "\n\n\n")
self.assert_(
'duplicate key value violates unique constraint "student_app_student_personal_email'
in str(e)
)
def test_009_student_with_repeated_locker_number(self):
try:
Student.objects.create(
name="Johnny H. Harris",
student_email="IsmyOEmail@school.com",
personal_email="otIsMyEmail@gmail.com",
locker_number=108,
locker_combination="11-11-11",
good_student=False,
)
new_student = Student.objects.create(
name="Johnny H. Harris",
student_email="IsyEmail@school.com",
personal_email="tIsMyEmail@gmail.com",
locker_number=108,
locker_combination="11-11-11",
good_student=False,
)
new_student.full_clean()
self.fail()
except IntegrityError as e:
# print(e)
self.assert_("student_app_student_locker_number" in str(e))
def test_010_student_utilizing_default_values(self):
new_student = Student.objects.create(
name="Maverick H. Macconnel",
student_email="mav@school.com",
personal_email="mav@gmail.com",
)
new_student.full_clean()
self.assertIsNotNone(new_student)
## PART III
def test_011_student_with_improper_name_format(self):
try:
new_student = Student.objects.create(
name="Maverick Macconnel",
student_email="mav@school.com",
personal_email="mav@gmail.com",
)
new_student.full_clean()
self.fail()
except ValidationError as e:
# print(e.message_dict)
self.assert_(
'Name must be in the format "First Middle Initial. Last"'
in e.message_dict["name"]
)
def test_012_student_with_improper_student_email(self):
try:
new_student = Student.objects.create(
name="Maverick H. Macconnel",
student_email="mav@school.org",
personal_email="mav@gmail.com",
)
new_student.full_clean()
self.fail()
except ValidationError as e:
# print(e.message_dict)
self.assert_(
'Invalid school email format. Please use an email ending with "@school.com".'
in e.message_dict["student_email"]
)
def test_013_student_with_improper_locker_combination(self):
try:
new_student = Student.objects.create(
name="Maverick H. Macconnel",
student_email="mav@school.com",
personal_email="mav@gmail.com",
locker_combination="zz-234-p1",
)
new_student.full_clean()
self.fail()
except ValidationError as e:
# print(e.message_dict)
self.assert_(
'Combination must be in the format "12-12-12"'
in e.message_dict["locker_combination"]
)
def test_014_student_with_low_locker_number(self):
try:
new_student = Student.objects.create(
name="Maverick H. Macconnel",
student_email="mav@school.com",
personal_email="mav@gmail.com",
locker_number=0,
)
new_student.full_clean()
self.fail()
except ValidationError as e:
# print(e.message_dict)
self.assert_(
"Ensure this value is greater than or equal to 1."
in e.message_dict["locker_number"]
)
def test_015_student_with_high_locker_number(self):
try:
new_student = Student.objects.create(
name="Maverick H. Macconnel",
student_email="mav@school.com",
personal_email="mav@gmail.com",
locker_number=350,
)
new_student.full_clean()
self.fail()
except ValidationError as e:
# print(e.message_dict)
self.assert_(
"Ensure this value is less than or equal to 200."
in e.message_dict["locker_number"]
)
## PART IV
def test_016_student_serializer_with_proper_data(self):
data = {
"name": "John W. Watson",
"student_email": "thisIsAnEmail@school.com",
"locker_number": 13,
}
serializer = StudentSerializer(data=data)
self.assertTrue(serializer.is_valid())
def test_017_student_serializer_with_proper_data(self):
student = Student(
**{
"name": "John W. Watson",
"student_email": "thisIsAnEmail@school.com",
"locker_number": 13,
}
)
serializer = StudentSerializer(student)
self.assertEquals(
serializer.data,
{
"name": "John W. Watson",
"student_email": "thisIsAnEmail@school.com",
"locker_number": 13,
},
)
def test_018_student_serializer_all_with_proper_data(self):
try:
Subject.objects.create(subject_name="Python", professor="Professor Adam")
data = {
"name": "John W. Watson",
"student_email": "thisIsAnEmail@school.com",
"personal_email": "thisIsAnEmail@gmail.com",
"locker_number": 13,
"locker_combination": "12-33-44",
"good_student": True,
"subjects": [1],
}
serializer = StudentAllSerializer(data=data)
self.assertTrue(serializer.is_valid())
except Exception as e:
print(serializer.errors)
def test_019_student_serializer_all_with_proper_reponse(self):
# Subject.objects.create(subject_name = "Python", professor = "Professor Adam")
stud = Student(
**{
"name": "John W. Watson",
"student_email": "thisIsAnEmail@school.com",
"personal_email": "thisIsAnEmail@gmail.com",
"locker_number": 13,
"locker_combination": "12-33-44",
"good_student": True,
}
)
serializer = StudentAllSerializer(stud)
self.assertEquals(
serializer.data,
{
"name": "John W. Watson",
"student_email": "thisIsAnEmail@school.com",
"personal_email": "thisIsAnEmail@gmail.com",
"locker_number": 13,
"locker_combination": "12-33-44",
"good_student": True,
"subjects": [],
},
)
# PART V
def test_020_student_with_not_enough_classes(self):
try:
new_student = Student.objects.create(
name="Maverick H. Macconnel",
student_email="mav@school.com",
personal_email="mav@gmail.com",
)
new_student.save()
a_class = Subject.objects.create(
subject_name=f"Python30000", professor="Professor Ana"
)
a_class.save()
new_student.remove_subject(a_class.id)
self.fail()
except Exception as e:
# print(e)
self.assertEquals("This students class schedule is empty!", str(e))
def test_021_student_with_too_many_classes(self):
try:
new_student = Student.objects.create(
name="Maverick H. Macconnel",
student_email="mav@school.com",
personal_email="mav@gmail.com",
)
new_student.save()
for idx in range(9):
a_class = Subject.objects.create(
subject_name=f"Python{idx}", professor="Professor Ana"
)
a_class.save()
new_student.add_subject(a_class.id)
self.fail()
except Exception as e:
# print(e)
self.assertEquals("This students class schedule is full!", str(e))
def test_022_subject_with_improper_subject_format(self):
try:
new_student = Student.objects.create(
name="Maverick H. Macconnel",
student_email="mav@school.com",
personal_email="mav@gmail.com",
)
new_student.save()
a_subject = Subject.objects.create(
subject_name="a subject", professor="Professor Ben"
)
a_subject.save()
a_subject.add_a_student(Student.objects.all().first().id)
a_subject.full_clean()
self.fail()
except ValidationError as e:
# print(e.message_dict)
self.assert_(
"Subject must be in title case format."
in e.message_dict["subject_name"]
)
def test_023_subject_with_improper_professor_format(self):
try:
new_student = Student.objects.create(
name="Maverick H. Macconnel",
student_email="mav@school.com",
personal_email="mav@gmail.com",
)
new_student.save()
a_subject = Subject.objects.create(subject_name="Math", professor="Mr. Ben")
a_subject.save()
a_subject.add_a_student(Student.objects.all().first().id)
a_subject.full_clean()
self.fail()
except ValidationError as e:
# print(e.message_dict)
self.assert_(
'Professor name must be in the format "Professor Adam".'
in e.message_dict["professor"]
)
def test_024_subject_with_not_enough_students(self):
try:
a_subject = Subject.objects.create(
subject_name="Math", professor="Professor Ben"
)
a_subject.save()
a_subject.drop_a_student(1)
a_subject.full_clean()
self.fail()
except Exception as e:
# print(e)
self.assertEquals("This subject is empty!", str(e))
def test_025_Grade_with_proper_input(self):
Subject.objects.create(subject_name="Math", professor="Professor Ben")
Student.objects.create(
name="Maverick H. Macconnel",
student_email="mav@school.com",
personal_email="mav@gmail.com",
)
grade = Grade.objects.create(
grade=98.75,
a_subject=Subject.objects.all().first(),
student=Student.objects.all().first(),
)
grade.full_clean()
self.assertIsNotNone(grade)
def test_026_Grade_with_high_grade(self):
try:
Subject.objects.create(subject_name="Math", professor="Professor Ben")
Student.objects.create(
name="Maverick H. Macconnel",
student_email="mav@school.com",
personal_email="mav@gmail.com",
)
grade = Grade.objects.create(
grade=198.75,
a_subject=Subject.objects.all().first(),
student=Student.objects.all().first(),
)
grade.full_clean()
self.fail()
except ValidationError as e:
# print(e)
self.assert_(
"Ensure this value is less than or equal to 100.0."
in e.message_dict["grade"]
)
def test_027_Grade_with_incorrect_grade_format(self):
try:
grade = Grade.objects.create(
grade=1598.756,
a_subject=Subject.objects.all().first(),
student=Student.objects.all().first(),
)
grade.full_clean()
self.fail()
except DataError as e:
# print(e)
self.assert_("numeric field overflow" in str(e))