Passed
Push — master ( cddcf6...1d3855 )
by Alexander
01:59
created

tcms.xmlrpc.tests.test_testbuild   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 157
Duplicated Lines 14.01 %

Importance

Changes 0
Metric Value
wmc 31
eloc 117
dl 22
loc 157
rs 9.92
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A BuildCreate._fixture_setup() 0 4 1
A BuildCreate.test_build_create_with_no_perms() 0 4 2
A BuildCreate.test_build_create_with_no_args() 0 5 3
A BuildCreate.test_build_create_with_no_required_fields() 0 15 4
A BuildFilter.test_build_filter_with_non_exist_id() 0 2 1
A BuildFilter.test_build_filter_with_name_and_product() 0 10 1
A BuildUpdate.test_build_update_with_no_perms() 0 4 2
A BuildUpdate.test_build_update_with_non_existing_product_name() 0 3 2
A BuildUpdate.test_build_update_with_multi_id() 0 4 2
A BuildUpdate.test_build_update_with_non_existing_build() 0 3 2
A BuildUpdate.test_build_update_with_non_existing_product_id() 0 3 2
A BuildCreate.test_build_create() 11 11 1
A BuildFilter._fixture_setup() 0 5 1
A BuildCreate.test_build_create_with_non_existing_product() 0 12 3
A BuildUpdate.test_build_update() 0 8 1
A BuildUpdate._fixture_setup() 0 9 1
A BuildFilter.test_build_filter_with_id() 0 7 1
A BuildCreate.test_build_create_with_chinese() 11 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# -*- coding: utf-8 -*-
2
# pylint: disable=invalid-name, attribute-defined-outside-init, objects-update-used
3
4
from xmlrpc.client import ProtocolError
5
from xmlrpc.client import Fault as XmlRPCFault
6
7
from tcms.tests.factories import ProductFactory
8
from tcms.tests.factories import BuildFactory
9
from tcms.xmlrpc.tests.utils import XmlrpcAPIBaseTest
10
11
12
class BuildCreate(XmlrpcAPIBaseTest):
13
14
    def _fixture_setup(self):
15
        super(BuildCreate, self)._fixture_setup()
16
17
        self.product = ProductFactory()
18
19
    def test_build_create_with_no_args(self):
20
        bad_args = ([], (), {})
21
        for arg in bad_args:
22
            with self.assertRaisesRegex(XmlRPCFault, 'Internal error:'):
23
                self.rpc_client.exec.Build.create(arg)
24
25
    def test_build_create_with_no_perms(self):
26
        self.rpc_client.exec.Auth.logout()
27
        with self.assertRaisesRegex(ProtocolError, '403 Forbidden'):
28
            self.rpc_client.exec.Build.create({})
29
30
    def test_build_create_with_no_required_fields(self):
31
        values = {
32
            "is_active": False
33
        }
34
        with self.assertRaisesRegex(XmlRPCFault, 'Product and name are both required'):
35
            self.rpc_client.exec.Build.create(values)
36
37
        values["name"] = "TB"
38
        with self.assertRaisesRegex(XmlRPCFault, 'Product and name are both required'):
39
            self.rpc_client.exec.Build.create(values)
40
41
        del values["name"]
42
        values["product"] = self.product.pk
43
        with self.assertRaisesRegex(XmlRPCFault, 'Product and name are both required'):
44
            self.rpc_client.exec.Build.create(values)
45
46
    def test_build_create_with_non_existing_product(self):
47
        values = {
48
            "product": 9999,
49
            "name": "B7",
50
            "is_active": False
51
        }
52
        with self.assertRaisesRegex(XmlRPCFault, 'Product matching query does not exist'):
53
            self.rpc_client.exec.Build.create(values)
54
55
        values['product'] = "AAAAAAAAAA"
56
        with self.assertRaisesRegex(XmlRPCFault, 'Product matching query does not exist'):
57
            self.rpc_client.exec.Build.create(values)
58
59 View Code Duplication
    def test_build_create_with_chinese(self):
0 ignored issues
show
Duplication introduced by jianchen
This code seems to be duplicated in your project.
Loading history...
60
        values = {
61
            "product": self.product.pk,
62
            "name": "开源中国",
63
            "is_active": False
64
        }
65
        b = self.rpc_client.exec.Build.create(values)
