File: test_munch.py

package info (click to toggle)
python-munch 4.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 264 kB
  • sloc: python: 761; makefile: 20
file content (582 lines) | stat: -rw-r--r-- 16,571 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# pylint: disable=unnecessary-lambda
import json
import pickle
from collections import namedtuple

import pytest
from munch import DefaultFactoryMunch, AutoMunch, DefaultMunch, Munch, munchify, unmunchify


def test_base():
    b = Munch()
    b.hello = 'world'
    assert b.hello == 'world'
    b['hello'] += "!"
    assert b.hello == 'world!'
    b.foo = Munch(lol=True)
    assert b.foo.lol is True
    assert b.foo is b['foo']

    assert sorted(b.keys()) == ['foo', 'hello']

    b.update({'ponies': 'are pretty!'}, hello=42)
    assert b == Munch({'ponies': 'are pretty!', 'foo': Munch({'lol': True}), 'hello': 42})

    assert sorted([(k, b[k]) for k in b]) == [('foo', Munch({'lol': True})), ('hello', 42), ('ponies', 'are pretty!')]

    format_munch = Munch(knights='lolcats', ni='can haz')
    assert "The {knights} who say {ni}!".format(**format_munch) == 'The lolcats who say can haz!'


def test_contains():
    b = Munch(ponies='are pretty!')
    assert 'ponies' in b
    assert ('foo' in b) is False

    b['foo'] = 42
    assert 'foo' in b

    b.hello = 'hai'
    assert 'hello' in b

    b[None] = 123
    assert None in b

    b[False] = 456
    assert False in b


def test_getattr():
    b = Munch(bar='baz', lol={})

    with pytest.raises(AttributeError):
        b.foo  # pylint: disable=pointless-statement

    assert b.bar == 'baz'
    assert getattr(b, 'bar') == 'baz'
    assert b['bar'] == 'baz'
    assert b.lol is b['lol']
    assert b.lol is getattr(b, 'lol')


def test_setattr():
    b = Munch(foo='bar', this_is='useful when subclassing')
    assert hasattr(b.values, '__call__')

    b.values = 'uh oh'
    assert b.values == 'uh oh'

    with pytest.raises(KeyError):
        b['values']  # pylint: disable=pointless-statement


def test_pickle():
    b = DefaultMunch.fromDict({"a": "b"})
    assert pickle.loads(pickle.dumps(b)) == b

def test_automunch():
    b = AutoMunch()
    b.urmom = {'sez': {'what': 'what'}}
    assert b.urmom.sez.what == 'what'  # pylint: disable=no-member


def test_delattr():
    b = Munch(lol=42)
    del b.lol

    with pytest.raises(KeyError):
        b['lol']  # pylint: disable=pointless-statement

    with pytest.raises(AttributeError):
        b.lol  # pylint: disable=pointless-statement


def test_toDict():
    b = Munch(foo=Munch(lol=True), hello=42, ponies='are pretty!')
    assert sorted(b.toDict().items()) == [('foo', {'lol': True}), ('hello', 42), ('ponies', 'are pretty!')]

def test_dict_property():
    b = Munch(foo=Munch(lol=True), hello=42, ponies='are pretty!')
    assert sorted(b.__dict__.items()) == [('foo', {'lol': True}), ('hello', 42), ('ponies', 'are pretty!')]

def test_repr():
    b = Munch(foo=Munch(lol=True), hello=42, ponies='are pretty!')
    assert repr(b).startswith("Munch({'")
    assert "'ponies': 'are pretty!'" in repr(b)
    assert "'hello': 42" in repr(b)
    assert "'foo': Munch({'lol': True})" in repr(b)
    assert "'hello': 42" in repr(b)

    with_spaces = Munch({1: 2, 'a b': 9, 'c': Munch({'simple': 5})})
    assert repr(with_spaces).startswith("Munch({")
    assert "'a b': 9" in repr(with_spaces)
    assert "1: 2" in repr(with_spaces)
    assert "'c': Munch({'simple': 5})" in repr(with_spaces)

    assert eval(repr(with_spaces)) == Munch({'a b': 9, 1: 2, 'c': Munch({'simple': 5})})  # pylint: disable=eval-used


