1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
import json |
3
|
|
|
from http import HTTPStatus |
4
|
|
|
|
5
|
|
|
from django import test |
6
|
|
|
from django.urls import reverse |
7
|
|
|
from django.conf import settings |
8
|
|
|
from django.db.models import Count |
9
|
|
|
from django.http.request import HttpRequest |
10
|
|
|
|
11
|
|
|
from tcms.core.ajax import _TagCounter, _TagObjects |
12
|
|
|
from tcms.testplans.models import TestPlanTag |
13
|
|
|
from tcms.testruns.models import TestRunTag |
14
|
|
|
from tcms.testcases.models import TestCase, TestCaseTag |
15
|
|
|
from tcms.tests import BasePlanCase |
16
|
|
|
|
17
|
|
|
from tcms.tests.factories import TagFactory |
18
|
|
|
from tcms.tests.factories import TestRunFactory |
19
|
|
|
from tcms.tests.factories import TestCaseFactory |
20
|
|
|
from tcms.tests.factories import TestPlanFactory |
21
|
|
|
|
22
|
|
|
from tcms.utils.permissions import initiate_user_with_default_setups |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
class Test_TestCaseUpdates(BasePlanCase): |
26
|
|
|
""" |
27
|
|
|
Tests for TC bulk update actions triggered via |
28
|
|
|
TP sub-menu. |
29
|
|
|
""" |
30
|
|
|
def _assert_default_tester_is(self, expected_value): |
31
|
|
|
for test_case in TestCase.objects.filter(plan=self.plan): |
32
|
|
|
self.assertEqual(test_case.default_tester, expected_value) |
33
|
|
|
|
34
|
|
|
@classmethod |
35
|
|
|
def setUpTestData(cls): |
36
|
|
|
super().setUpTestData() |
37
|
|
|
initiate_user_with_default_setups(cls.tester) |
38
|
|
|
cls.url = reverse('ajax.update.cases-actor') |
39
|
|
|
|
40
|
|
|
def setUp(self): |
41
|
|
|
super().setUp() |
42
|
|
|
self._assert_default_tester_is(None) |
43
|
|
|
|
44
|
|
View Code Duplication |
def test_update_default_tester_via_username(self): |
|
|
|
|
45
|
|
|
response = self.client.post(self.url, { |
46
|
|
|
'case[]': [case.pk for case in TestCase.objects.filter(plan=self.plan)], |
47
|
|
|
'what_to_update': 'default_tester', |
48
|
|
|
'username': self.tester.username |
49
|
|
|
}) |
50
|
|
|
|
51
|
|
|
self.assertEqual(HTTPStatus.OK, response.status_code) |
52
|
|
|
result = json.loads(str(response.content, encoding=settings.DEFAULT_CHARSET)) |
53
|
|
|
self.assertEqual(result['rc'], 0) |
54
|
|
|
self.assertEqual(result['response'], 'ok') |
55
|
|
|
|
56
|
|
|
self._assert_default_tester_is(self.tester) |
57
|
|
|
|
58
|
|
View Code Duplication |
def test_update_default_tester_via_email(self): |
|
|
|
|
59
|
|
|
# test for https://github.com/kiwitcms/Kiwi/issues/85 |
60
|
|
|
response = self.client.post(self.url, { |
61
|
|
|
'case[]': [case.pk for case in TestCase.objects.filter(plan=self.plan)], |
62
|
|
|
'what_to_update': 'default_tester', |
63
|
|
|
'username': self.tester.email |
64
|
|
|
}) |
65
|
|
|
|
66
|
|
|
self.assertEqual(HTTPStatus.OK, response.status_code) |
67
|
|
|
result = json.loads(str(response.content, encoding=settings.DEFAULT_CHARSET)) |
68
|
|
|
self.assertEqual(result['rc'], 0) |
69
|
|
|
self.assertEqual(result['response'], 'ok') |
70
|
|
|
|
71
|
|
|
self._assert_default_tester_is(self.tester) |
72
|
|
|
|
73
|
|
|
def test_update_default_tester_non_existing_user(self): |
74
|
|
|
response = self.client.post(self.url, { |
75
|
|
|
'case[]': [case.pk for case in TestCase.objects.filter(plan=self.plan)], |
76
|
|
|
'what_to_update': 'default_tester', |
77
|
|
|
'usernmae': 'user which doesnt exist' |
78
|
|
|
}) |
79
|
|
|
|
80
|
|
|
self.assertEqual(HTTPStatus.OK, response.status_code) |
81
|
|
|
result = json.loads(str(response.content, encoding=settings.DEFAULT_CHARSET)) |
82
|
|
|
self.assertEqual(result['rc'], 1) |
83
|
|
|
self.assertEqual(result['response'], 'Default tester not found!') |
84
|
|
|
|
85
|
|
|
self._assert_default_tester_is(None) |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
class Test_Tag_Test(test.TestCase): |
89
|
|
|
|
90
|
|
|
@classmethod |
91
|
|
|
def setUpTestData(cls): |
92
|
|
|
cls.url = reverse('ajax-tags') |
93
|
|
|
cls.test_tag = TagFactory() |
94
|
|
|
cls.test_plan = TestPlanFactory() |
95
|
|
|
cls.test_case = TestCaseFactory() |
96
|
|
|
cls.test_run = TestRunFactory() |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
class Test_Tag_Add(Test_Tag_Test): |
100
|
|
|
|
101
|
|
|
def test_add_tag_to_test_plan(self): |
102
|
|
|
response = self.client.get(self.url, { |
103
|
|
|
'tags': self.test_tag, |
104
|
|
|
'plan': self.test_plan.plan_id, |
105
|
|
|
'a': 'add' |
106
|
|
|
}) |
107
|
|
|
|
108
|
|
|
self.assertEqual(response.status_code, HTTPStatus.OK) |
109
|
|
|
self.assertEqual(self.test_plan.tag.count(), 1) |
110
|
|
|
self.assertTrue(self.test_tag in self.test_plan.tag.all()) |
111
|
|
|
|
112
|
|
|
def test_add_tag_to_test_case(self): |
113
|
|
|
response = self.client.get(self.url, { |
114
|
|
|
'tags': self.test_tag, |
115
|
|
|
'case': self.test_case.case_id, |
116
|
|
|
'a': 'add' |
117
|
|
|
}) |
118
|
|
|
|
119
|
|
|
self.assertEqual(response.status_code, HTTPStatus.OK) |
120
|
|
|
self.assertEqual(self.test_case.tag.count(), 1) |
121
|
|
|
self.assertTrue(self.test_tag in self.test_case.tag.all()) |
122
|
|
|
|
123
|
|
|
def test_add_tag_to_test_run(self): |
124
|
|
|
response = self.client.get(self.url, { |
125
|
|
|
'tags': self.test_tag, |
126
|
|
|
'run': self.test_run.run_id, |
127
|
|
|
'a': 'add' |
128
|
|
|
}) |
129
|
|
|
|
130
|
|
|
self.assertEqual(response.status_code, HTTPStatus.OK) |
131
|
|
|
self.assertEqual(self.test_run.tag.count(), 1) |
132
|
|
|
self.assertTrue(self.test_tag in self.test_run.tag.all()) |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
class Test_Tag_Remove(Test_Tag_Test): |
136
|
|
|
|
137
|
|
|
def test_remove_tag_from_test_plan(self): |
138
|
|
|
self.test_plan.add_tag(self.test_tag) |
139
|
|
|
|
140
|
|
|
response = self.client.get(self.url, { |
141
|
|
|
'tags': self.test_tag, |
142
|
|
|
'plan': self.test_plan.plan_id, |
143
|
|
|
'a': 'remove' |
144
|
|
|
}) |
145
|
|
|
|
146
|
|
|
self.assertEqual(response.status_code, HTTPStatus.OK) |
147
|
|
|
self.assertEqual(self.test_plan.tag.count(), 0) |
148
|
|
|
|
149
|
|
|
def test_remove_tag_from_test_case(self): |
150
|
|
|
self.test_case.add_tag(self.test_tag) |
151
|
|
|
|
152
|
|
|
response = self.client.get(self.url, { |
153
|
|
|
'tags': self.test_tag, |
154
|
|
|
'case': self.test_case.case_id, |
155
|
|
|
'a': 'remove' |
156
|
|
|
}) |
157
|
|
|
|
158
|
|
|
self.assertEqual(response.status_code, HTTPStatus.OK) |
159
|
|
|
self.assertEqual(self.test_case.tag.count(), 0) |
160
|
|
|
|
161
|
|
|
def test_remove_tag_from_test_run(self): |
162
|
|
|
self.test_run.add_tag(self.test_tag) |
163
|
|
|
|
164
|
|
|
response = self.client.get(self.url, { |
165
|
|
|
'tags': self.test_tag, |
166
|
|
|
'run': self.test_run.run_id, |
167
|
|
|
'a': 'remove' |
168
|
|
|
}) |
169
|
|
|
|
170
|
|
|
self.assertEqual(response.status_code, HTTPStatus.OK) |
171
|
|
|
self.assertEqual(self.test_run.tag.count(), 0) |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
class Test_Tag_Render(Test_Tag_Test): |
175
|
|
|
|
176
|
|
|
@classmethod |
177
|
|
|
def setUpTestData(cls): |
178
|
|
|
super(Test_Tag_Render, cls).setUpTestData() |
179
|
|
|
|
180
|
|
|
cls.test_plan.add_tag(cls.test_tag) |
181
|
|
|
cls.test_case.add_tag(cls.test_tag) |
182
|
|
|
cls.test_run.add_tag(cls.test_tag) |
183
|
|
|
|
184
|
|
|
for _ in range(0, 3): |
185
|
|
|
TestPlanFactory().add_tag(cls.test_tag) |
186
|
|
|
|
187
|
|
|
for _ in range(0, 4): |
188
|
|
|
TestCaseFactory().add_tag(cls.test_tag) |
189
|
|
|
|
190
|
|
|
for _ in range(0, 5): |
191
|
|
|
TestRunFactory().add_tag(cls.test_tag) |
192
|
|
|
|
193
|
|
|
def test_render_plan(self): |
194
|
|
|
response = self.client.get(self.url, { |
195
|
|
|
'plan': self.test_plan.plan_id |
196
|
|
|
}) |
197
|
|
|
|
198
|
|
|
self._assert_tags(response) |
199
|
|
|
|
200
|
|
|
def test_render_case(self): |
201
|
|
|
response = self.client.get(self.url, { |
202
|
|
|
'case': self.test_case.case_id |
203
|
|
|
}) |
204
|
|
|
|
205
|
|
|
self._assert_tags(response) |
206
|
|
|
|
207
|
|
|
def _assert_tags(self, response): |
208
|
|
|
self.assertEqual(response.status_code, HTTPStatus.OK) |
209
|
|
|
|
210
|
|
|
# asserting the number of tags for the given plan/case/run |
211
|
|
|
self.assertEqual(self.test_plan.tag.count(), 1) |
212
|
|
|
self.assertEqual(self.test_case.tag.count(), 1) |
213
|
|
|
self.assertEqual(self.test_run.tag.count(), 1) |
214
|
|
|
|
215
|
|
|
# asserting the number or plans/cases/runs the tag has been assigned to |
216
|
|
|
self.assertContains(response, '>4</a>') |
217
|
|
|
self.assertContains(response, '>5</a>') |
218
|
|
|
self.assertContains(response, '>6</a>') |
219
|
|
|
|
220
|
|
|
|
221
|
|
|
class Test_Tag_Objects(test.TestCase): |
222
|
|
|
|
223
|
|
|
@classmethod |
224
|
|
|
def setUpTestData(cls): |
225
|
|
|
cls.request = HttpRequest() |
226
|
|
|
|
227
|
|
|
cls.test_plan = TestPlanFactory() |
228
|
|
|
cls.test_case = TestCaseFactory() |
229
|
|
|
|
230
|
|
|
def test_get_plan(self): |
231
|
|
|
self.request.GET = {'plan': self.test_plan.pk} |
232
|
|
|
tag_objects = _TagObjects(self.request) |
233
|
|
|
|
234
|
|
|
self.assertEqual(tag_objects.get()[0], 'management/get_tag.html') |
235
|
|
|
self.assertEqual(tag_objects.get()[1], self.test_plan) |
236
|
|
|
|
237
|
|
|
def test_get_case(self): |
238
|
|
|
self.request.GET = {'case': self.test_case.pk} |
239
|
|
|
tag_objects = _TagObjects(self.request) |
240
|
|
|
|
241
|
|
|
self.assertEqual(tag_objects.get()[0], 'management/get_tag.html') |
242
|
|
|
self.assertEqual(tag_objects.get()[1], self.test_case) |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
class Test_Tag_Counter(test.TestCase): |
246
|
|
|
|
247
|
|
|
@classmethod |
248
|
|
|
def setUpTestData(cls): |
249
|
|
|
cls.tag_one = TagFactory() |
250
|
|
|
cls.tag_two = TagFactory() |
251
|
|
|
cls.tag_three = TagFactory() |
252
|
|
|
cls.tags = [cls.tag_one, cls.tag_two, cls.tag_three] |
253
|
|
|
|
254
|
|
|
def test_with_empty_query(self): |
255
|
|
|
"""Given an empty TestCaseTag QuerySet we expect the result of all the counting to be 0""" |
256
|
|
|
|
257
|
|
|
test_case_tags = TestCaseTag.objects.filter(tag=-1).values('tag').annotate( |
258
|
|
|
num_cases=Count('tag')).order_by('tag') |
259
|
|
|
|
260
|
|
|
case_tag_counter = _TagCounter('num_cases', test_case_tags) |
261
|
|
|
count_for_tag_one = case_tag_counter.calculate_tag_count(self.tag_one) |
262
|
|
|
count_for_tag_two = case_tag_counter.calculate_tag_count(self.tag_two) |
263
|
|
|
|
264
|
|
|
self.assertEqual(count_for_tag_one, 0) |
265
|
|
|
self.assertEqual(count_for_tag_two, 0) |
266
|
|
|
|
267
|
|
|
def test_with_tag_not_in_query(self): |
268
|
|
|
"""Given a QuerySet that does not contain a given tag,the count for this tag should be 0""" |
269
|
|
|
|
270
|
|
|
test_case = TestCaseFactory() |
271
|
|
|
test_case.add_tag(self.tag_one) |
272
|
|
|
|
273
|
|
|
test_case_tags = TestCaseTag.objects.filter( |
274
|
|
|
tag=self.tag_one).values('tag').annotate( |
275
|
|
|
num_cases=Count('tag')).order_by('tag') |
276
|
|
|
|
277
|
|
|
case_tag_counter = _TagCounter('num_cases', test_case_tags) |
278
|
|
|
count_for_tag_one = case_tag_counter.calculate_tag_count(self.tag_one) |
279
|
|
|
count_for_tag_two = case_tag_counter.calculate_tag_count(self.tag_two) |
280
|
|
|
|
281
|
|
|
self.assertEqual(count_for_tag_one, 1) |
282
|
|
|
self.assertEqual(count_for_tag_two, 0) |
283
|
|
|
|
284
|
|
|
def test_in_loop(self): |
285
|
|
|
|
286
|
|
|
test_plan = TestPlanFactory() |
287
|
|
|
test_run = TestRunFactory() |
288
|
|
|
test_case_one = TestCaseFactory() |
289
|
|
|
test_case_two = TestCaseFactory() |
290
|
|
|
|
291
|
|
|
test_plan.add_tag(self.tag_one) |
292
|
|
|
test_plan.add_tag(self.tag_two) |
293
|
|
|
test_plan.add_tag(self.tag_three) |
294
|
|
|
|
295
|
|
|
test_case_one.add_tag(self.tag_one) |
296
|
|
|
test_case_one.add_tag(self.tag_three) |
297
|
|
|
|
298
|
|
|
test_run.add_tag(self.tag_two) |
299
|
|
|
|
300
|
|
|
test_case_two.add_tag(self.tag_three) |
301
|
|
|
|
302
|
|
|
test_plan_tags = TestPlanTag.objects.filter(tag__in=self.tags).values('tag').annotate( |
303
|
|
|
num_plans=Count('tag')).order_by('tag') |
304
|
|
|
test_case_tags = TestCaseTag.objects.filter(tag__in=self.tags).values('tag').annotate( |
305
|
|
|
num_cases=Count('tag')).order_by('tag') |
306
|
|
|
test_run_tags = TestRunTag.objects.filter(tag__in=self.tags).values('tag').annotate( |
307
|
|
|
num_runs=Count('tag')).order_by('tag') |
308
|
|
|
|
309
|
|
|
plan_counter = _TagCounter('num_plans', test_plan_tags) |
310
|
|
|
case_counter = _TagCounter('num_cases', test_case_tags) |
311
|
|
|
run_counter = _TagCounter('num_runs', test_run_tags) |
312
|
|
|
|
313
|
|
|
for tag in self.tags: |
314
|
|
|
tag.num_plans = plan_counter.calculate_tag_count(tag) |
315
|
|
|
tag.num_cases = case_counter.calculate_tag_count(tag) |
316
|
|
|
tag.num_runs = run_counter.calculate_tag_count(tag) |
317
|
|
|
|
318
|
|
|
self.assertEqual(self.tag_one.num_plans, 1) |
319
|
|
|
self.assertEqual(self.tag_two.num_plans, 1) |
320
|
|
|
self.assertEqual(self.tag_three.num_plans, 1) |
321
|
|
|
|
322
|
|
|
self.assertEqual(self.tag_one.num_cases, 1) |
323
|
|
|
self.assertEqual(self.tag_two.num_cases, 0) |
324
|
|
|
self.assertEqual(self.tag_three.num_cases, 2) |
325
|
|
|
|
326
|
|
|
self.assertEqual(self.tag_one.num_runs, 0) |
327
|
|
|
self.assertEqual(self.tag_two.num_runs, 1) |
328
|
|
|
self.assertEqual(self.tag_three.num_runs, 0) |
329
|
|
|
|