|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
from benedict.utils import dict_util as u |
|
4
|
|
|
from decimal import Decimal |
|
5
|
|
|
|
|
6
|
|
|
import datetime as dt |
|
7
|
|
|
import unittest |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class dict_util_test_case(unittest.TestCase): |
|
11
|
|
|
|
|
12
|
|
|
def test_clean(self): |
|
13
|
|
|
i = { |
|
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
|
|
|
o = i.copy() |
|
25
|
|
|
u.clean(o) |
|
26
|
|
|
r = { |
|
27
|
|
|
'b': { 'x': 1 }, |
|
28
|
|
|
'd': [0, 1], |
|
29
|
|
|
'e': 0.0, |
|
30
|
|
|
'h': '0', |
|
31
|
|
|
} |
|
32
|
|
|
self.assertEqual(o, r) |
|
33
|
|
|
|
|
34
|
|
|
o = i.copy() |
|
35
|
|
|
u.clean(o, 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(o, r) |
|
44
|
|
|
|
|
45
|
|
|
o = i.copy() |
|
46
|
|
|
u.clean(o, 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(o, r) |
|
55
|
|
|
|
|
56
|
|
|
o = i.copy() |
|
57
|
|
|
u.clean(o, 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(o, r) |
|
66
|
|
|
|
|
67
|
|
|
def test_clone(self): |
|
68
|
|
|
i = { |
|
69
|
|
|
'a': { |
|
70
|
|
|
'b': { |
|
71
|
|
|
'c': 1 |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
o = u.clone(i) |
|
76
|
|
|
self.assertEqual(type(i), type(o)) |
|
77
|
|
|
self.assertEqual(i, o) |
|
78
|
|
|
self.assertFalse(i is o) |
|
79
|
|
|
o['a']['b']['c'] = 2 |
|
80
|
|
|
self.assertEqual(i['a']['b']['c'], 1) |
|
81
|
|
|
self.assertEqual(o['a']['b']['c'], 2) |
|
82
|
|
|
|
|
83
|
|
|
def test_dump(self): |
|
84
|
|
|
d = { |
|
85
|
|
|
'a': { |
|
86
|
|
|
'b': { |
|
87
|
|
|
'c': 1 |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
r = """{ |
|
92
|
|
|
"a": { |
|
93
|
|
|
"b": { |
|
94
|
|
|
"c": 1 |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
}""" |
|
98
|
|
|
o = u.dump(d) |
|
99
|
|
|
self.assertEqual(o, r) |
|
100
|
|
|
|
|
101
|
|
|
def test_dump_with_datetime(self): |
|
102
|
|
|
d = { |
|
103
|
|
|
'datetime': dt.datetime(2019, 6, 11), |
|
104
|
|
|
} |
|
105
|
|
|
r = """{ |
|
106
|
|
|
"datetime": "2019-06-11 00:00:00" |
|
107
|
|
|
}""" |
|
108
|
|
|
o = u.dump(d) |
|
109
|
|
|
self.assertEqual(o, r) |
|
110
|
|
|
|
|
111
|
|
|
def test_dump_with_decimal(self): |
|
112
|
|
|
d = { |
|
113
|
|
|
'decimal': Decimal('1.75'), |
|
114
|
|
|
} |
|
115
|
|
|
r = """{ |
|
116
|
|
|
"decimal": "1.75" |
|
117
|
|
|
}""" |
|
118
|
|
|
o = u.dump(d) |
|
119
|
|
|
self.assertEqual(o, r) |
|
120
|
|
|
|
|
121
|
|
|
def test_filter(self): |
|
122
|
|
|
i = { |
|
123
|
|
|
'a': 1, |
|
124
|
|
|
'b': 2, |
|
125
|
|
|
'c': '4', |
|
126
|
|
|
'e': '5', |
|
127
|
|
|
'f': 6, |
|
128
|
|
|
'g': 7, |
|
129
|
|
|
} |
|
130
|
|
|
with self.assertRaises(ValueError): |
|
131
|
|
|
f = u.filter(i, True) |
|
132
|
|
|
o = u.filter(i, lambda key, val: isinstance(val, int)) |
|
133
|
|
|
r = { |
|
134
|
|
|
'a': 1, |
|
135
|
|
|
'b': 2, |
|
136
|
|
|
'f': 6, |
|
137
|
|
|
'g': 7, |
|
138
|
|
|
} |
|
139
|
|
|
self.assertFalse(i is o) |
|
140
|
|
|
self.assertEqual(o, r) |
|
141
|
|
|
|
|
142
|
|
|
def test_flatten(self): |
|
143
|
|
|
i = { |
|
144
|
|
|
'a': 1, |
|
145
|
|
|
'b': 2, |
|
146
|
|
|
'c': { |
|
147
|
|
|
'd': { |
|
148
|
|
|
'e': 3, |
|
149
|
|
|
'f': 4, |
|
150
|
|
|
'g': { |
|
151
|
|
|
'h': 5, |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
}, |
|
155
|
|
|
} |
|
156
|
|
|
o = u.flatten(i) |
|
157
|
|
|
r = { |
|
158
|
|
|
'a': 1, |
|
159
|
|
|
'b': 2, |
|
160
|
|
|
'c_d_e': 3, |
|
161
|
|
|
'c_d_f': 4, |
|
162
|
|
|
'c_d_g_h': 5, |
|
163
|
|
|
} |
|
164
|
|
|
self.assertEqual(o, r) |
|
165
|
|
|
|
|
166
|
|
|
def test_flatten_with_custom_separator(self): |
|
167
|
|
|
i = { |
|
168
|
|
|
'a': 1, |
|
169
|
|
|
'b': 2, |
|
170
|
|
|
'c': { |
|
171
|
|
|
'd': { |
|
172
|
|
|
'e': 3, |
|
173
|
|
|
'f': 4, |
|
174
|
|
|
'g': { |
|
175
|
|
|
'h': 5, |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
}, |
|
179
|
|
|
} |
|
180
|
|
|
o = u.flatten(i, separator='/') |
|
181
|
|
|
r = { |
|
182
|
|
|
'a': 1, |
|
183
|
|
|
'b': 2, |
|
184
|
|
|
'c/d/e': 3, |
|
185
|
|
|
'c/d/f': 4, |
|
186
|
|
|
'c/d/g/h': 5, |
|
187
|
|
|
} |
|
188
|
|
|
self.assertEqual(o, r) |
|
189
|
|
|
|
|
190
|
|
|
def test_flatten_with_key_conflict(self): |
|
191
|
|
|
i = { |
|
192
|
|
|
'a': 1, |
|
193
|
|
|
'b': 2, |
|
194
|
|
|
'c': { |
|
195
|
|
|
'd': 3, |
|
196
|
|
|
}, |
|
197
|
|
|
'c_d': 4, |
|
198
|
|
|
'd_e': 5, |
|
199
|
|
|
'd': { |
|
200
|
|
|
'e': 6, |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
o = u.flatten(i) |
|
204
|
|
|
r = { |
|
205
|
|
|
'a': 1, |
|
206
|
|
|
'b': 2, |
|
207
|
|
|
'c_d': 4, |
|
208
|
|
|
'd_e': 5, |
|
209
|
|
|
} |
|
210
|
|
|
self.assertEqual(o, r) |
|
211
|
|
|
|
|
212
|
|
|
def test_invert_with_unique_values(self): |
|
213
|
|
|
i = { |
|
214
|
|
|
'a': 1, |
|
215
|
|
|
'b': 2, |
|
216
|
|
|
'c': 3, |
|
217
|
|
|
'd': 4, |
|
218
|
|
|
'e': 5, |
|
219
|
|
|
} |
|
220
|
|
|
o = u.invert(i) |
|
221
|
|
|
r = { |
|
222
|
|
|
1: ['a'], |
|
223
|
|
|
2: ['b'], |
|
224
|
|
|
3: ['c'], |
|
225
|
|
|
4: ['d'], |
|
226
|
|
|
5: ['e'], |
|
227
|
|
|
} |
|
228
|
|
|
self.assertEqual(o, r) |
|
229
|
|
|
|
|
230
|
|
|
def test_invert_with_flat_unique_values(self): |
|
231
|
|
|
i = { |
|
232
|
|
|
'a': 1, |
|
233
|
|
|
'b': 2, |
|
234
|
|
|
'c': 3, |
|
235
|
|
|
'd': 4, |
|
236
|
|
|
'e': 5, |
|
237
|
|
|
} |
|
238
|
|
|
o = u.invert(i, flat=True) |
|
239
|
|
|
r = { |
|
240
|
|
|
1: 'a', |
|
241
|
|
|
2: 'b', |
|
242
|
|
|
3: 'c', |
|
243
|
|
|
4: 'd', |
|
244
|
|
|
5: 'e', |
|
245
|
|
|
} |
|
246
|
|
|
self.assertEqual(o, r) |
|
247
|
|
|
|
|
248
|
|
|
def test_invert_with_multiple_values(self): |
|
249
|
|
|
i = { |
|
250
|
|
|
'a': 1, |
|
251
|
|
|
'b': 2, |
|
252
|
|
|
'c': 3, |
|
253
|
|
|
'd': 1, |
|
254
|
|
|
'e': 2, |
|
255
|
|
|
'f': 3, |
|
256
|
|
|
} |
|
257
|
|
|
o = u.invert(i) |
|
258
|
|
|
self.assertTrue('a' and 'd' in o[1]) |
|
259
|
|
|
self.assertTrue('b' and 'e' in o[2]) |
|
260
|
|
|
self.assertTrue('c' and 'f' in o[3]) |
|
261
|
|
|
|
|
262
|
|
|
def test_items_sorted_by_keys(self): |
|
263
|
|
|
i = { |
|
264
|
|
|
'y': 3, |
|
265
|
|
|
'a': 6, |
|
266
|
|
|
'f': 9, |
|
267
|
|
|
'z': 4, |
|
268
|
|
|
'x': 1, |
|
269
|
|
|
} |
|
270
|
|
|
o = u.items_sorted_by_keys(i) |
|
271
|
|
|
r = [ |
|
272
|
|
|
('a', 6,), |
|
273
|
|
|
('f', 9,), |
|
274
|
|
|
('x', 1,), |
|
275
|
|
|
('y', 3,), |
|
276
|
|
|
('z', 4,), |
|
277
|
|
|
] |
|
278
|
|
|
self.assertEqual(o, r) |
|
279
|
|
|
|
|
280
|
|
|
def test_items_sorted_by_keys_reverse(self): |
|
281
|
|
|
i = { |
|
282
|
|
|
'y': 3, |
|
283
|
|
|
'a': 6, |
|
284
|
|
|
'f': 9, |
|
285
|
|
|
'z': 4, |
|
286
|
|
|
'x': 1, |
|
287
|
|
|
} |
|
288
|
|
|
o = u.items_sorted_by_keys(i, reverse=True) |
|
289
|
|
|
r = [ |
|
290
|
|
|
('z', 4,), |
|
291
|
|
|
('y', 3,), |
|
292
|
|
|
('x', 1,), |
|
293
|
|
|
('f', 9,), |
|
294
|
|
|
('a', 6,), |
|
295
|
|
|
] |
|
296
|
|
|
self.assertEqual(o, r) |
|
297
|
|
|
|
|
298
|
|
|
def test_items_sorted_by_values(self): |
|
299
|
|
|
i = { |
|
300
|
|
|
'a': 3, |
|
301
|
|
|
'b': 6, |
|
302
|
|
|
'c': 9, |
|
303
|
|
|
'e': 4, |
|
304
|
|
|
'd': 1, |
|
305
|
|
|
} |
|
306
|
|
|
o = u.items_sorted_by_values(i) |
|
307
|
|
|
r = [ |
|
308
|
|
|
('d', 1,), |
|
309
|
|
|
('a', 3,), |
|
310
|
|
|
('e', 4,), |
|
311
|
|
|
('b', 6,), |
|
312
|
|
|
('c', 9,), |
|
313
|
|
|
] |
|
314
|
|
|
self.assertEqual(o, r) |
|
315
|
|
|
|
|
316
|
|
|
def test_items_sorted_by_values_reverse(self): |
|
317
|
|
|
i = { |
|
318
|
|
|
'a': 3, |
|
319
|
|
|
'b': 6, |
|
320
|
|
|
'c': 9, |
|
321
|
|
|
'e': 4, |
|
322
|
|
|
'd': 1, |
|
323
|
|
|
} |
|
324
|
|
|
o = u.items_sorted_by_values(i, reverse=True) |
|
325
|
|
|
r = [ |
|
326
|
|
|
('c', 9,), |
|
327
|
|
|
('b', 6,), |
|
328
|
|
|
('e', 4,), |
|
329
|
|
|
('a', 3,), |
|
330
|
|
|
('d', 1,), |
|
331
|
|
|
] |
|
332
|
|
|
self.assertEqual(o, r) |
|
333
|
|
|
|
|
334
|
|
|
def test_keypaths(self): |
|
335
|
|
|
i = { |
|
336
|
|
|
'a': 1, |
|
337
|
|
|
'b': { |
|
338
|
|
|
'c': { |
|
339
|
|
|
'x': 2, |
|
340
|
|
|
'y': 3, |
|
341
|
|
|
}, |
|
342
|
|
|
'd': { |
|
343
|
|
|
'x': 4, |
|
344
|
|
|
'y': 5, |
|
345
|
|
|
}, |
|
346
|
|
|
}, |
|
347
|
|
|
} |
|
348
|
|
|
o = u.keypaths(i) |
|
349
|
|
|
r = [ |
|
350
|
|
|
'a', |
|
351
|
|
|
'b', |
|
352
|
|
|
'b.c', |
|
353
|
|
|
'b.c.x', |
|
354
|
|
|
'b.c.y', |
|
355
|
|
|
'b.d', |
|
356
|
|
|
'b.d.x', |
|
357
|
|
|
'b.d.y', |
|
358
|
|
|
] |
|
359
|
|
|
self.assertEqual(o, r) |
|
360
|
|
|
|
|
361
|
|
|
def test_keypaths_with_custom_separator(self): |
|
362
|
|
|
i = { |
|
363
|
|
|
'a': 1, |
|
364
|
|
|
'b': { |
|
365
|
|
|
'c': { |
|
366
|
|
|
'x': 2, |
|
367
|
|
|
'y': 3, |
|
368
|
|
|
}, |
|
369
|
|
|
'd': { |
|
370
|
|
|
'x': 4, |
|
371
|
|
|
'y': 5, |
|
372
|
|
|
}, |
|
373
|
|
|
}, |
|
374
|
|
|
} |
|
375
|
|
|
o = u.keypaths(i, separator='/') |
|
376
|
|
|
r = [ |
|
377
|
|
|
'a', |
|
378
|
|
|
'b', |
|
379
|
|
|
'b/c', |
|
380
|
|
|
'b/c/x', |
|
381
|
|
|
'b/c/y', |
|
382
|
|
|
'b/d', |
|
383
|
|
|
'b/d/x', |
|
384
|
|
|
'b/d/y', |
|
385
|
|
|
] |
|
386
|
|
|
self.assertEqual(o, r) |
|
387
|
|
|
|
|
388
|
|
|
def test_keypaths_without_separator(self): |
|
389
|
|
|
i = { |
|
390
|
|
|
'a': 1, |
|
391
|
|
|
'b': { |
|
392
|
|
|
'c': { |
|
393
|
|
|
'x': 2, |
|
394
|
|
|
'y': 3, |
|
395
|
|
|
}, |
|
396
|
|
|
'd': { |
|
397
|
|
|
'x': 4, |
|
398
|
|
|
'y': 5, |
|
399
|
|
|
}, |
|
400
|
|
|
}, |
|
401
|
|
|
} |
|
402
|
|
|
with self.assertRaises(ValueError): |
|
403
|
|
|
o = u.keypaths(i, separator=None) |
|
404
|
|
|
|
|
405
|
|
|
def test_keypaths_with_non_string_keys(self): |
|
406
|
|
|
i = { |
|
407
|
|
|
True: { |
|
408
|
|
|
True: 1, |
|
409
|
|
|
}, |
|
410
|
|
|
False: { |
|
411
|
|
|
False: 1, |
|
412
|
|
|
}, |
|
413
|
|
|
None: { |
|
414
|
|
|
None: 1, |
|
415
|
|
|
}, |
|
416
|
|
|
} |
|
417
|
|
|
o = u.keypaths(i) |
|
418
|
|
|
r = [ |
|
419
|
|
|
'False', |
|
420
|
|
|
'False.False', |
|
421
|
|
|
'None', |
|
422
|
|
|
'None.None', |
|
423
|
|
|
'True', |
|
424
|
|
|
'True.True', |
|
425
|
|
|
] |
|
426
|
|
|
self.assertEqual(o, r) |
|
427
|
|
|
|
|
428
|
|
|
def test_merge_with_flatten_dict(self): |
|
429
|
|
|
d = { |
|
430
|
|
|
'a': 1, |
|
431
|
|
|
'b': 1, |
|
432
|
|
|
} |
|
433
|
|
|
m = { |
|
434
|
|
|
'b': 2, |
|
435
|
|
|
'c': 3, |
|
436
|
|
|
} |
|
437
|
|
|
u.merge(d, m) |
|
438
|
|
|
r = { |
|
439
|
|
|
'a': 1, |
|
440
|
|
|
'b': 2, |
|
441
|
|
|
'c': 3, |
|
442
|
|
|
} |
|
443
|
|
|
self.assertEqual(d, r) |
|
444
|
|
|
|
|
445
|
|
|
def test_merge_with_multiple_dicts(self): |
|
446
|
|
|
d = { |
|
447
|
|
|
'a': 1, |
|
448
|
|
|
'b': 1, |
|
449
|
|
|
} |
|
450
|
|
|
a = { |
|
451
|
|
|
'b': 2, |
|
452
|
|
|
'c': 3, |
|
453
|
|
|
'd': 3, |
|
454
|
|
|
} |
|
455
|
|
|
b = { |
|
456
|
|
|
'd': 5, |
|
457
|
|
|
'e': 5, |
|
458
|
|
|
} |
|
459
|
|
|
c = { |
|
460
|
|
|
'd': 4, |
|
461
|
|
|
'f': 6, |
|
462
|
|
|
} |
|
463
|
|
|
u.merge(d, a, b, c) |
|
464
|
|
|
r = { |
|
465
|
|
|
'a': 1, |
|
466
|
|
|
'b': 2, |
|
467
|
|
|
'c': 3, |
|
468
|
|
|
'd': 4, |
|
469
|
|
|
'e': 5, |
|
470
|
|
|
'f': 6, |
|
471
|
|
|
} |
|
472
|
|
|
self.assertEqual(d, r) |
|
473
|
|
|
|
|
474
|
|
|
def test_merge_with_nested_dict(self): |
|
475
|
|
|
d = { |
|
476
|
|
|
'a': 1, |
|
477
|
|
|
'b': { |
|
478
|
|
|
'c': { |
|
479
|
|
|
'x': 2, |
|
480
|
|
|
'y': 3, |
|
481
|
|
|
}, |
|
482
|
|
|
'd': { |
|
483
|
|
|
'x': 4, |
|
484
|
|
|
'y': 5, |
|
485
|
|
|
}, |
|
486
|
|
|
'e': { |
|
487
|
|
|
'x': 6, |
|
488
|
|
|
'y': 7, |
|
489
|
|
|
}, |
|
490
|
|
|
}, |
|
491
|
|
|
} |
|
492
|
|
|
m = { |
|
493
|
|
|
'a': 0, |
|
494
|
|
|
'b': { |
|
495
|
|
|
'c': 1, |
|
496
|
|
|
'd': { |
|
497
|
|
|
'y': 1, |
|
498
|
|
|
'z': 2, |
|
499
|
|
|
}, |
|
500
|
|
|
'e': { |
|
501
|
|
|
'f': { |
|
502
|
|
|
'x': 2, |
|
503
|
|
|
'y': 3, |
|
504
|
|
|
}, |
|
505
|
|
|
'g': { |
|
506
|
|
|
'x': 4, |
|
507
|
|
|
'y': 5, |
|
508
|
|
|
}, |
|
509
|
|
|
}, |
|
510
|
|
|
}, |
|
511
|
|
|
} |
|
512
|
|
|
u.merge(d, m) |
|
513
|
|
|
r = { |
|
514
|
|
|
'a': 0, |
|
515
|
|
|
'b': { |
|
516
|
|
|
'c': 1, |
|
517
|
|
|
'd': { |
|
518
|
|
|
'x': 4, |
|
519
|
|
|
'y': 1, |
|
520
|
|
|
'z': 2, |
|
521
|
|
|
}, |
|
522
|
|
|
'e': { |
|
523
|
|
|
'f': { |
|
524
|
|
|
'x': 2, |
|
525
|
|
|
'y': 3, |
|
526
|
|
|
}, |
|
527
|
|
|
'g': { |
|
528
|
|
|
'x': 4, |
|
529
|
|
|
'y': 5, |
|
530
|
|
|
}, |
|
531
|
|
|
'x': 6, |
|
532
|
|
|
'y': 7, |
|
533
|
|
|
}, |
|
534
|
|
|
}, |
|
535
|
|
|
} |
|
536
|
|
|
self.assertEqual(d, r) |
|
537
|
|
|
|
|
538
|
|
|
def test_move(self): |
|
539
|
|
|
d = { |
|
540
|
|
|
'a': { |
|
541
|
|
|
'x': 1, |
|
542
|
|
|
'y': 1, |
|
543
|
|
|
}, |
|
544
|
|
|
'b': { |
|
545
|
|
|
'x': 2, |
|
546
|
|
|
'y': 2, |
|
547
|
|
|
}, |
|
548
|
|
|
'c': { |
|
549
|
|
|
'x': 3, |
|
550
|
|
|
'y': 3, |
|
551
|
|
|
}, |
|
552
|
|
|
} |
|
553
|
|
|
u.move(d, 'a', 'd') |
|
554
|
|
|
r = { |
|
555
|
|
|
'b': { |
|
556
|
|
|
'x': 2, |
|
557
|
|
|
'y': 2, |
|
558
|
|
|
}, |
|
559
|
|
|
'c': { |
|
560
|
|
|
'x': 3, |
|
561
|
|
|
'y': 3, |
|
562
|
|
|
}, |
|
563
|
|
|
'd': { |
|
564
|
|
|
'x': 1, |
|
565
|
|
|
'y': 1, |
|
566
|
|
|
}, |
|
567
|
|
|
} |
|
568
|
|
|
self.assertEqual(d, r) |
|
569
|
|
|
|
|
570
|
|
|
def test_move_with_same_key(self): |
|
571
|
|
|
d = { |
|
572
|
|
|
'a': 1, |
|
573
|
|
|
'b': 2, |
|
574
|
|
|
} |
|
575
|
|
|
u.move(d, 'a', 'a') |
|
576
|
|
|
r = { |
|
577
|
|
|
'a': 1, |
|
578
|
|
|
'b': 2, |
|
579
|
|
|
} |
|
580
|
|
|
self.assertEqual(d, r) |
|
581
|
|
|
|
|
582
|
|
|
def test_remove_with_single_key(self): |
|
583
|
|
|
d = { |
|
584
|
|
|
'a': 1, |
|
585
|
|
|
'b': 2, |
|
586
|
|
|
'c': 3, |
|
587
|
|
|
} |
|
588
|
|
|
u.remove(d, 'c') |
|
589
|
|
|
r = { |
|
590
|
|
|
'a': 1, |
|
591
|
|
|
'b': 2, |
|
592
|
|
|
} |
|
593
|
|
|
self.assertEqual(d, r) |
|
594
|
|
|
|
|
595
|
|
|
def test_remove_with_multiple_keys_as_args(self): |
|
596
|
|
|
d = { |
|
597
|
|
|
'a': 1, |
|
598
|
|
|
'b': 2, |
|
599
|
|
|
'c': 3, |
|
600
|
|
|
'd': 4, |
|
601
|
|
|
'e': 5, |
|
602
|
|
|
} |
|
603
|
|
|
u.remove(d, 'c', 'd', 'e') |
|
604
|
|
|
r = { |
|
605
|
|
|
'a': 1, |
|
606
|
|
|
'b': 2, |
|
607
|
|
|
} |
|
608
|
|
|
self.assertEqual(d, r) |
|
609
|
|
|
|
|
610
|
|
|
def test_remove_with_multiple_keys_as_list(self): |
|
611
|
|
|
d = { |
|
612
|
|
|
'a': 1, |
|
613
|
|
|
'b': 2, |
|
614
|
|
|
'c': 3, |
|
615
|
|
|
'd': 4, |
|
616
|
|
|
'e': 5, |
|
617
|
|
|
} |
|
618
|
|
|
u.remove(d, ['c', 'd', 'e']) |
|
619
|
|
|
r = { |
|
620
|
|
|
'a': 1, |
|
621
|
|
|
'b': 2, |
|
622
|
|
|
} |
|
623
|
|
|
self.assertEqual(d, r) |
|
624
|
|
|
|
|
625
|
|
|
def test_standardize(self): |
|
626
|
|
|
d = { |
|
627
|
|
|
'CamelCase': 1, |
|
628
|
|
|
'CamelCamelCase': 1, |
|
629
|
|
|
'Camel2Camel2Case': 1, |
|
630
|
|
|
'getHTTPResponseCode': 1, |
|
631
|
|
|
'get2HTTPResponseCode': 1, |
|
632
|
|
|
'HTTPResponseCode': 1, |
|
633
|
|
|
'HTTPResponseCodeXYZ': 1, |
|
634
|
|
|
' LocationCoordinates ': { |
|
635
|
|
|
'Lat. ': 0.0, |
|
636
|
|
|
'Lng. ': 0.0, |
|
637
|
|
|
}, |
|
638
|
|
|
} |
|
639
|
|
|
u.standardize(d) |
|
640
|
|
|
r = { |
|
641
|
|
|
'camel_case': 1, |
|
642
|
|
|
'camel_camel_case': 1, |
|
643
|
|
|
'camel2_camel2_case': 1, |
|
644
|
|
|
'get_http_response_code': 1, |
|
645
|
|
|
'get2_http_response_code': 1, |
|
646
|
|
|
'http_response_code': 1, |
|
647
|
|
|
'http_response_code_xyz': 1, |
|
648
|
|
|
'location_coordinates': { |
|
649
|
|
|
'lat': 0.0, |
|
650
|
|
|
'lng': 0.0, |
|
651
|
|
|
}, |
|
652
|
|
|
} |
|
653
|
|
|
# print(u.dump(d)) |
|
654
|
|
|
# print(u.dump(r)) |
|
655
|
|
|
self.assertEqual(d, r) |
|
656
|
|
|
|
|
657
|
|
|
def test_subset_with_keys_as_args(self): |
|
658
|
|
|
i = { |
|
659
|
|
|
'a': 1, |
|
660
|
|
|
'b': 2, |
|
661
|
|
|
'c': 3, |
|
662
|
|
|
'd': 4, |
|
663
|
|
|
'e': 5, |
|
664
|
|
|
'f': 6, |
|
665
|
|
|
} |
|
666
|
|
|
o = u.subset(i, 'c', 'f', 'x') |
|
667
|
|
|
r = { |
|
668
|
|
|
'c': 3, |
|
669
|
|
|
'f': 6, |
|
670
|
|
|
'x': None, |
|
671
|
|
|
} |
|
672
|
|
|
self.assertFalse(i is o) |
|
673
|
|
|
self.assertEqual(o, r) |
|
674
|
|
|
|
|
675
|
|
|
def test_subset_with_keys_as_list(self): |
|
676
|
|
|
i = { |
|
677
|
|
|
'a': 1, |
|
678
|
|
|
'b': 2, |
|
679
|
|
|
'c': 3, |
|
680
|
|
|
'd': 4, |
|
681
|
|
|
'e': 5, |
|
682
|
|
|
'f': 6, |
|
683
|
|
|
} |
|
684
|
|
|
o = u.subset(i, ['c', 'f', 'x']) |
|
685
|
|
|
r = { |
|
686
|
|
|
'c': 3, |
|
687
|
|
|
'f': 6, |
|
688
|
|
|
'x': None, |
|
689
|
|
|
} |
|
690
|
|
|
self.assertFalse(i is o) |
|
691
|
|
|
self.assertEqual(o, r) |
|
692
|
|
|
|
|
693
|
|
|
def test_swap(self): |
|
694
|
|
|
d = { |
|
695
|
|
|
'a': 1, |
|
696
|
|
|
'b': 2, |
|
697
|
|
|
'c': 3, |
|
698
|
|
|
} |
|
699
|
|
|
u.swap(d, 'a', 'b') |
|
700
|
|
|
r = { |
|
701
|
|
|
'a': 2, |
|
702
|
|
|
'b': 1, |
|
703
|
|
|
'c': 3, |
|
704
|
|
|
} |
|
705
|
|
|
self.assertEqual(d, r) |
|
706
|
|
|
|
|
707
|
|
|
def test_swap_with_same_key(self): |
|
708
|
|
|
d = { |
|
709
|
|
|
'a': 1, |
|
710
|
|
|
'b': 2, |
|
711
|
|
|
} |
|
712
|
|
|
u.swap(d, 'a', 'a') |
|
713
|
|
|
r = { |
|
714
|
|
|
'a': 1, |
|
715
|
|
|
'b': 2, |
|
716
|
|
|
} |
|
717
|
|
|
self.assertEqual(d, r) |
|
718
|
|
|
|
|
719
|
|
|
def test_swap_with_invalid_key(self): |
|
720
|
|
|
d = { |
|
721
|
|
|
'a': 1, |
|
722
|
|
|
'b': 2, |
|
723
|
|
|
'c': 3, |
|
724
|
|
|
} |
|
725
|
|
|
with self.assertRaises(KeyError): |
|
726
|
|
|
u.swap(d, 'a', 'd') |
|
727
|
|
|
|
|
728
|
|
|
def test_traverse(self): |
|
729
|
|
|
i = { |
|
730
|
|
|
'a': { |
|
731
|
|
|
'x': 2, |
|
732
|
|
|
'y': 3, |
|
733
|
|
|
'z': { |
|
734
|
|
|
'ok': 5, |
|
735
|
|
|
} |
|
736
|
|
|
}, |
|
737
|
|
|
'b': { |
|
738
|
|
|
'x': 7, |
|
739
|
|
|
'y': 11, |
|
740
|
|
|
'z': { |
|
741
|
|
|
'ok': 13, |
|
742
|
|
|
} |
|
743
|
|
|
}, |
|
744
|
|
|
'c': { |
|
745
|
|
|
'x': 17, |
|
746
|
|
|
'y': 19, |
|
747
|
|
|
'z': { |
|
748
|
|
|
'ok': 23, |
|
749
|
|
|
} |
|
750
|
|
|
}, |
|
751
|
|
|
} |
|
752
|
|
|
o = u.clone(i) |
|
753
|
|
|
with self.assertRaises(ValueError): |
|
754
|
|
|
u.traverse(o, True) |
|
755
|
|
|
def f(parent, key, value): |
|
756
|
|
|
if not isinstance(value, dict): |
|
757
|
|
|
parent[key] = (value + 1) |
|
758
|
|
|
u.traverse(o, f) |
|
759
|
|
|
r = { |
|
760
|
|
|
'a': { |
|
761
|
|
|
'x': 3, |
|
762
|
|
|
'y': 4, |
|
763
|
|
|
'z': { |
|
764
|
|
|
'ok': 6, |
|
765
|
|
|
} |
|
766
|
|
|
}, |
|
767
|
|
|
'b': { |
|
768
|
|
|
'x': 8, |
|
769
|
|
|
'y': 12, |
|
770
|
|
|
'z': { |
|
771
|
|
|
'ok': 14, |
|
772
|
|
|
} |
|
773
|
|
|
}, |
|
774
|
|
|
'c': { |
|
775
|
|
|
'x': 18, |
|
776
|
|
|
'y': 20, |
|
777
|
|
|
'z': { |
|
778
|
|
|
'ok': 24, |
|
779
|
|
|
} |
|
780
|
|
|
}, |
|
781
|
|
|
} |
|
782
|
|
|
self.assertEqual(o, r) |
|
783
|
|
|
|
|
784
|
|
|
def test_unique(self): |
|
785
|
|
|
d = { |
|
786
|
|
|
'a': { |
|
787
|
|
|
'x': 1, |
|
788
|
|
|
'y': 1, |
|
789
|
|
|
}, |
|
790
|
|
|
'b': { |
|
791
|
|
|
'x': 2, |
|
792
|
|
|
'y': 2, |
|
793
|
|
|
}, |
|
794
|
|
|
'c': { |
|
795
|
|
|
'x': 1, |
|
796
|
|
|
'y': 1, |
|
797
|
|
|
}, |
|
798
|
|
|
'd': { |
|
799
|
|
|
'x': 1, |
|
800
|
|
|
}, |
|
801
|
|
|
'e': { |
|
802
|
|
|
'x': 1, |
|
803
|
|
|
'y': 1, |
|
804
|
|
|
'z': 1, |
|
805
|
|
|
}, |
|
806
|
|
|
'f': { |
|
807
|
|
|
'x': 2, |
|
808
|
|
|
'y': 2, |
|
809
|
|
|
}, |
|
810
|
|
|
} |
|
811
|
|
|
u.unique(d) |
|
812
|
|
|
rv = [ |
|
813
|
|
|
{ |
|
814
|
|
|
'x': 1, |
|
815
|
|
|
'y': 1, |
|
816
|
|
|
}, |
|
817
|
|
|
{ |
|
818
|
|
|
'x': 2, |
|
819
|
|
|
'y': 2, |
|
820
|
|
|
}, |
|
821
|
|
|
{ |
|
822
|
|
|
'x': 1, |
|
823
|
|
|
}, |
|
824
|
|
|
{ |
|
825
|
|
|
'x': 1, |
|
826
|
|
|
'y': 1, |
|
827
|
|
|
'z': 1, |
|
828
|
|
|
}, |
|
829
|
|
|
] |
|
830
|
|
|
self.assertEqual(len(d.keys()), len(rv)) |
|
831
|
|
|
self.assertTrue(all([value in rv for value in d.values()])) |
|
832
|
|
|
|