Passed
Push — master ( 68a360...546f3a )
by Fabio
03:54
created

UtilityDictTestCase.test_merge()   B

Complexity

Conditions 1

Size

Total Lines 64
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 64
rs 8.7345
c 0
b 0
f 0
cc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
# -*- coding: utf-8 -*-
2
3
from benedict.dicts import UtilityDict
4
from datetime import datetime
5
from decimal import Decimal
6
7
import unittest
8
9
10
class UtilityDictTestCase(unittest.TestCase):
11
12
    def test_clean(self):
13
        d = {
14
            'a': {},
15
            'b': { 'x': 1 },
16
            'c': [],
17
            'd': [0, 1],
18
            'e': 0.0,
19
            'f': '',
20
            'g': None,
21
            'h': '0'
22
        }
23
24
        b = UtilityDict(d)
25
        b.clean()
26
        r = {
27
            'b': { 'x': 1 },
28
            'd': [0, 1],
29
            'e': 0.0,
30
            'h': '0',
31
        }
32
        self.assertEqual(b, r)
33
34
        b = UtilityDict(d)
35
        b.clean(dicts=False)
36
        r = {
37
            'a': {},
38
            'b': { 'x': 1 },
39
            'd': [0, 1],
40
            'e': 0.0,
41
            'h': '0'
42
        }
43
        self.assertEqual(b, r)
44
45
        b = UtilityDict(d)
46
        b.clean(lists=False)
47
        r = {
48
            'b': { 'x': 1 },
49
            'c': [],
50
            'd': [0, 1],
51
            'e': 0.0,
52
            'h': '0'
53
        }
54
        self.assertEqual(b, r)
55
56
        b = UtilityDict(d)
57
        b.clean(strings=False)
58
        r = {
59
            'b': { 'x': 1 },
60
            'd': [0, 1],
61
            'e': 0.0,
62
            'f': '',
63
            'h': '0',
64
        }
65
        self.assertEqual(b, r)
66
67
    def test_clone(self):
68
        d = {
69
            'a': {
70
                'b': {
71
                    'c': 1
72
                }
73
            }
74
        }
75
        b = UtilityDict(d)
76
        c = b.clone()
77
        self.assertEqual(b, c)
78
        self.assertFalse(c is b)
79
        c['a']['b']['c'] = 2
80
        self.assertEqual(b['a']['b']['c'], 1)
81
        self.assertEqual(c['a']['b']['c'], 2)
82
83
    def test_deepcopy(self):
84
        d = {
85
            'a': {
86
                'b': {
87
                    'c': 1
88
                }
89
            }
90
        }
91
        b = UtilityDict(d)
92
        c = b.deepcopy()
93
        self.assertEqual(b, c)
94
        self.assertFalse(c is b)
95
        c['a']['b']['c'] = 2
96
        self.assertEqual(b['a']['b']['c'], 1)
97
        self.assertEqual(c['a']['b']['c'], 2)
98
99
    def test_deepupdate_with_single_dict(self):
100
        d = {
101
            'a': 1,
102
            'b': 1,
103
        }
104
        a = {
105
            'b': 2,
106
            'c': 3,
107
        }
108
        d = UtilityDict(d)
109
        d.deepupdate(a)
110
        r = {
111
            'a': 1,
112
            'b': 2,
113
            'c': 3,
114
        }
115
        self.assertEqual(d, r)
116
117
    def test_deepupdate_with_multiple_dicts(self):
118
        d = {
119
            'a': 1,
120
            'b': 1,
121
        }
122
        a = {
123
            'b': 2,
124
            'c': 3,
125
            'd': 3,
126
        }
127
        b = {
128
            'd': 5,
129
            'e': 5,
130
        }
131
        c = {
132
            'd': 4,
133
            'f': 6,
134
        }
135
        d = UtilityDict(d)
136
        d.deepupdate(a, b, c)
137
        r = {
138
            'a': 1,
139
            'b': 2,
140
            'c': 3,
141
            'd': 4,
142
            'e': 5,
143
            'f': 6,
144
        }
145
        self.assertEqual(d, r)
146
147
    def test_deepupdate(self):
148
        d = {
149
            'a': 1,
150
            'b': {
151
                'c': {
152
                    'x': 2,
153
                    'y': 3,
154
                },
155
                'd': {
156
                    'x': 4,
157
                    'y': 5,
158
                },
159
                'e': {
160
                    'x': 6,
161
                    'y': 7,
162
                },
163
            },
164
        }
165
        a = {
166
            'a': 0,
167
            'b': {
168
                'c': 1,
169
                'd': {
170
                    'y': 1,
171
                    'z': 2,
172
                },
173
                'e': {
174
                    'f': {
175
                        'x': 2,
176
                        'y': 3,
177
                    },
178
                    'g': {
179
                        'x': 4,
180
                        'y': 5,
181
                    },
182
                },
183
            },
184
        }
185
        d = UtilityDict(d)
186
        d.deepupdate(a)
187
        r = {
188
            'a': 0,
189
            'b': {
190
                'c': 1,
191
                'd': {
192
                    'x': 4,
193
                    'y': 1,
194
                    'z': 2,
195
                },
196
                'e': {
197
                    'f': {
198
                        'x': 2,
199
                        'y': 3,
200
                    },
201
                    'g': {
202
                        'x': 4,
203
                        'y': 5,
204
                    },
205
                    'x': 6,
206
                    'y': 7,
207
                },
208
            },
209
        }