def test_dir():
    m = Munch(a=1, b=2)
    assert dir(m) == ['a', 'b']


def test_fromDict():
    b = Munch.fromDict({'urmom': {'sez': {'what': 'what'}}})
    assert b.urmom.sez.what == 'what'


def test_copy():
    m = Munch(urmom=Munch(sez=Munch(what='what')))
    c = m.copy()
    assert c is not m
    assert c.urmom is not m.urmom
    assert c.urmom.sez is not m.urmom.sez
    assert c.urmom.sez.what == 'what'
    assert c == m


def test_munchify():
    b = munchify({'urmom': {'sez': {'what': 'what'}}})
    assert b.urmom.sez.what == 'what'

    b = munchify({'lol': ('cats', {'hah': 'i win again'}), 'hello': [{'french': 'salut', 'german': 'hallo'}]})
    assert b.hello[0].french == 'salut'
    assert b.lol[1].hah == 'i win again'

def test_munchify_with_namedtuple():
    nt = namedtuple('nt', ['prop_a', 'prop_b'])

    b = munchify({'top': nt('in named tuple', 3)})
    assert b.top.prop_a == 'in named tuple'
    assert b.top.prop_b == 3

    b = munchify({'top': {'middle': nt(prop_a={'leaf': 'should be munchified'},
                                       prop_b={'leaf': 'should be munchified'})}})
    assert b.top.middle.prop_a.leaf == 'should be munchified'
    assert b.top.middle.prop_b.leaf == 'should be munchified'


def test_unmunchify():
    b = Munch(foo=Munch(lol=True), hello=42, ponies='are pretty!')
    assert sorted(unmunchify(b).items()) == [('foo', {'lol': True}), ('hello', 42), ('ponies', 'are pretty!')]

    b = Munch(foo=['bar', Munch(lol=True)], hello=42, ponies=('are pretty!', Munch(lies='are trouble!')))
    assert sorted(unmunchify(b).items()) == [('foo', ['bar', {'lol': True}]),
                                             ('hello', 42),
                                             ('ponies', ('are pretty!', {'lies': 'are trouble!'}))]


def test_unmunchify_namedtuple():
    nt = namedtuple('nt', ['prop_a', 'prop_b'])
    b = Munch(foo=Munch(lol=True), hello=nt(prop_a=42, prop_b='yop'), ponies='are pretty!')
    assert sorted(unmunchify(b).items()) == [('foo', {'lol': True}),
                                             ('hello', nt(prop_a=42, prop_b='yop')),
                                             ('ponies', 'are pretty!')]


def test_toJSON_and_fromJSON():
    # pylint: disable=unidiomatic-typecheck
    obj = Munch(foo=Munch(lol=True), hello=42, ponies='are pretty!')
    obj_json = obj.toJSON()
    assert json.dumps(obj) == obj_json
    new_obj = Munch.fromJSON(obj_json)
    assert type(obj) == Munch
    assert new_obj == obj

    default_value = object()
    dm_obj = DefaultMunch.fromJSON(obj_json, default_value)
    assert type(dm_obj) == DefaultMunch
    assert dm_obj == obj
    assert dm_obj['not_exist'] is default_value
    assert dm_obj.not_exist is default_value


