Passed
Push — master ( afd6fa...9657ba )
by Alexander
02:39
created

TestActualDurationProperty.test_actual_duration_empty_stop_date()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# pylint: disable=invalid-name, no-member
3
4
from django.conf import settings
5
from django.template.loader import render_to_string
6
from django.test import TestCase
7
from django.utils import timezone
8
from django.utils.translation import gettext_lazy as _
9
from mock import patch
10
11
from tcms.core.history import history_email_for
12
from tcms.testcases.helpers.email import get_case_notification_recipients
13
from tcms.testruns.models import TestExecution
14
from tcms.tests import BasePlanCase
15
from tcms.tests.factories import (
16
    ComponentFactory,
17
    TagFactory,
18
    TestCaseComponentFactory,
19
    TestCaseFactory,
20
    TestCaseTagFactory,
21
)
22
23
24
class SupportsCyrillic(TestCase):
25
    def test_create_testcase_with_cyrillic_works_issue_1770(self):
26
        # https://github.com/kiwitcms/Kiwi/issues/1770
27
        case = TestCaseFactory(summary="Това е тест на кирилица")
28
        case.save()
29
30
        case.refresh_from_db()
31
        self.assertTrue(case.summary.endswith("кирилица"))
32
33
34
class TestCaseRemoveComponent(BasePlanCase):
35
    """Test TestCase.remove_component"""
36
37
    @classmethod
38
    def setUpTestData(cls):
39
        super(TestCaseRemoveComponent, cls).setUpTestData()
40
41
        cls.component_1 = ComponentFactory(
42
            name="Application",
43
            product=cls.product,
44
            initial_owner=cls.tester,
45
            initial_qa_contact=cls.tester,
46
        )
47
        cls.component_2 = ComponentFactory(
48
            name="Database",
49
            product=cls.product,
50
            initial_owner=cls.tester,
51
            initial_qa_contact=cls.tester,
52
        )
53
54
        cls.cc_rel_1 = TestCaseComponentFactory(
55
            case=cls.case, component=cls.component_1
56
        )
57
        cls.cc_rel_2 = TestCaseComponentFactory(
58
            case=cls.case, component=cls.component_2
59
        )
60
61
    def test_remove_a_component(self):
62
        self.case.remove_component(self.component_1)
63
64
        found = self.case.component.filter(pk=self.component_1.pk).exists()
65
        self.assertFalse(
66
            found,
67
            "Component {0} exists. But, it should be removed.".format(
68
                self.component_1.pk
69
            ),
70
        )
71
        found = self.case.component.filter(pk=self.component_2.pk).exists()
72
        self.assertTrue(
73
            found,
74
            "Component {0} does not exist. It should not be removed.".format(
75
                self.component_2.pk
76
            ),
77
        )
78
79
80
class TestCaseRemoveTag(BasePlanCase):
81
    """Test TestCase.remove_tag"""
82
83
    @classmethod
84
    def setUpTestData(cls):
85
        super(TestCaseRemoveTag, cls).setUpTestData()
86
87
        cls.tag_rhel = TagFactory(name="rhel")
88
        cls.tag_fedora = TagFactory(name="fedora")
89
        TestCaseTagFactory(case=cls.case, tag=cls.tag_rhel)
90
        TestCaseTagFactory(case=cls.case, tag=cls.tag_fedora)
91
92
    def test_remove_tag(self):
93
        self.case.remove_tag(self.tag_rhel)
94
95
        tag_pks = list(self.case.tag.all().values_list("pk", flat=True))
96
        self.assertEqual([self.tag_fedora.pk], tag_pks)
97
98
99
class TestSendMailOnCaseIsUpdated(BasePlanCase):
100
    """Test send mail on case post_save signal is triggered"""
101
102
    @classmethod
103
    def setUpTestData(cls):
104
        super().setUpTestData()
105
106
        cls.case.emailing.notify_on_case_update = True
107
        cls.case.emailing.auto_to_case_author = True
108
        cls.case.emailing.save()
109
110
    @patch("tcms.core.utils.mailto.send_mail")
111
    def test_send_mail_to_case_author(self, send_mail):
112
        self.case.summary = "New summary for running test"
113
        self.case.save()
114
115
        expected_subject, expected_body = history_email_for(
116
            self.case, self.case.summary
117
        )
118
        recipients = get_case_notification_recipients(self.case)
119
120
        # Verify notification mail
121
        send_mail.assert_called_once_with(
122
            settings.EMAIL_SUBJECT_PREFIX + expected_subject,
123
            expected_body,
124
            settings.DEFAULT_FROM_EMAIL,
125
            recipients,
126
            fail_silently=False,
127
        )
128
129
130
class TestSendMailOnCaseIsDeleted(BasePlanCase):
131
    """Test send mail on case post_delete signal is triggered"""
132
133
    @classmethod
134
    def setUpTestData(cls):
135
        super().setUpTestData()
136
137
        cls.case.emailing.notify_on_case_delete = True
138
        cls.case.emailing.auto_to_case_author = True
139
        cls.case.emailing.save()
140
141
    @patch("tcms.core.utils.mailto.send_mail")
142
    def test_send_mail_to_case_author(self, send_mail):
143
        expected_subject = _("DELETED: TestCase #%(pk)d - %(summary)s") % {
144
            "pk": self.case.pk,
145
            "summary": self.case.summary,
146
        }
147
        expected_body = render_to_string(
148
            "email/post_case_delete/email.txt", {"case": self.case}
149
        )
150
        recipients = get_case_notification_recipients(self.case)
151
152
        self.case.delete()
153
154
        # Verify notification mail
155
        send_mail.assert_called_once_with(
156
            settings.EMAIL_SUBJECT_PREFIX + expected_subject,
157
            expected_body,
158
            settings.DEFAULT_FROM_EMAIL,
159
            recipients,
160
            fail_silently=False,
161
        )
162
163
164
class TestActualDurationProperty(TestCase):
165
    """Test TestExecution.actual_duration"""
166
167
    @classmethod
168
    def setUpTestData(cls):
169
        super().setUpTestData()
170
171
        cls.test_execution = TestExecution()
172
        cls.test_execution.start_date = timezone.now()
173
        cls.test_execution.stop_date = (
174
            cls.test_execution.start_date + timezone.timedelta(days=1)
175
        )
176
177
    def test_calculation_of_actual_duration(self):
178
        self.assertEqual(
179
            self.test_execution.actual_duration, timezone.timedelta(days=1)
180
        )
181
182
    def test_actual_duration_empty_start_date(self):
183
        empty_start_date_actual_duration = TestExecution(start_date=None)
184
        self.assertEqual(empty_start_date_actual_duration.actual_duration, None)
185
186
    def test_actual_duration_empty_stop_date(self):
187
        empty_stop_date_actual_duration = TestExecution(stop_date=None)
188
        self.assertEqual(empty_stop_date_actual_duration.actual_duration, None)
189