File: test_mergedict.py

package info (click to toggle)
python-mergedict 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 92 kB
  • sloc: python: 176; makefile: 3
file content (78 lines) | stat: -rw-r--r-- 2,170 bytes parent folder | download | duplicates (2)
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
from mergedict import MergeDict, ConfigDict

import pytest


class TestMergeDict():
    def test_default_merge_is_like_update(self):
        d1 = MergeDict({'a': 1, 'b': 'one', 'c': [1]})
        d1.merge({'a':2, 'c': [2]})
        assert d1 == {'a': 2, 'b': 'one', 'c': [2]}

    def test_custom_merge(self):
        class SumDict(MergeDict):
            @MergeDict.dispatch(int)
            def merge_int(this, other):
                return this + other

        d1 = SumDict({'a': 1, 'b': 'one', 'c': [1]})
        d1.merge({'a':2, 'c': [2]})
        assert d1 == {'a': 3, 'b': 'one', 'c': [2]}

        # make sure subclass doesnt mess up with MergeDict
        d2 = MergeDict({'a': 1, 'b': 'one', 'c': [1]})
        d2.merge({'a':2, 'c': [2]})
        assert d2 == {'a': 2, 'b': 'one', 'c': [2]}


    def test_hierarchy(self):
        class SumDict(MergeDict):
            @MergeDict.dispatch(int)
            def merge_int(this, other):
                return this + other

        class ConcatDict(SumDict):
            @MergeDict.dispatch(str)
            def merge_str(this, other):
                return this + other

        d1 = ConcatDict({'a': 1, 'b': 'one', 'c': [1]})
        d1.merge({'a':2, 'b': '-two'})
        assert d1 == {'a': 3, 'b': 'one-two', 'c': [1]}


    def test_update_error_two_args(self):
        config = MergeDict({'foo': 'bar'})
        pytest.raises(TypeError, config.merge, {'foo': 'baz'}, {'foo': 'baz2'})

    def test_merge_keyword(self):
        config = MergeDict({'foo': 'bar'})
        config.merge(foo='baz')
        assert config['foo'] == 'baz'



def test_ConfigDict():
    d1 = ConfigDict({
            'a': 1,
            'b': 'one',
            'c': [1],
            'd': {'foo': 'x1', 'bar': 'x2'},
            'e': set((3, 5)),
            })
    d1.merge({
            'a': 2,
            'c': [1, 2],
            'd': {'bar': 'y2', 'baz': 'y3'},
            'e': set((3, 6)),
            'f': 'func',
            })
    assert d1 == {
        'a': 2,
        'b': 'one',
        'c': [1, 1, 2],
        'd': {'foo': 'x1', 'bar': 'y2', 'baz': 'y3'},
        'e': set((3, 5, 6)),
        'f': 'func',
        }