@pytest.mark.parametrize("attrname", dir(Munch))
def test_reserved_attributes(attrname):
    # Make sure that the default attributes on the Munch instance are
    # accessible.

    taken_munch = Munch(**{attrname: 'abc123'})

    # Make sure that the attribute is determined as in the filled collection...
    assert attrname in taken_munch

    # ...and that it is available using key access...
    assert taken_munch[attrname] == 'abc123'

    # ...but that it is not available using attribute access.
    attr = getattr(taken_munch, attrname)
    assert attr != 'abc123'

    empty_munch = Munch()

    # Make sure that the attribute is not seen contained in the empty
    # collection...
    assert attrname not in empty_munch

    # ...and that the attr is of the correct original type.
    attr = getattr(empty_munch, attrname)
    if attrname == '__doc__':
        assert isinstance(attr, str)
    elif attrname in ('__hash__', '__weakref__'):
        assert attr is None
    elif attrname == '__module__':
        assert attr == 'munch'
    elif attrname == '__dict__':
        assert attr == {}
    elif attrname == '__static_attributes__':
        # Python 3.13: added __static_attributes__ attribute, populated by the
        # compiler, containing a tuple of names of attributes of this class
        # which are accessed through self.X from any function in its body.
        assert isinstance(attr, tuple)
    elif attrname == '__firstlineno__':
        # Python 3.13: added __firstlineno__ attribute, populated by the
        # compiler, containing the line number of the first line of the class definition
        assert isinstance(attr, int)
    else:
        assert callable(attr)


def test_getattr_default():
    b = DefaultMunch(bar='baz', lol={})
    assert b.foo is None
    assert b['foo'] is None

    assert b.bar == 'baz'
    assert getattr(b, 'bar') == 'baz'
    assert b['bar'] == 'baz'
    assert b.lol is b['lol']
    assert b.lol is getattr(b, 'lol')

    undefined = object()
    b = DefaultMunch(undefined, bar='baz', lol={})
    assert b.foo is undefined
    assert b['foo'] is undefined


def test_setattr_default():
    b = DefaultMunch(foo='bar', this_is='useful when subclassing')
    assert hasattr(b.values, '__call__')

    b.values = 'uh oh'
    assert b.values == 'uh oh'
    assert b['values'] is None

    assert b.__default__ is None
    assert '__default__' not in b


def test_delattr_default():
    b = DefaultMunch(lol=42)
    del b.lol

    assert b.lol is None
    assert b['lol'] is None


def test_pickle_default():
    b = DefaultMunch.fromDict({"a": "b"})
    assert pickle.loads(pickle.dumps(b)) == b


def test_fromDict_default():
    undefined = object()
    b = DefaultMunch.fromDict({'urmom': {'sez': {'what': 'what'}}}, undefined)
    assert b.urmom.sez.what == 'what'
    assert b.urmom.sez.foo is undefined


def test_copy_default():
    undefined = object()
    m = DefaultMunch.fromDict({'urmom': {'sez': {'what': 'what'}}}, undefined)
    c = m.copy()
    assert c is not m
    assert c.urmom is not m.urmom
    assert c.urmom.sez is not m.urmom.sez
    assert c.urmom.sez.what == 'what'
    assert c == m
    assert c.urmom.sez.foo is undefined
    assert c.urmom.sez.__undefined__ is undefined


def test_munchify_default():
    undefined = object()
    b = munchify(
        {'urmom': {'sez': {'what': 'what'}}},
        lambda d: DefaultMunch(undefined, d))
    assert b.urmom.sez.what == 'what'
    assert b.urdad is undefined
    assert b.urmom.sez.ni is undefined


def test_repr_default():
    b = DefaultMunch(foo=DefaultMunch(lol=True), ponies='are pretty!')
    assert repr(b).startswith("DefaultMunch(None, {'")
    assert "'ponies': 'are pretty!'" in repr(b)


