File: test_utils.py

package info (click to toggle)
dictdiffer 0.9.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 292 kB
  • sloc: python: 1,519; makefile: 153; sh: 6
file content (146 lines) | stat: -rw-r--r-- 5,804 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
# This file is part of Dictdiffer.
#
# Copyright (C) 2015 CERN.
#
# Dictdiffer is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more
# details.

import unittest

from dictdiffer.utils import (PathLimit, WildcardDict, create_dotted_node,
                              dot_lookup, get_path, is_super_path, nested_hash)


class UtilsTest(unittest.TestCase):
    def test_wildcarddict(self):
        wd = WildcardDict()

        wd[('authors', '*')] = True

        self.assertRaises(KeyError, wd.__getitem__, ('authors',))
        self.assertTrue(wd[('authors', 1)])
        self.assertTrue(wd[('authors', 1, 'name')])
        self.assertTrue(wd[('authors', 1, 'affiliation')])

        del wd[('authors', '*')]

        wd[('authors', '+')] = True

        self.assertRaises(KeyError, wd.__getitem__, ('authors',))
        self.assertTrue(wd[('authors', 1)])
        self.assertRaises(KeyError, wd.__getitem__, ('authors', 1, 'name'))
        self.assertRaises(KeyError, wd.__getitem__, ('authors', 1,
                                                     'affiliation'))

        del wd[('authors', '+')]

        wd[('foo', 'bar')] = True

        self.assertRaises(KeyError, wd.__getitem__, ('foo',))
        self.assertTrue(wd[('foo', 'bar')])
        self.assertRaises(KeyError, wd.__getitem__, ('foo', 'bar', 'banana'))

        # query_path part
        wd = WildcardDict()
        wd[('authors', '*')] = True
        wd[('apple', '+')] = True
        wd[('foo', 'bar')] = True

        self.assertRaises(KeyError, wd.query_path, ('utz',))
        self.assertRaises(KeyError, wd.query_path, ('foo',))
        self.assertRaises(KeyError, wd.query_path, ('bar',))
        self.assertRaises(KeyError, wd.query_path, ('apple', 'banana',
                                                    'mango'))
        self.assertEqual(('authors', '*'), wd.query_path(('authors', 1)))
        self.assertEqual(('authors', '*'), wd.query_path(('authors', 1, 1)))
        self.assertEqual(('authors', '*'), wd.query_path(('authors', 1, 1, 1)))
        self.assertEqual(('apple', '+'), wd.query_path(('apple', 'banana')))
        self.assertEqual(('apple', '+'), wd.query_path(('apple', 'mango')))
        self.assertEqual(('foo', 'bar'), wd.query_path(('foo', 'bar')))

    def test_pathlimit(self):
        path_limit = PathLimit([('author', 'name')])
        self.assertFalse(path_limit.path_is_limit(('author')))
        self.assertTrue(path_limit.path_is_limit(('author', 'name')))
        self.assertFalse(path_limit.path_is_limit(('author', 'name', 'foo')))

        path_limit = PathLimit([('authors', '*')])
        self.assertFalse(path_limit.path_is_limit(('authors')))
        self.assertTrue(path_limit.path_is_limit(('authors', 'name')))
        self.assertTrue(path_limit.path_is_limit(('authors', 1)))
        self.assertTrue(path_limit.path_is_limit(('authors', 2)))
        self.assertFalse(path_limit.path_is_limit(('authors', 'name', 'foo')))

    def test_create_dotted_node(self):
        node = ('foo', 'bar')
        self.assertEqual('foo.bar', create_dotted_node(node))

        node = ('foo', 1)
        self.assertEqual(['foo', 1], create_dotted_node(node))

        node = ('foo', 1, 'bar')
        self.assertEqual(['foo', 1, 'bar'], create_dotted_node(node))

    def test_get_path(self):
        patch = ('add/delete', '', [('author', 'Bob')])
        self.assertEqual(('author',), get_path(patch))
        patch = ('add/delete', 'authors', [('name', 'Bob')])
        self.assertEqual(('authors', 'name'), get_path(patch))
        patch = ('add/delete', 'foo.bar', [('name', 'Bob')])
        self.assertEqual(('foo', 'bar', 'name'), get_path(patch))
        patch = ('add/delete', ['foo', 1], [('name', 'Bob')])
        self.assertEqual(('foo', 1, 'name'), get_path(patch))

        patch = ('change', 'foo', [('John', 'Bob')])
        self.assertEqual(('foo',), get_path(patch))
        patch = ('change', 'foo.bar', [('John', 'Bob')])
        self.assertEqual(('foo', 'bar'), get_path(patch))
        patch = ('change', ['foo', 'bar'], [('John', 'Bob')])
        self.assertEqual(('foo', 'bar'), get_path(patch))
        patch = ('change', ['foo', 1], [('John', 'Bob')])
        self.assertEqual(('foo', 1), get_path(patch))

    def test_is_super_path(self):
        # # True
        path1 = ('authors', 1, 'name')
        path2 = ('authors', 1, 'name')
        self.assertTrue(is_super_path(path1, path2))

        path1 = ('authors', 1)
        path2 = ('authors', 1, 'name')
        self.assertTrue(is_super_path(path1, path2))

        path1 = ('authors',)
        path2 = ('authors', 1, 'name')
        self.assertTrue(is_super_path(path1, path2))

        # # False
        path1 = ('authors', 1, 'name')
        path2 = ('authors', 1, 'surname')
        self.assertFalse(is_super_path(path1, path2))

        path1 = ('authors', 2)
        path2 = ('authors', 1, 'surname')
        self.assertFalse(is_super_path(path1, path2))

        path1 = ('author',)
        path2 = ('authors', 1, 'surname')
        self.assertFalse(is_super_path(path1, path2))

    def test_dot_lookup(self):
        self.assertEqual(dot_lookup({'a': {'b': 'hello'}}, 'a.b'), 'hello')
        self.assertEqual(dot_lookup({'a': {'b': 'hello'}}, ['a', 'b']),
                         'hello')

        self.assertEqual(dot_lookup({'a': {'b': 'hello'}}, 'a.b', parent=True),
                         {'b': 'hello'})
        self.assertEqual(dot_lookup({'a': {'b': 'hello'}}, ''),
                         {'a': {'b': 'hello'}})

    def test_nested_hash(self):
        # No reasonable way to test this
        nested_hash([1, 2, 3])
        nested_hash((1, 2, 3))
        nested_hash(set([1, 2, 3]))
        nested_hash({'foo': 'bar'})