Passed
Push — master ( 62d7d9...8230b1 )
by Alexander
03:29
created

tcms.testruns.tests.test_data.TestGetExecutionBugs.test_get_bugs()   A

Complexity

Conditions 3

Size

Total Lines 37
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 37
rs 9.28
c 0
b 0
f 0
cc 3
nop 1
1
# -*- coding: utf-8 -*-
2
# pylint: disable=invalid-name, too-many-ancestors
3
4
from datetime import datetime
5
6
from tcms.core.helpers.comments import add_comment
7
from tcms.testruns.data import TestExecutionDataMixin
8
from tcms.tests import BaseCaseRun
9
from tcms.tests import BasePlanCase
10
from tcms.tests.factories import TestExecutionFactory
11
from tcms.tests.factories import TestExecutionStatus
12
from tcms.tests.factories import TestRunFactory
13
14
15
class TestGetCaseRunsStatsByStatusFromEmptyTestRun(BasePlanCase):
16
17
    @classmethod
18
    def setUpTestData(cls):
19
        super(TestGetCaseRunsStatsByStatusFromEmptyTestRun, cls).setUpTestData()
20
21
        cls.empty_test_run = TestRunFactory(manager=cls.tester, default_tester=cls.tester,
22
                                            plan=cls.plan)
23
24
        cls.statuss = TestExecutionStatus.objects.all().order_by('pk')
25
26
    def test_get_from_empty_case_runs(self):
27
        data = self.empty_test_run.stats_executions_status(self.statuss)
28
29
        subtotal = dict((status.pk, [0, status])
30
                        for status in self.statuss)
31
32
        self.assertEqual(subtotal, data.StatusSubtotal)
33
        self.assertEqual(0, data.CaseRunsTotalCount)
34
        self.assertEqual(.0, data.CompletedPercentage)
35
        self.assertEqual(.0, data.FailurePercentage)
36
37
38
class TestGetCaseRunsStatsByStatus(BasePlanCase):
39
40
    @classmethod
41
    def setUpTestData(cls):
42
        super(TestGetCaseRunsStatsByStatus, cls).setUpTestData()
43
44
        cls.statuss = TestExecutionStatus.objects.all().order_by('pk')
45
46
        cls.status_idle = TestExecutionStatus.objects.get(name='IDLE')
47
        cls.status_failed = TestExecutionStatus.objects.get(name='FAILED')
48
        cls.status_waived = TestExecutionStatus.objects.get(name='WAIVED')
49
50
        cls.test_run = TestRunFactory(product_version=cls.version, plan=cls.plan,
51
                                      manager=cls.tester, default_tester=cls.tester)
52
53
        for case, status in ((cls.case_1, cls.status_idle),
54
                             (cls.case_2, cls.status_failed),
55
                             (cls.case_3, cls.status_failed),
56
                             (cls.case_4, cls.status_waived),
57
                             (cls.case_5, cls.status_waived),
58
                             (cls.case_6, cls.status_waived)):
59
            TestExecutionFactory(assignee=cls.tester, tested_by=cls.tester,
60
                                 run=cls.test_run, case=case, status=status)
61
62
    def test_get_stats(self):
63
        data = self.test_run.stats_executions_status(self.statuss)
64
65
        subtotal = dict((status.pk, [0, status])
66
                        for status in self.statuss)
67
        subtotal[self.status_idle.pk][0] = 1
68
        subtotal[self.status_failed.pk][0] = 2
69
        subtotal[self.status_waived.pk][0] = 3
70
71
        expected_completed_percentage = 5.0 * 100 / 6
72
        expected_failure_percentage = 2.0 * 100 / 6
73
74
        self.assertEqual(subtotal, data.StatusSubtotal)
75
        self.assertEqual(6, data.CaseRunsTotalCount)
76
        self.assertEqual(expected_completed_percentage, data.CompletedPercentage)
77
        self.assertEqual(expected_failure_percentage, data.FailurePercentage)
78
79
80
class TestGetExecutionComments(BaseCaseRun):
81
    """Test TestExecutionDataMixin.get_caseruns_comments
82
83
    There are two test runs created already, cls.test_run and cls.test_run_1.
84
85
    For this case, comments will be added to cls.test_run_1 in order to ensure
86
    comments could be retrieved correctly. And another one is for ensuring
87
    empty result even if no comment is added.
88
    """
89
90
    @classmethod
91
    def setUpTestData(cls):
92
        super().setUpTestData()
93
94
        cls.submit_date = datetime(2017, 7, 7, 7, 7, 7)
95
96
        add_comment([cls.execution_4, cls.execution_5],
97
                    comments='new comment',
98
                    user=cls.tester,
99
                    submit_date=cls.submit_date)
100
        add_comment([cls.execution_4],
101
                    comments='make better',
102
                    user=cls.tester,
103
                    submit_date=cls.submit_date)
104
105
    def test_get_empty_comments_if_no_comment_there(self):
106
        data = TestExecutionDataMixin()
107
        comments = data.get_execution_comments(self.test_run.pk)
108
        self.assertEqual({}, comments)
109
110
    def test_get_comments(self):
111
        data = TestExecutionDataMixin()
112
        comments = data.get_execution_comments(self.test_run_1.pk)
113
114
        # note: keys are integer but the values are all string
115
        expected_comments = {
116
            self.execution_4.pk: [
117
                {
118
                    'case_run_id': str(self.execution_4.pk),
119
                    'user_name': self.tester.username,
120
                    'submit_date': self.submit_date,
121
                    'comment': 'new comment'
122
                },
123
                {
124
                    'case_run_id': str(self.execution_4.pk),
125
                    'user_name': self.tester.username,
126
                    'submit_date': self.submit_date,
127
                    'comment': 'make better'
128
                }
129
            ],
130
            self.execution_5.pk: [
131
                {
132
                    'case_run_id': str(self.execution_5.pk),
133
                    'user_name': self.tester.username,
134
                    'submit_date': self.submit_date,
135
                    'comment': 'new comment'
136
                }
137
            ]
138
        }
139
140
        for exp_key in expected_comments:
141
            for exp_cmt in expected_comments[exp_key]:
142
                self.assertIn(exp_cmt, comments[exp_key])
143