-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOLD_test.py
363 lines (320 loc) · 21.2 KB
/
OLD_test.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
'''
These are the old/default tests that were database dependent that required DB creation rights to be able to
create a test database to run these tests against. Marking this file as OLD to disable running these as part of the CI
pipeline as our current free hosting setup does not allow for DB creation.
See other tests inside this module for non DB specific unit tests.
'''
from django.test import TestCase, Client
import django
django.setup()
from ponder.models import Categorization, Categorizer, Commit, Dataset, ProblemCategory, ProblemCause, ProblemSymptom, ProblemFix
from django.contrib.auth.models import User
from ponder.forms import CategorizationForm, CategorizerForm
from django.db import IntegrityError
from django.contrib.auth.models import Group
class AddCategorizationFormTests(TestCase):
'''
@classmethod
def setUpTestData(self):
newCategory1 = ProblemCategory.objects.create(category='category1', description='test')
newCategory2 = ProblemCategory.objects.create(category='category2', description='test')
newCategory3 = ProblemCategory.objects.create(category='category3', description='test')
newCategory4 = ProblemCategory.objects.create(category='category4', description='test')
newCategory5 = ProblemCategory.objects.create(category='category5', description='test')
newCause = ProblemCause.objects.create(cause='test', description='test')
newSymptom = ProblemSymptom.objects.create(symptom='test', description='test')
newFix = ProblemFix.objects.create(fix='test', description='test')
# These 2 tests should not be part of the insertion form validation. The validity of the sha and the user can be tested when the
# commits table and the categorizations table are rendered, respectively.
def test_sha_not_null(self):
form = CategorizationForm(category_text='', category_description='', cause_text='', cause_description='',fix_text='',fix_description='', \
symptom_text='', symptom_description='',sha=None, data={}, user='testUser')
self.assertFalse(form.is_valid()) # The form should not be valid because sha is null
def test_categorizer_not_null(self):
form = CategorizationForm(category_text='', category_description='', cause_text='', cause_description='',fix_text='',fix_description='', \
symptom_text='',symptom_description='',sha='0000000', data={}, user=None)
self.assertFalse(form.is_valid()) # The form should not be valid because categorizer is null
'''
#Test that all the fields in test data exist in the form
def test_all_test_data_fields_exist_in_form(self):
data = {
"is_func_fix": True,
"problem_category": "1",
'category_comment': "test",
"problem_cause": "1",
'cause_comment': "test",
"problem_symptom": "1",
'symptom_comment': "test",
"problem_fix": "1",
"fix_comment": "test",
"should_discuss": True
}
form = CategorizationForm(category_text='', category_description='', cause_text='', cause_description='',fix_text='',fix_description='', \
symptom_text='', symptom_description='',sha='0000000', data=data, user='testUser')
for field in data:
self.assertIn(field, form.data)
# Test that all the test data was correctly populated in the form.
def test_all_entered_fields_are_stored_correctly_in_form(self):
data = {
"is_func_fix": True,
"problem_category": "1",
'category_comment': "test",
"problem_cause": "1",
'cause_comment': "test",
"problem_symptom": "1",
'symptom_comment': "test",
"problem_fix": "1",
"fix_comment": "test",
"should_discuss": True
}
form = CategorizationForm(category_text='', category_description='', cause_text='', cause_description='',fix_text='',fix_description='', \
symptom_text='', symptom_description='',sha='0000000', data=data, user='testUser')
for field in data:
self.assertEqual(data[field], form.data[field])
# Check constraint is at https://gist.github.com/khatchad/09f0c8d1ca6e0f23b0b9bbf5c62ac8f9#file-commit_categorizations-sql-L32.
#If the func fix is null, then we don't need any of the other fields populated.
def test_null_func_fix(self):
form = CategorizationForm(category_text='', category_description='', cause_text='', cause_description='',fix_text='',fix_description='', \
symptom_text='', symptom_description='',sha='0000000', data={"is_func_fix": None}, user='testUser')
self.assertTrue(form.is_valid()) # The form should be valid.
self.assertNotIn("is_func_fix", form.errors) # There should be no errors.
#If the id of the problem category is 1, then we don't need any of the other fields populated.
def test_problem_category_id_1(self):
form = CategorizationForm(category_text='', category_description='', cause_text='', cause_description='',fix_text='',fix_description='', \
symptom_text='', symptom_description='',sha='0000000', data={"is_func_fix": True, "problem_category": "1"}, user='testUser')
self.assertTrue(form.is_valid()) # The form should be valid.
self.assertNotIn("problem_category", form.errors) # There should be no errors.
#If the id of the problem category is 2, then we don't need any of the other fields populated.
def test_problem_category_id_2(self):
form = CategorizationForm(category_text='', category_description='', cause_text='', cause_description='',fix_text='',fix_description='', \
symptom_text='', symptom_description='',sha='0000000', data={"is_func_fix": True, "problem_category": "2"}, user='testUser')
self.assertTrue(form.is_valid()) # The form should be valid.
self.assertNotIn("problem_category", form.errors) # There should be no errors.
#If the id of the problem category is 5, then we don't need any of the other fields populated.
def test_problem_category_id_5(self):
form = CategorizationForm(category_text='', category_description='', cause_text='', cause_description='',fix_text='',fix_description='', \
symptom_text='', symptom_description='',sha='0000000', data={"is_func_fix": True, "problem_category": "5"}, user='testUser')
self.assertTrue(form.is_valid()) # The form should be valid.
self.assertNotIn("problem_category", form.errors) # There should be no errors.
'''
If the func fix is not null and the problem category is not 1, 2, or 5, then
problem cateogry, problem cause, problem symptom, and problem fix, and should
dicuss must not be null. Case when all required fields are not null.
'''
def test_required_fields_not_null(self):
data = {
"is_func_fix": True, #not null
"problem_category": "3", #not 1, 2, or 5
"problem_cause": "1",
"problem_symptom": "1",
"problem_fix": "1",
"should_discuss": True
}
form = CategorizationForm(category_text='', category_description='', cause_text='', cause_description='',fix_text='',fix_description='', \
symptom_text='', symptom_description='',sha='0000000', data=data, user='testUser')
self.assertTrue(form.is_valid()) # The form should be valid
self.assertEqual(form.errors, {}) # There should be no errors
#Case when is func fix is not null and problem category is missing
def test_problem_cause_not_null(self):
data = {
"is_func_fix": True, #not null
"problem_cause": "1",
"problem_symptom": "1",
"problem_fix": "1",
"should_discuss": True
}
form = CategorizationForm(category_text='', category_description='', cause_text='', cause_description='',fix_text='',fix_description='', \
symptom_text='', symptom_description='',sha='0000000', data=data, user='testUser')
self.assertFalse(form.is_valid()) # The form should not be valid because is func fix is not null and problem category is null
self.assertEqual(form.errors["problem_category"], ['This field is required. Select an existing problem category or enter a new one.'])
#Case when is func fix is not null, problem caretegory id is not 1, 2 or 5, and problem cause is missing
def test_problem_cause_not_null(self):
data = {
"is_func_fix": True, #not null
"problem_category": "3", #not 1, 2, or 5
"problem_symptom": "1",
"problem_fix": "1",
"should_discuss": True
}
form = CategorizationForm(category_text='', category_description='', cause_text='', cause_description='',fix_text='',fix_description='', \
symptom_text='', symptom_description='',sha='0000000', data=data, user='testUser')
self.assertFalse(form.is_valid()) # The form should not be valid because problem cause is missing
self.assertEqual(form.errors["problem_cause"], ['This field is required. Select an existing problem cause or enter a new one.'])
#Case when is func fix is not null, problem caretegory id is not 1, 2 or 5, and problem symptom is missing
def test_problem_symptom_not_null(self):
data = {
"is_func_fix": True, #not null
"problem_category": "3", #not 1, 2, or 5
"problem_cause": "1",
"problem_fix": "1",
"should_discuss": True
}
form = CategorizationForm(category_text='', category_description='', cause_text='', cause_description='',fix_text='',fix_description='', \
symptom_text='', symptom_description='',sha='0000000', data=data, user='testUser')
self.assertFalse(form.is_valid()) # The form should not be valid because problem symptom is missing
self.assertEqual(form.errors["problem_symptom"], ['This field is required. Select an existing problem symptom or enter a new one.'])
#Case when is func fix is not null, problem caretegory id is not 1, 2 or 5, and problem fix is missing
def test_problem_fix_not_null(self):
data = {
"is_func_fix": True, #not null
"problem_category": "3", #not 1, 2, or 5
"problem_cause": "1",
"problem_symptom": "1",
"should_discuss": True
}
form = CategorizationForm(category_text='', category_description='', cause_text='', cause_description='',fix_text='',fix_description='', \
symptom_text='', symptom_description='',sha='0000000', data=data, user='testUser')
self.assertFalse(form.is_valid()) # The form should not be valid because problem fix is missing
self.assertEqual(form.errors["problem_fix"], ['This field is required. Select an existing problem fix or enter a new one.'])
'''
#Case when is func fix is not null, problem caretegory id is not 1, 2 or 5, and should discuss is missing
def test_should_discuss_not_null(self):
data = {
"is_func_fix": True, #not null
"problem_category": "3", #not 1, 2, or 5
"problem_cause": "1",
"problem_symptom": "1",
"problem_fix": "1"
}
form = CategorizationForm(category_text='', category_description='', cause_text='', cause_description='',fix_text='',fix_description='', \
symptom_text='', symptom_description='',sha='0000000', data=data, user='testUser')
self.assertFalse(form.is_valid()) # The form should not be valid because should dicuss is missing
self.assertEqual(form.errors["should_dicuss"], ["Should_discuss can not be null."])
'''
# issue: https://github.com/ponder-lab/Imperative-DL-Study-Web-App/issues/103
# Case when an existing category is selected and a new category is entered
def test_problem_category_not_selected_and_entered(self):
form = CategorizationForm(category_text='test', category_description='', cause_text='', cause_description='',fix_text='',fix_description='', \
symptom_text='', symptom_description='',sha='0000000', data={"is_func_fix": True, "problem_category": "1"}, user='testUser')
self.assertFalse(form.is_valid()) # The form should not be valid.
self.assertEqual(form.errors["problem_category"], ["Choose only one option. Either select an existing problem category or enter a new one."])
# Case when an existing problem cause is selected and a new problem cause is entered
def test_problem_cause_not_selected_and_entered(self):
form = CategorizationForm(category_text='', category_description='', cause_text='test', cause_description='',fix_text='',fix_description='', \
symptom_text='', symptom_description='',sha='0000000', data={"is_func_fix": True, "problem_cause": "1"}, user='testUser')
self.assertFalse(form.is_valid()) # The form should not be valid.
self.assertEqual(form.errors["problem_cause"], ["Choose only one option. Either select an existing problem cause or enter a new one."])
# Case when an existing problem symptom is selected and a new problem symptom is entered
def test_problem_symptom_not_selected_and_entered(self):
form = CategorizationForm(category_text='', category_description='', cause_text='', cause_description='',fix_text='',fix_description='', \
symptom_text='test', symptom_description='',sha='0000000', data={"is_func_fix": True, "problem_symptom": "1"}, user='testUser')
self.assertFalse(form.is_valid()) # The form should not be valid.
self.assertEqual(form.errors["problem_symptom"], ["Choose only one option. Either select an existing problem symptom or enter a new one."])
# Case when an existing problem fix is selected and a new problem fix is entered
def test_problem_fix_not_selected_and_entered(self):
form = CategorizationForm(category_text='', category_description='', cause_text='', cause_description='',fix_text='test',fix_description='', \
symptom_text='', symptom_description='',sha='0000000', data={"is_func_fix": True, "problem_fix": "1"}, user='testUser')
self.assertFalse(form.is_valid()) # The form should not be valid.
self.assertEqual(form.errors["problem_fix"], ["Choose only one option. Either select an existing problem fix or enter a new one."])
# Case when there is a problem category but func fix is false
def test_category_not_null_and_func_fix_null(self):
form = CategorizationForm(category_text='', category_description='', cause_text='', cause_description='',fix_text='test',fix_description='', \
symptom_text='', symptom_description='',sha='0000000', data={"is_func_fix": False, "problem_category": "1"}, user='testUser')
self.assertFalse(form.is_valid()) # The form should not be valid.
self.assertEqual(form.errors["is_func_fix"], ["This field should be checked. An existing problem category indicates a bug fix."])
# Case when there is a problem category text but func fix is false
def test_category_text_not_null_and_func_fix_null(self):
form = CategorizationForm(category_text='test', category_description='', cause_text='', cause_description='',fix_text='test',fix_description='', \
symptom_text='', symptom_description='',sha='0000000', data={"is_func_fix": False}, user='testUser')
self.assertFalse(form.is_valid()) # The form should not be valid.
self.assertEqual(form.errors["is_func_fix"], ["This field should be checked. An existing problem category indicates a bug fix."])
class CategorizerTests(TestCase):
@classmethod
def setUpTestData(self):
# Make sure the categorization table is empty
Categorization.objects.all().delete()
# Create two new Django users.
self.user1 = User.objects.create_user(username='testUser1', password='testpassword')
self.user2 = User.objects.create_user(username='testUser2', password='testpassword')
# Case when a new categorizer is being added when the given name already exists in the table.
def test_same_name(self):
# Make sure there are no other categorizers.
Categorizer.objects.all().delete()
# Create a categorizer John Smith.
Categorizer.objects.create(name='John Smith', initials='JS', user=self.user1)
# Second categorizer is inserted with the same name and different initials and username.
Categorizer.objects.create(name='John Smith', initials='AB', user=self.user2)
# Both testUser1 and testUser2 with the name "John Smith" should be in the table because two categorzers can have the same name
self.assertTrue(Categorizer.objects.filter(user='testUser1').exists())
self.assertTrue(Categorizer.objects.filter(user='testUser2').exists())
# Case when a new categorizer is being added when the given initial already exists in the table.
def test_same_initials(self):
# Make sure there are no other categorizers.
Categorizer.objects.all().delete()
# Create a categorizer John Smith.
Categorizer.objects.create(name='John Smith', initials='JS', user=self.user1)
# Second categorizer is inserted with the same initials and different name and username.
# Exception here because the two users have the same initials.
self.assertRaises(IntegrityError, Categorizer.objects.create, name='Jane Scott', initials='JS', user=self.user2)
# Case when a new categorizer is being added when the given user already exists in the table
def test_same_user(self):
# Make sure there are no other categorizers.
Categorizer.objects.all().delete()
# Create a categorizer John Smith.
Categorizer.objects.create(name='John Smith', initials='JS', user=self.user1)
# The same user is inserted again with different name and initials.
# Expecting an exception because two categorizers can't be related to the same Django user.
self.assertRaises(IntegrityError, Categorizer.objects.create, name='Michelle Reed', initials='MR', user=self.user1)
class CategorizerFormTests(TestCase):
@classmethod
def setUpTestData(self):
# Make sure the categorization table is empty
Categorization.objects.all().delete()
# Create a new Django user.
self.user = User.objects.create_user(username='testUser3', password='testpassword')
def test_name_exist(self):
# Make sure there are no other categorizers.
Categorizer.objects.all().delete()
# Insert one categorizer into the databse
Categorizer.objects.create(name='John Smith', initials='JS', user=self.user)
# Create a form that has the same name as the existing categorizer but with different initials
form = CategorizerForm({'name':'John Smith', 'initials':'AB'})
self.assertTrue(form.is_valid())
def test_initials_exist(self):
# Make sure there are no other categorizers.
Categorizer.objects.all().delete()
# Insert one categorizer into the databse
Categorizer.objects.create(name='John Smith', initials='JS', user=self.user)
# Create a form that has the same name as the existing initials but with different name
form = CategorizerForm({'name':'Jane Scott', 'initials':'JS'})
self.assertFalse(form.is_valid()) # Assert that the form test is invalid.
self.assertEqual(form.errors["initials"], ["Categorizer with this Initials already exists."])
def test_name_empty(self):
# Make sure there are no other categorizers.
Categorizer.objects.all().delete()
# Create a form that's missing name
form = CategorizerForm({'name': '', 'initials':'JS'})
self.assertFalse(form.is_valid())
def test_initials_empty(self):
# Make sure there are no other categorizers.
Categorizer.objects.all().delete()
# Create a form that's missing initials
form = CategorizerForm({'name': 'John Smith', 'initials':''})
self.assertFalse(form.is_valid())
class ViewTests(TestCase):
@classmethod
def setUpTestData(self):
# Clear categorization, categorizer and user table
Categorization.objects.all().delete()
Categorizer.objects.all().delete()
User.objects.all().delete()
# Create a new Django user
self.user = User.objects.create_user(username='testUser', password='testpassword')
# Make the user a categorizer
self.user.groups.add(Group.objects.get(name='categorizer'))
Categorizer.objects.create(name='test', initials='t', user=self.user)
# Log in as the user
self.c = Client()
self.c.login(username='testUser', password='testpassword')
# Test if accessing the Categorization page is successful
def test_access_categorizations(self):
response = self.c.get('/categorizations/?user='+str(self.user.id))
self.assertEqual(response.status_code, 200)
# Test if accessing the Commit page is successful
def test_access_commits(self):
response = self.c.get('/commits')
self.assertEqual(response.status_code, 200)
# Test if accessing the Bug Fixes page is successful
def test_access_bug_fixes(self):
response = self.c.get('/bug_fixes/')
self.assertEqual(response.status_code, 200)