Passed
Push — master ( 929d80...68a360 )
by Fabio
04:07
created

UtilityDictTestCase.test_subset()   A

Complexity

Conditions 1

Size

Total Lines 18
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 18
rs 9.6
c 0
b 0
f 0
cc 1
nop 1
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_deepcopy(self):
68
        d = {
69
            'a': {
70
                'b': {
71
                    'c': 1
72
                }
73
            }
74
        }
75
        b = UtilityDict(d)
76
        c = b.deepcopy()
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_dump(self):
84
        d = {
85
            'a': {
86
                'b': {
87
                    'c': 1
88
                }
89
            }
90
        }
91
        b = UtilityDict(d)
92
        expected_output = """{
93
    "a": {
94
        "b": {
95
            "c": 1
96
        }
97
    }
98
}"""
99
        output = UtilityDict.dump(b)
100
        self.assertEqual(output, expected_output)
101
102
    def test_dump_items(self):
103
        d = {
104
            'a': {
105
                'b': {
106
                    'c': 1
107
                }
108
            }
109
        }
110
        b = UtilityDict(d)
111
        print(b.dump_items())
112
        expected_output = """{
113
    "a": {
114
        "b": {
115
            "c": 1
116
        }
117
    }
118
}"""
119
        output = b.dump_items()
120
        self.assertEqual(output, expected_output)
121
122
    def test_dump_items_with_key(self):
123
        d = {
124
            'a': {
125
                'b': 1
126
            }
127
        }
128
        b = UtilityDict(d)
129
        expected_output = """{
130
    "b": 1
131
}"""
132
        output = b.dump_items('a')
133
        self.assertEqual(output, expected_output)
134
135
    def test_dump_items_with_datetime(self):
136
        d = {
137
            'datetime': datetime(2019, 6, 11),
138
        }
139
        b = UtilityDict(d)
140
        expected_output = """{
141
    "datetime": "2019-06-11 00:00:00"
142
}"""
143
        output = b.dump_items()
144
        self.assertEqual(output, expected_output)
145
146
    def test_dump_items_with_decimal(self):
147
        d = {
148
            'decimal': Decimal('1.75'),
149
        }
150
        b = UtilityDict(d)
151
        expected_output = """{
152
    "decimal": "1.75"
153
}"""
154
        output = b.dump_items()
155
        self.assertEqual(output, expected_output)
156
157
    def test_filter(self):
158
        d = {
159
            'a': 1,
160
            'b': 2,
161
            'c': '4',
162
            'e': '5',
163
            'f': 6,
164
            'g': 7,
165
        }
166
        b = UtilityDict(d)
167
        with self.assertRaises(ValueError):
168
            f = b.filter(True)
169
        f = b.filter(lambda key, val: isinstance(val, int))
170
        r = {
171
            'a': 1,
172
            'b': 2,
173
            'f': 6,
174
            'g': 7,
175
        }
176
        self.assertEqual(f, r)
177
        self.assertFalse(b is f)
178
179
    def test_remove(self):
180
        d = {
181
            'a': 1,
182
            'b': 2,
183
            'c': '4',
184
            'e': '5',
185
            'f': 6,
186
            'g': 7,
187
        }
188
        b = UtilityDict(d)
189
        b.remove(['c', 'e', 'f', 'g'])
190
        r = {
191
            'a': 1,
192
            'b': 2,
193
        }
194
        self.assertEqual(b, r)
195
196
    def test_subset(self):
197
        d = {
198
            'a': 1,
199
            'b': 2,
200
            'c': '4',
201
            'e': '5',
202
            'f': 6,
203
            'g': 7,
204
        }
205
        b = UtilityDict(d)
206
        f = b.subset(['c', 'f', 'x'])
207
        r = {
208
            'c': '4',
209
            'f': 6,
210
            'x': None,
211
        }
212
        self.assertEqual(f, r)
213
        self.assertFalse(f is b)
214