Passed
Branch master (9e8f7a)
by Oleksandr
01:29
created

tests.test_ext   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 317
Duplicated Lines 19.24 %

Importance

Changes 0
Metric Value
wmc 21
eloc 215
dl 61
loc 317
rs 10
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A ValuesTestCase.test_get_by_value_with_duplicates() 0 8 1
A ValuesTestCase.test_get_by_value() 0 7 1
A ValuesTestCase.test_filter_by_value() 0 17 1
A VerboseConstantTestCase.test_all_args() 0 9 1
A VerboseConstantTestCase.test_help_text() 0 3 1
A VerboseConstantTestCase.test_group() 29 29 1
A ValuesTestCase.test_to_primitive_scalar() 0 10 1
A ValuesTestCase.test_group() 0 24 1
A VerboseConstantTestCase.test_versose_name() 0 3 1
A VerboseValueConstantTestCase.test_group() 32 32 1
A VerboseConstantTestCase.test_to_primitive_no_args() 0 11 1
A VerboseConstantTestCase.test_to_primitive_all_args() 0 11 1
A ValuesTestCase.test_to_primitive_date() 0 10 1
A ValuesTestCase.test_values() 0 11 1
A ValuesTestCase.test_get_by_value_missing() 0 11 2
A ValuesTestCase.test_to_primitive_callable() 0 10 2
A VerboseConstantTestCase.test_no_args() 0 9 1
A ValuesTestCase.test_to_primitive_custom() 0 18 1
A ValuesTestCase.test_itervalues() 0 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
import operator
2
import unittest
3
4
from collections.abc import Iterator
5
6
from datetime import date
7
8
from dataclasses import dataclass
9
10
from candv.core import Constants
11
from candv.core import SimpleConstant
12
13
from candv.exceptions import CandvValueNotFoundError
14
15
from candv.ext import VerboseConstant
16
from candv.ext import Values
17
from candv.ext import ValueConstant
18
from candv.ext import VerboseValueConstant
19
20
21
class VerboseConstantTestCase(unittest.TestCase):
22
23
  def test_no_args(self):
24
25
    class FOO(Constants):
26
      CONSTANT = VerboseConstant()
27
28
    self.assertEqual(FOO.CONSTANT.name, "CONSTANT")
29
    self.assertEqual(FOO.CONSTANT.full_name, "FOO.CONSTANT")
30
    self.assertIsNone(FOO.CONSTANT.verbose_name)
31
    self.assertIsNone(FOO.CONSTANT.help_text)
32
33
  def test_versose_name(self):
34
    constant = VerboseConstant(verbose_name="foo")
35
    self.assertEqual(constant.verbose_name, "foo")
36
37
  def test_help_text(self):
38
    constant = VerboseConstant(help_text="just test constant")
39
    self.assertEqual(constant.help_text, "just test constant")
40
41
  def test_all_args(self):
42
43
    class FOO(Constants):
44
      CONSTANT = VerboseConstant("foo", "just test constant")
45
46
    self.assertEqual(FOO.CONSTANT.name, "CONSTANT")
47
    self.assertEqual(FOO.CONSTANT.full_name, "FOO.CONSTANT")
48
    self.assertEqual(FOO.CONSTANT.verbose_name, "foo")
49
    self.assertEqual(FOO.CONSTANT.help_text, "just test constant")
50
51
  def test_to_primitive_no_args(self):
52
53
    class FOO(Constants):
54
      CONSTANT = VerboseConstant()
55
56
    self.assertEqual(
57
      FOO.CONSTANT.to_primitive(),
58
      {
59
        'name': "CONSTANT",
60
        'verbose_name': None,
61
        'help_text': None,
62
      }
63
    )
64
65
  def test_to_primitive_all_args(self):
66
67
    class FOO(Constants):
68
      CONSTANT = VerboseConstant("Constant", "A test constant")
69
70
    self.assertEqual(
71
      FOO.CONSTANT.to_primitive(),
72
      {
73
        'name': "CONSTANT",
74
        'verbose_name': "Constant",
75
        'help_text': "A test constant",
76
      }
77
    )
