forked from Code-Platoon-Assignments/django-school-api-III
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
284 lines (263 loc) · 9.94 KB
/
tests.py
File metadata and controls
284 lines (263 loc) · 9.94 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
from django.test import TestCase
from .models import Student
from django.core.exceptions import ValidationError
from django.db import IntegrityError
# 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"]
)