Completed
Push — master ( 897349...4f09bf )
by Alexander
109:51 queued 107:08
created

Test_Tag_Counter.setUpTestData()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nop 1
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
from django.utils.translation import ugettext_lazy as _
11
12
from tcms.core.ajax import _TagCounter, _TagObjects
13
from tcms.testplans.models import TestPlanTag
14
from tcms.testruns.models import TestRunTag
15
from tcms.testcases.models import TestCase, TestCaseTag
16
from tcms.tests import BasePlanCase
17
18
from tcms.tests.factories import TagFactory
19
from tcms.tests.factories import TestRunFactory
20
from tcms.tests.factories import TestCaseFactory
21
from tcms.tests.factories import TestPlanFactory
22
23
from tcms.utils.permissions import initiate_user_with_default_setups
24
25
26
class Test_TestCaseUpdates(BasePlanCase):
27
    """
28
        Tests for TC bulk update actions triggered via
29
        TP sub-menu.
30
    """
31
    def _assert_default_tester_is(self, expected_value):
32
        for test_case in TestCase.objects.filter(plan=self.plan):
33
            self.assertEqual(test_case.default_tester, expected_value)
34
35
    @classmethod
36
    def setUpTestData(cls):
37
        super().setUpTestData()
38
        initiate_user_with_default_setups(cls.tester)
39
        cls.url = reverse('ajax.update.cases-actor')
40
41
    def setUp(self):
42
        super().setUp()
43
        self._assert_default_tester_is(None)
44
45 View Code Duplication
    def test_update_default_tester_via_username(self):
0 ignored issues
show
Duplication introduced by Mr. Senko
This code seems to be duplicated in your project.
Loading history...
46
        response = self.client.post(self.url, {
47
            'case[]': [case.pk for case in TestCase.objects.filter(plan=self.plan)],
48
            'what_to_update': 'default_tester',
49
            'username': self.tester.username
50
        })
51
52
        self.assertEqual(HTTPStatus.OK, response.status_code)
53
        result = json.loads(str(response.content, encoding=settings.DEFAULT_CHARSET))
54
        self.assertEqual(result['rc'], 0)
55
        self.assertEqual(result['response'], 'ok')
56
57
        self._assert_default_tester_is(self.tester)
58
59 View Code Duplication
    def test_update_default_tester_via_email(self):
0 ignored issues
show
Duplication introduced by Mr. Senko
This code seems to be duplicated in your project.
Loading history...
60
        # test for https://github.com/kiwitcms/Kiwi/issues/85
61
        response = self.client.post(self.url, {
62
            'case[]': [case.pk for case in TestCase.objects.filter(plan=self.plan)],
63
            'what_to_update': 'default_tester',
64
            'username': self.tester.email
65
        })
66
67
        self.assertEqual(HTTPStatus.OK, response.status_code)
68
        result = json.loads(str(response.content, encoding=settings.DEFAULT_CHARSET))
69
        self.assertEqual(result['rc'], 0)
70
        self.assertEqual(result['response'], 'ok')
71
72
        self._assert_default_tester_is(self.tester)
73
74
    def test_update_default_tester_non_existing_user(self):
75
        username = 'user which doesnt exist'
76
        response = self.client.post(self.url, {
77
            'case[]': [case.pk for case in TestCase.objects.filter(plan=self.plan)],
78
            'what_to_update': 'default_tester',
79
            'username': username
80
        })
81
82
        self.assertEqual(HTTPStatus.NOT_FOUND, response.status_code)
83
        result = json.loads(str(response.content, encoding=settings.DEFAULT_CHARSET))
84
        self.assertEqual(result['rc'], 1)
85
        self.assertEqual(result['response'], _('User %s not found!') % username)
86
87
        self._assert_default_tester_is(None)
88
89
90
class Test_Tag_Render(BasePlanCase):
91
92
    @classmethod
93
    def setUpTestData(cls):
94
        super().setUpTestData()
95
        cls.url = reverse('ajax-tags')
96
        cls.test_tag = TagFactory()
97
        cls.test_plan = TestPlanFactory()
98
        cls.test_case = TestCaseFactory()
99
        cls.test_run = TestRunFactory()
100
101
        cls.test_plan.add_tag(cls.test_tag)
102
        cls.test_case.add_tag(cls.test_tag)
103
        cls.test_run.add_tag(cls.test_tag)
104
105
        for i in range(0, 3):
106
            TestPlanFactory().add_tag(cls.test_tag)
107
108
        for i in range(0, 4):
109
            TestCaseFactory().add_tag(cls.test_tag)
110
111
        for i in range(0, 5):
112
            TestRunFactory().add_tag(cls.test_tag)
113
114
    def test_render_plan(self):
115
        response = self.client.get(self.url, {
116
            'plan': self.test_plan.plan_id
117
        })
118
119
        self._assert_tags(response)
120
121
    def test_render_case(self):
122
        response = self.client.get(self.url, {
123
            'case': self.test_case.case_id
124
        })
125
126
        self._assert_tags(response)
127
128
    def _assert_tags(self, response):
129
        self.assertEqual(response.status_code, HTTPStatus.OK)
130
131
        # asserting the number of tags for the given plan/case/run
132
        self.assertEqual(self.test_plan.tag.count(), 1)
133
        self.assertEqual(self.test_case.tag.count(), 1)