78
79 View Code Duplication
  def test_group(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
80
81
    class FOO(Constants):
82
      A = SimpleConstant()
83
      B = VerboseConstant(
84
        verbose_name="Constant B",
85
        help_text="Just a group with verbose name"
86
      ).to_group(
87
        group_class=Constants,
88
        C=SimpleConstant(),
89
        D=SimpleConstant(),
90
      )
91
92
    self.assertEqual(FOO.B.verbose_name, "Constant B")
93
    self.assertEqual(FOO.B.help_text, "Just a group with verbose name")
94
    self.assertEqual(FOO.B.names(), ["C", "D", ])
95
    self.assertEqual(
96
      FOO.to_primitive(),
97
      {
98
        'name': "FOO",
99
        'items': [
100
          {'name': "A", },
101
          {
102
            'name': "B",
103
            'verbose_name': "Constant B",
104
            'help_text': "Just a group with verbose name",
105
            'items': [
106
              {'name': "C", },
107
              {'name': "D", },
108
            ],
109
          },
110
        ],
111
      },
112
    )
113
114
115
class ValuesTestCase(unittest.TestCase):
116
117
  def test_get_by_value(self):
118
119
    class FOO(Values):
120
      ONE = ValueConstant(1)
121
      TWO = ValueConstant(2)
122
123
    self.assertEqual(FOO.get_by_value(2), FOO.TWO)
124
125
  def test_get_by_value_missing(self):
126
127
    class FOO(Values):
128
      ONE = ValueConstant(1)
129
130
    with self.assertRaises(CandvValueNotFoundError) as cm:
131
      FOO.get_by_value(2)
132
133
    self.assertEqual(
134
      cm.exception.args[0],
135
      "constant with value \"2\" is not present in "
136
      "\"<constants container 'FOO'>\""
137
    )
138
139
  def test_get_by_value_with_duplicates(self):
140
141
    class FOO(Values):
142
      ONE = ValueConstant(1)
143
      TWO = ValueConstant(2)
144
      TWO_DUB = ValueConstant(1)
145
146
    self.assertEqual(FOO.get_by_value(2), FOO.TWO)
147
148
  def test_filter_by_value(self):
149
150
    class FOO(Values):
151
      ONE = ValueConstant(1)
152
      TWO = ValueConstant(2)
153
      ONE_DUB2 = ValueConstant(1)
154
      THREE = ValueConstant(3)
155
      ONE_DUB1 = ValueConstant(1)
156
157
    constants = FOO.filter_by_value(1)
158
    self.assertIsInstance(constants, list)
159
    self.assertEqual(
160
      list(map(operator.attrgetter("name"), constants)),
161
      [
162
        "ONE",
163
        "ONE_DUB2",
164
        "ONE_DUB1",
165
      ],
166
    )
167
168
  def test_values(self):
169
170
    class FOO(Values):
171
      ONE = ValueConstant(1)
172
      THREE = ValueConstant(3)
173
      TWO = ValueConstant(2)
174
175
    values = FOO.values()
176
177
    self.assertIsInstance(values, list)
178
    self.assertEqual(values, [1, 3, 2, ])
179
180
  def test_itervalues(self):
181
182
    class FOO(Values):
183
      ONE = ValueConstant(1)
184
      THREE = ValueConstant(3)
185
      TWO = ValueConstant(2)
186
187
    values = FOO.itervalues()
188
189
    self.assertIsInstance(values, Iterator)
190
    self.assertEqual(list(values), [1, 3, 2, ])
191
192
  def test_to_primitive_scalar(self):
193
194
    class FOO(Values):
195
      ONE = ValueConstant(1)
196
197
    self.assertEqual(
198
      FOO.ONE.to_primitive(),
199
      {
200
        'name': "ONE",
201
        'value': 1,
202
      },
203
    )
204
205
  def test_to_primitive_callable(self):
206
207
    class FOO(Values):
208
      ONE = ValueConstant(lambda: 1)
209
210
    self.assertEqual(
211
      FOO.ONE.to_primitive(),
212
      {
213
        'name': "ONE",
214
        'value': 1,
215
      },
216
    )
217
218
  def test_to_primitive_custom(self):
219
220
    @dataclass
221
    class Point2D:
222
      x: int
223
      y: int
224
225
      def to_primitive(self, context=None):
226
        return f"{self.x}:{self.y}"
227
228
    class FOO(Values):
229
      POS = ValueConstant(Point2D(10, 20))
230
231
    self.assertEqual(
232
      FOO.POS.to_primitive(),
233
      {
234
        'name': "POS",
235
        'value': "10:20",
236
      },
237
    )
238
239
  def test_to_primitive_date(self):
240
241
    class FOO(Values):
242
      DATE = ValueConstant(date(1999, 12, 31))
243
244
    self.assertEqual(
245
      FOO.DATE.to_primitive(),
246
      {
247
        'name': "DATE",
248
        'value': "1999-12-31",
249
      },
250
    )
251
252
  def test_group(self):
253
254
    class FOO(Constants):
255
      A = SimpleConstant()
256
      B = ValueConstant(10).to_group(
257
        group_class=Constants,
258
        C=SimpleConstant(),
259
        D=SimpleConstant(),
260
      )
261
262
    self.assertEqual(FOO.B.value, 10)
263
    self.assertEqual(FOO.B.names(), ["C", "D", ])
264
    self.assertEqual(
265
      FOO.to_primitive(),
266
      {
267
        'name': "FOO",
268
        'items': [
269
          {'name': "A", },
270
          {
271
            'name': "B",
272
            'value': 10,
273
            'items': [
274
              {'name': "C", },
275
              {'name': "D", },
276
            ],
277
          },
278
        ],
279
      },
280
    )
281
282
283
class VerboseValueConstantTestCase(unittest.TestCase):
284
285 View Code Duplication
  def test_group(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
286
287
    class FOO(Constants):
288
      A = SimpleConstant()
289
      B = VerboseValueConstant(
290
        value=10,
291
        verbose_name="Constant B",
292
        help_text="A group with verbose name and value"
293
      ).to_group(
294
        group_class=Constants,
295
        C=SimpleConstant(),
296
        D=SimpleConstant(),
297
      )
298
299
    self.assertEqual(FOO.B.value, 10)
300
    self.assertEqual(FOO.B.verbose_name, "Constant B")
301
    self.assertEqual(FOO.B.help_text, "A group with verbose name and value")
302
    self.assertEqual(FOO.B.names(), ["C", "D", ])
303
    self.assertEqual(
304
      FOO.to_primitive(),
305
      {
306
        'name': "FOO",
307
        'items': [
308
          {'name': "A", },
309
          {
310
            'name': "B",
311
            'verbose_name': "Constant B",
312
            'help_text': "A group with verbose name and value",
313
            'value': 10,
314
            'items': [
315
                {'name': "C", },
316
                {'name': "D", },
317
            ],
318
          },
319
        ],
320
      },
321
    )
322