210
        self.assertEqual(d, r)
211
212
    def test_dump(self):
213
        d = {
214
            'a': {
215
                'b': {
216
                    'c': 1
217
                }
218
            }
219
        }
220
        b = UtilityDict(d)
221
        expected_output = """{
222
    "a": {
223
        "b": {
224
            "c": 1
225
        }
226
    }
227
}"""
228
        output = UtilityDict.dump(b)
229
        self.assertEqual(output, expected_output)
230
        output = b.dump()
231
        self.assertEqual(output, expected_output)
232
233
    def test_dump_with_datetime(self):
234
        d = {
235
            'datetime': datetime(2019, 6, 11),
236
        }
237
        b = UtilityDict(d)
238
        expected_output = """{
239
    "datetime": "2019-06-11 00:00:00"
240
}"""
241
        output = b.dump()
242
        self.assertEqual(output, expected_output)
243
244
    def test_dump_with_decimal(self):
245
        d = {
246
            'decimal': Decimal('1.75'),
247
        }
248
        b = UtilityDict(d)
249
        expected_output = """{
250
    "decimal": "1.75"
251
}"""
252
        output = b.dump()
253
        self.assertEqual(output, expected_output)
254
255
    def test_filter(self):
256
        d = {
257
            'a': 1,
258
            'b': 2,
259
            'c': '4',
260
            'e': '5',
261
            'f': 6,
262
            'g': 7,
263
        }
264
        b = UtilityDict(d)
265
        with self.assertRaises(ValueError):
266
            f = b.filter(True)
267
        f = b.filter(lambda key, val: isinstance(val, int))
268
        r = {
269
            'a': 1,
270
            'b': 2,
271
            'f': 6,
272
            'g': 7,
273
        }
274
        self.assertEqual(f, r)
275
        self.assertFalse(b is f)
276
277
    def test_merge_with_single_dict(self):
278
        d = {
279
            'a': 1,
280
            'b': 1,
281
        }
282
        a = {
283
            'b': 2,
284
            'c': 3,
285
        }
286
        d = UtilityDict(d)
287
        d.merge(a)
288
        r = {
289
            'a': 1,
290
            'b': 2,
291
            'c': 3,
292
        }
293
        self.assertEqual(d, r)
294
295
    def test_merge_with_multiple_dicts(self):
296
        d = {
297
            'a': 1,
298
            'b': 1,
299
        }
300
        a = {
301
            'b': 2,
302
            'c': 3,
303
            'd': 3,
304
        }
305
        b = {
306
            'd': 5,
307
            'e': 5,
308
        }
309
        c = {
310
            'd': 4,
311
            'f': 6,
312
        }
313
        d = UtilityDict(d)
314
        d.merge(a, b, c)
315
        r = {
316
            'a': 1,
317
            'b': 2,
318
            'c': 3,
319
            'd': 4,
320
            'e': 5,
321
            'f': 6,
322
        }
323
        self.assertEqual(d, r)
324
325
    def test_merge(self):
326
        d = {
327
            'a': 1,
328
            'b': {
329
                'c': {
330
                    'x': 2,
331
                    'y': 3,
332
                },
333
                'd': {
334
                    'x': 4,
335
                    'y': 5,
336
                },
337
                'e': {
338
                    'x': 6,
339
                    'y': 7,
340
                },
341
            },
342
        }
343
        a = {
344
            'a': 0,
345
            'b': {
346
                'c': 1,
347
                'd': {
348
                    'y': 1,
349
                    'z': 2,
350
                },
351
                'e': {
352
                    'f': {
353
                        'x': 2,
354
                        'y': 3,
355
                    },
356
                    'g': {
357
                        'x': 4,
358
                        'y': 5,
359
                    },
360
                },
361
            },
362
        }
363
        d = UtilityDict(d)
364
        d.merge(a)
365
        r = {
366
            'a': 0,
367
            'b': {
368
                'c': 1,
369
                'd': {
370
                    'x': 4,
371
                    'y': 1,
372
                    'z': 2,
373
                },
374
                'e': {
375
                    'f': {
376
                        'x': 2,
377
                        'y': 3,
378
                    },
379
                    'g': {
380
                        'x': 4,
381
                        'y': 5,
382
                    },
383
                    'x': 6,
384
                    'y': 7,
385
                },
386
            },
387
        }
388
        self.assertEqual(d, r)
389
390
    def test_remove(self):
391
        d = {
392
            'a': 1,
393
            'b': 2,
394
            'c': '4',
395
            'e': '5',
396
            'f': 6,
397
            'g': 7,
398
        }
399
        b = UtilityDict(d)
400
        b.remove(['c', 'e', 'f', 'g'])
401
        r = {
402
            'a': 1,
403
            'b': 2,
404
        }
405
        self.assertEqual(b, r)
406
407
    def test_subset(self):
408
        d = {
409
            'a': 1,
410
            'b': 2,
411
            'c': '4',
412
            'e': '5',
413
            'f': 6,
414
            'g': 7,
415
        }
416
        b = UtilityDict(d)
417
        f = b.subset(['c', 'f', 'x'])
418
        r = {
419
            'c': '4',
420
            'f': 6,
421
            'x': None,
422
        }
423
        self.assertEqual(f, r)
424
        self.assertFalse(f is b)
425