66
        self.assertIsNotNone(b)
67
        self.assertEqual(b['product_id'], self.product.pk)
68
        self.assertEqual(b['name'], "开源中国")
69
        self.assertEqual(b['is_active'], False)
70
71 View Code Duplication
    def test_build_create(self):
0 ignored issues
show
Duplication introduced by jianchen
This code seems to be duplicated in your project.
Loading history...
72
        values = {
73
            "product": self.product.pk,
74
            "name": "B7",
75
            "is_active": False
76
        }
77
        b = self.rpc_client.exec.Build.create(values)
78
        self.assertIsNotNone(b)
79
        self.assertEqual(b['product_id'], self.product.pk)
80
        self.assertEqual(b['name'], "B7")
81
        self.assertEqual(b['is_active'], False)
82
83
84
class BuildUpdate(XmlrpcAPIBaseTest):
85
86
    def _fixture_setup(self):
87
        super(BuildUpdate, self)._fixture_setup()
88
89
        self.product = ProductFactory()
90
        self.another_product = ProductFactory()
91
92
        self.build_1 = BuildFactory(product=self.product)
93
        self.build_2 = BuildFactory(product=self.product)
94
        self.build_3 = BuildFactory(product=self.product)
95
96
    def test_build_update_with_non_existing_build(self):
97
        with self.assertRaisesRegex(XmlRPCFault, 'Build matching query does not exist'):
98
            self.rpc_client.exec.Build.update(-99, {})
99
100
    def test_build_update_with_no_perms(self):
101
        self.rpc_client.exec.Auth.logout()
102
        with self.assertRaisesRegex(ProtocolError, '403 Forbidden'):
103
            self.rpc_client.exec.Build.update(self.build_1.pk, {})
104
105
    def test_build_update_with_multi_id(self):
106
        builds = (self.build_1.pk, self.build_2.pk, self.build_3.pk)
107
        with self.assertRaisesRegex(XmlRPCFault, 'Invalid parameter'):
108
            self.rpc_client.exec.Build.update(builds, {})
109
110
    def test_build_update_with_non_existing_product_id(self):
111
        with self.assertRaisesRegex(XmlRPCFault, 'Product matching query does not exist'):
112
            self.rpc_client.exec.Build.update(self.build_1.pk, {"product": -9999})
113
114
    def test_build_update_with_non_existing_product_name(self):
115
        with self.assertRaisesRegex(XmlRPCFault, 'Product matching query does not exist'):
116
            self.rpc_client.exec.Build.update(self.build_1.pk, {"product": "AAAAAAAAAAAAAA"})
117
118
    def test_build_update(self):
119
        b = self.rpc_client.exec.Build.update(self.build_3.pk, {
120
            "product": self.another_product.pk,
121
            "name": "Update",
122
        })
123
        self.assertIsNotNone(b)
124
        self.assertEqual(b['product_id'], self.another_product.pk)
125
        self.assertEqual(b['name'], 'Update')
126
127
128
class BuildFilter(XmlrpcAPIBaseTest):
129
130
    def _fixture_setup(self):
131
        super(BuildFilter, self)._fixture_setup()
132
133
        self.product = ProductFactory()
134
        self.build = BuildFactory(product=self.product)
135
136
    def test_build_filter_with_non_exist_id(self):
137
        self.assertEqual(0, len(self.rpc_client.exec.Build.filter({'pk': -9999})))
138
139
    def test_build_filter_with_id(self):
140
        b = self.rpc_client.exec.Build.filter({'pk': self.build.pk})[0]
141
        self.assertIsNotNone(b)
142
        self.assertEqual(b['build_id'], self.build.pk)
143
        self.assertEqual(b['name'], self.build.name)
144
        self.assertEqual(b['product_id'], self.product.pk)
145
        self.assertTrue(b['is_active'])
146
147
    def test_build_filter_with_name_and_product(self):
148
        b = self.rpc_client.exec.Build.filter({
149
            'name': self.build.name,
150
            'product': self.product.pk
151
        })[0]
152
        self.assertIsNotNone(b)
153
        self.assertEqual(b['build_id'], self.build.pk)
154
        self.assertEqual(b['name'], self.build.name)
155
        self.assertEqual(b['product_id'], self.product.pk)
156
        self.assertEqual(b['is_active'], True)
157