def test_getattr_default_factory():
    b = DefaultFactoryMunch(lambda: None, bar='baz', lol={})
    assert b.foo is None
    assert b['foo'] is None

    assert b.bar == 'baz'
    assert getattr(b, 'bar') == 'baz'
    assert b['bar'] == 'baz'
    assert b.lol is b['lol']
    assert b.lol is getattr(b, 'lol')

    undefined = object()
    default = lambda: undefined
    b = DefaultFactoryMunch(default, bar='baz', lol={})
    assert b.foo is undefined
    assert b['foo'] is undefined

    default = lambda: object()
    b = DefaultFactoryMunch(default, bar='baz', lol={})
    assert b.foo is not b.baz
    assert b.foo is b['foo']
    assert b.foobar is b.foobar

    b = DefaultFactoryMunch(list)
    assert b.foo == []
    b.foo.append('bar')
    assert b.foo == ['bar']
    assert b.default_factory is list


def test_setattr_default_factory():
    b = DefaultFactoryMunch(lambda: None, foo='bar', this_is='useful when subclassing')
    assert hasattr(b.values, '__call__')

    b.values = 'uh oh'
    assert b.values == 'uh oh'
    assert b['values'] is None

    assert b.default_factory() is None
    assert 'default_factory' not in b


def test_delattr_default_factory():
    b = DefaultFactoryMunch(lambda: None, lol=42)
    del b.lol

    assert b.lol is None
    assert b['lol'] is None


def test_fromDict_default_factory():
    obj = object()
    undefined = lambda: obj
    b = DefaultFactoryMunch.fromDict({'urmom': {'sez': {'what': 'what'}}}, undefined)
    assert b.urmom.sez.what == 'what'
    assert b.urmom.sez.foo is undefined()


def test_copy_default_factory():
    undefined = lambda: object()
    m = DefaultFactoryMunch.fromDict({'urmom': {'sez': {'what': 'what'}}}, undefined)
    c = m.copy()
    assert c is not m
    assert c.urmom is not m.urmom
    assert c.urmom.sez is not m.urmom.sez
    assert c.urmom.sez.what == 'what'
    assert c == m


def test_munchify_default_factory():
    undefined = lambda: object()
    b = munchify(
        {'urmom': {'sez': {'what': 'what'}}},
        lambda d: DefaultFactoryMunch(undefined, d))
    assert b.urmom.sez.what == 'what'
    assert b.urdad is not undefined()
    assert b.urmom.sez.ni is not b.urdad


def test_munchify_cycle():
    # dict1 -> dict2 -> dict1
    x = dict(id="x")
    y = dict(x=x, id="y")
    x['y'] = y

    m = munchify(x)
    assert m.id == "x"
    assert m.y.id == "y"
    assert m.y.x is m

    # dict -> list -> dict
    x = dict(id="x")
    y = ["y", x]
    x["y"] = y

    m = munchify(x)
    assert m.id == "x"
    assert m.y[0] == "y"
    assert m.y[1] is m

    # dict -> tuple -> dict
    x = dict(id="x")
    y = ("y", x)
    x["y"] = y

    m = munchify(x)
    assert m.id == "x"
    assert m.y[0] == "y"
    assert m.y[1] is m

    # dict1 -> list -> dict2 -> list
    z = dict(id="z")
    y = ["y", z]
    z["y"] = y
    x = dict(id="x", y=y)

    m = munchify(x)
    assert m.id == "x"
    assert m.y[0] == "y"
    assert m.y[1].id == "z"
    assert m.y[1].y is m.y

    # dict1 -> tuple -> dict2 -> tuple
    z = dict(id="z")
    y = ("y", z)
    z["y"] = y
    x = dict(id="x", y=y)

    m = munchify(x)
    assert m.id == "x"
    assert m.y[0] == "y"
    assert m.y[1].id == "z"
    assert m.y[1].y is m.y