134
        self.assertEqual(self.test_run.tag.count(), 1)
135
136
        # asserting the number or plans/cases/runs the tag has been assigned to
137
        self.assertContains(response, '>4</a>')
138
        self.assertContains(response, '>5</a>')
139
        self.assertContains(response, '>6</a>')
140
141
142
class Test_Tag_Objects(test.TestCase):
143
144
    @classmethod
145
    def setUpTestData(cls):
146
        cls.request = HttpRequest()
147
148
        cls.test_plan = TestPlanFactory()
149
        cls.test_case = TestCaseFactory()
150
151
    def test_get_plan(self):
152
        self.request.GET = {'plan': self.test_plan.pk}
153
        tag_objects = _TagObjects(self.request)
154
155
        self.assertEqual(tag_objects.get()[0], 'management/get_tag.html')
156
        self.assertEqual(tag_objects.get()[1], self.test_plan)
157
158
    def test_get_case(self):
159
        self.request.GET = {'case': self.test_case.pk}
160
        tag_objects = _TagObjects(self.request)
161
162
        self.assertEqual(tag_objects.get()[0], 'management/get_tag.html')
163
        self.assertEqual(tag_objects.get()[1], self.test_case)
164
165
166
class Test_Tag_Counter(test.TestCase):
167
168
    @classmethod
169
    def setUpTestData(cls):
170
        cls.tag_one = TagFactory()
171
        cls.tag_two = TagFactory()
172
        cls.tag_three = TagFactory()
173
        cls.tags = [cls.tag_one, cls.tag_two, cls.tag_three]
174
175
    def test_with_empty_query(self):
176
        """Given an empty TestCaseTag QuerySet we expect the result of all the counting to be 0"""
177
178
        test_case_tags = TestCaseTag.objects.filter(tag=-1).values('tag').annotate(
179
            num_cases=Count('tag')).order_by('tag')
180
181
        case_tag_counter = _TagCounter('num_cases', test_case_tags)
182
        count_for_tag_one = case_tag_counter.calculate_tag_count(self.tag_one)
183
        count_for_tag_two = case_tag_counter.calculate_tag_count(self.tag_two)
184
185
        self.assertEqual(count_for_tag_one, 0)
186
        self.assertEqual(count_for_tag_two, 0)
187
188
    def test_with_tag_not_in_query(self):
189
        """Given a QuerySet that does not contain a given tag,the count for this tag should be 0"""
190
191
        test_case = TestCaseFactory()
192
        test_case.add_tag(self.tag_one)
193
194
        test_case_tags = TestCaseTag.objects.filter(
195
            tag=self.tag_one).values('tag').annotate(
196
            num_cases=Count('tag')).order_by('tag')
197
198
        case_tag_counter = _TagCounter('num_cases', test_case_tags)
199
        count_for_tag_one = case_tag_counter.calculate_tag_count(self.tag_one)
200
        count_for_tag_two = case_tag_counter.calculate_tag_count(self.tag_two)
201
202
        self.assertEqual(count_for_tag_one, 1)
203
        self.assertEqual(count_for_tag_two, 0)
204
205
    def test_in_loop(self):
206
207
        test_plan = TestPlanFactory()
208
        test_run = TestRunFactory()
209
        test_case_one = TestCaseFactory()
210
        test_case_two = TestCaseFactory()
211
212
        test_plan.add_tag(self.tag_one)
213
        test_plan.add_tag(self.tag_two)
214
        test_plan.add_tag(self.tag_three)
215
216
        test_case_one.add_tag(self.tag_one)
217
        test_case_one.add_tag(self.tag_three)
218
219
        test_run.add_tag(self.tag_two)
220
221
        test_case_two.add_tag(self.tag_three)
222
223
        test_plan_tags = TestPlanTag.objects.filter(tag__in=self.tags).values('tag').annotate(
224
            num_plans=Count('tag')).order_by('tag')
225
        test_case_tags = TestCaseTag.objects.filter(tag__in=self.tags).values('tag').annotate(
226
            num_cases=Count('tag')).order_by('tag')
227
        test_run_tags = TestRunTag.objects.filter(tag__in=self.tags).values('tag').annotate(
228
            num_runs=Count('tag')).order_by('tag')
229
230
        plan_counter = _TagCounter('num_plans', test_plan_tags)
231
        case_counter = _TagCounter('num_cases', test_case_tags)
232
        run_counter = _TagCounter('num_runs', test_run_tags)
233
234
        for tag in self.tags:
235
            tag.num_plans = plan_counter.calculate_tag_count(tag)
236
            tag.num_cases = case_counter.calculate_tag_count(tag)
237
            tag.num_runs = run_counter.calculate_tag_count(tag)
238
239
        self.assertEqual(self.tag_one.num_plans, 1)
240
        self.assertEqual(self.tag_two.num_plans, 1)
241
        self.assertEqual(self.tag_three.num_plans, 1)
242
243
        self.assertEqual(self.tag_one.num_cases, 1)
244
        self.assertEqual(self.tag_two.num_cases, 0)
245
        self.assertEqual(self.tag_three.num_cases, 2)
246
247
        self.assertEqual(self.tag_one.num_runs, 0)
248
        self.assertEqual(self.tag_two.num_runs, 1)
249
        self.assertEqual(self.tag_three.num_runs, 0)
250