def test_unmunchify_cycle():
    # munch -> munch -> munch
    x = Munch(id="x")
    y = Munch(x=x, id="y")
    x.y = y

    d = unmunchify(x)
    assert d["id"] == "x"
    assert d["y"]["id"] == "y"
    assert d["y"]["x"] is d

    # munch -> list -> munch
    x = Munch(id="x")
    y = ["y", x]
    x.y = y

    d = unmunchify(x)
    assert d["id"] == "x"
    assert d["y"][0] == "y"
    assert d["y"][1] is d

    # munch -> tuple -> munch
    x = Munch(id="x")
    y = ("y", x)
    x.y = y

    d = unmunchify(x)
    assert d["id"] == "x"
    assert d["y"][0] == "y"
    assert d["y"][1] is d

    # munch1 -> list -> munch2 -> list
    z = Munch(id="z")
    y = ["y", z]
    z.y = y
    x = Munch(id="x", y=y)

    d = unmunchify(x)
    assert d["id"] == "x"
    assert d["y"][0] == "y"
    assert d["y"][1]["id"] == "z"
    assert d["y"][1]["y"] is d["y"]

    # munch1 -> tuple -> munch2 -> tuple
    z = Munch(id="z")
    y = ("y", z)
    z.y = y
    x = Munch(id="x", y=y)

    d = unmunchify(x)
    assert d["id"] == "x"
    assert d["y"][0] == "y"
    assert d["y"][1]["id"] == "z"
    assert d["y"][1]["y"] is d["y"]


def test_repr_default_factory():
    b = DefaultFactoryMunch(list, foo=DefaultFactoryMunch(list, lol=True), ponies='are pretty!')
    assert repr(b).startswith("DefaultFactoryMunch(list, {'")
    assert "'ponies': 'are pretty!'" in repr(b)

    assert eval(repr(b)) == b  # pylint: disable=eval-used


def test_pickling_unpickling_nested():
    m = {'a': {'b': 'c'}}
    m = munchify(m)
    assert m == Munch({'a': Munch({'b': 'c'})})
    assert isinstance(m.a, Munch)
    result = pickle.loads(pickle.dumps(m))
    assert result == m
    assert isinstance(result.a, Munch)


def test_setitem_dunder_for_subclass():

    def test_class(cls, *args):
        class CustomMunch(cls):
            def __setitem__(self, k, v):
                super().__setitem__(k, [v] * 2)
        custom_munch = CustomMunch(*args, a='foo')
        assert custom_munch.a == ['foo', 'foo']
        regular_dict = {}
        regular_dict.update(custom_munch)
        assert regular_dict['a'] == ['foo', 'foo']
        assert repr(regular_dict) == "{'a': ['foo', 'foo']}"
        custom_munch.setdefault('bar', 'baz')
        assert custom_munch.bar == ['baz', 'baz']

    test_class(Munch)
    test_class(DefaultFactoryMunch, list)
    test_class(DefaultMunch, 42)


def test_getitem_dunder_for_subclass():
    class CustomMunch(Munch):
        def __getitem__(self, k):
            return 42

    custom_munch = CustomMunch(a='foo')
    custom_munch.update({'b': 1})
    assert custom_munch.a == 42
    assert custom_munch.get('b') == 42
    assert custom_munch.copy() == Munch(a=42, b=42)


@pytest.mark.usefixtures("yaml")
def test_get_default_value(munch_obj):
    assert munch_obj.get("fake_key", "default_value") == "default_value"
    assert isinstance(munch_obj.toJSON(), str)
    assert isinstance(munch_obj.toYAML(), str)
    munch_obj.copy()
    data = munch_obj.toDict()
    munch_cls = type(munch_obj)
    kwargs = {} if munch_cls != DefaultFactoryMunch else {"default_factory": munch_obj.default_factory}
    munch_cls.fromDict(data, **kwargs)


def test_munchify_tuple_list():
    data = ([{'A': 'B'}],)
    actual = munchify(data)
    expected = ([Munch(A='B')],)
    assert actual == expected


def test_munchify_tuple_list_more_elements():
    data = (1, 2, [{'A': 'B'}])
    actual = munchify(data)
    expected = (1, 2, [Munch({'A': 'B'})])
    assert actual == expected