| 1 |
# SPDX-FileCopyrightText: 2015 CERN. |
| 2 |
# SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
import unittest |
| 5 |
|
| 6 |
from dictdiffer.utils import ( |
| 7 |
PathLimit, |
| 8 |
WildcardDict, |
| 9 |
create_dotted_node, |
| 10 |
dot_lookup, |
| 11 |
get_path, |
| 12 |
is_super_path, |
| 13 |
nested_hash, |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
class UtilsTest(unittest.TestCase): |
| 18 |
def test_wildcarddict(self): |
| 19 |
wd = WildcardDict() |
| 20 |
|
| 21 |
wd[('authors', '*')] = True |
| 22 |
|
| 23 |
self.assertRaises(KeyError, wd.__getitem__, ('authors',)) |
| 24 |
self.assertTrue(wd[('authors', 1)]) |
| 25 |
self.assertTrue(wd[('authors', 1, 'name')]) |
| 26 |
self.assertTrue(wd[('authors', 1, 'affiliation')]) |
| 27 |
|
| 28 |
del wd[('authors', '*')] |
| 29 |
|
| 30 |
wd[('authors', '+')] = True |
| 31 |
|
| 32 |
self.assertRaises(KeyError, wd.__getitem__, ('authors',)) |
| 33 |
self.assertTrue(wd[('authors', 1)]) |
| 34 |
self.assertRaises(KeyError, wd.__getitem__, ('authors', 1, 'name')) |
| 35 |
self.assertRaises(KeyError, wd.__getitem__, ('authors', 1, |
| 36 |
'affiliation')) |
| 37 |
|
| 38 |
del wd[('authors', '+')] |
| 39 |
|
| 40 |
wd[('foo', 'bar')] = True |
| 41 |
|
| 42 |
self.assertRaises(KeyError, wd.__getitem__, ('foo',)) |
| 43 |
self.assertTrue(wd[('foo', 'bar')]) |
| 44 |
self.assertRaises(KeyError, wd.__getitem__, ('foo', 'bar', 'banana')) |
| 45 |
|
| 46 |
# query_path part |
| 47 |
wd = WildcardDict() |
| 48 |
wd[('authors', '*')] = True |
| 49 |
wd[('apple', '+')] = True |
| 50 |
wd[('foo', 'bar')] = True |
| 51 |
|
| 52 |
self.assertRaises(KeyError, wd.query_path, ('utz',)) |
| 53 |
self.assertRaises(KeyError, wd.query_path, ('foo',)) |
| 54 |
self.assertRaises(KeyError, wd.query_path, ('bar',)) |
| 55 |
self.assertRaises(KeyError, wd.query_path, ('apple', 'banana', |
| 56 |
'mango')) |
| 57 |
self.assertEqual(('authors', '*'), wd.query_path(('authors', 1))) |
| 58 |
self.assertEqual(('authors', '*'), wd.query_path(('authors', 1, 1))) |
| 59 |
self.assertEqual(('authors', '*'), wd.query_path(('authors', 1, 1, 1))) |
| 60 |
self.assertEqual(('apple', '+'), wd.query_path(('apple', 'banana'))) |
| 61 |
self.assertEqual(('apple', '+'), wd.query_path(('apple', 'mango'))) |
| 62 |
self.assertEqual(('foo', 'bar'), wd.query_path(('foo', 'bar'))) |
| 63 |
|
| 64 |
def test_pathlimit(self): |
| 65 |
path_limit = PathLimit([('author', 'name')]) |
| 66 |
self.assertFalse(path_limit.path_is_limit(('author'))) |
| 67 |
self.assertTrue(path_limit.path_is_limit(('author', 'name'))) |
| 68 |
self.assertFalse(path_limit.path_is_limit(('author', 'name', 'foo'))) |
| 69 |
|
| 70 |
path_limit = PathLimit([('authors', '*')]) |
| 71 |
self.assertFalse(path_limit.path_is_limit(('authors'))) |
| 72 |
self.assertTrue(path_limit.path_is_limit(('authors', 'name'))) |
| 73 |
self.assertTrue(path_limit.path_is_limit(('authors', 1))) |
| 74 |
self.assertTrue(path_limit.path_is_limit(('authors', 2))) |
| 75 |
self.assertFalse(path_limit.path_is_limit(('authors', 'name', 'foo'))) |
| 76 |
|
| 77 |
def test_create_dotted_node(self): |
| 78 |
node = ('foo', 'bar') |
| 79 |
self.assertEqual('foo.bar', create_dotted_node(node)) |
| 80 |
|
| 81 |
node = ('foo', 1) |
| 82 |
self.assertEqual(['foo', 1], create_dotted_node(node)) |
| 83 |
|
| 84 |
node = ('foo', 1, 'bar') |
| 85 |
self.assertEqual(['foo', 1, 'bar'], create_dotted_node(node)) |
| 86 |
|
| 87 |
def test_get_path(self): |
| 88 |
patch = ('add/delete', '', [('author', 'Bob')]) |
| 89 |
self.assertEqual(('author',), get_path(patch)) |
| 90 |
patch = ('add/delete', 'authors', [('name', 'Bob')]) |
| 91 |
self.assertEqual(('authors', 'name'), get_path(patch)) |
| 92 |
patch = ('add/delete', 'foo.bar', [('name', 'Bob')]) |
| 93 |
self.assertEqual(('foo', 'bar', 'name'), get_path(patch)) |
| 94 |
patch = ('add/delete', ['foo', 1], [('name', 'Bob')]) |
| 95 |
self.assertEqual(('foo', 1, 'name'), get_path(patch)) |
| 96 |
|
| 97 |
patch = ('change', 'foo', [('John', 'Bob')]) |
| 98 |
self.assertEqual(('foo',), get_path(patch)) |
| 99 |
patch = ('change', 'foo.bar', [('John', 'Bob')]) |
| 100 |
self.assertEqual(('foo', 'bar'), get_path(patch)) |
| 101 |
patch = ('change', ['foo', 'bar'], [('John', 'Bob')]) |
| 102 |
self.assertEqual(('foo', 'bar'), get_path(patch)) |
| 103 |
patch = ('change', ['foo', 1], [('John', 'Bob')]) |
| 104 |
self.assertEqual(('foo', 1), get_path(patch)) |
| 105 |
|
| 106 |
def test_is_super_path(self): |
| 107 |
# # True |
| 108 |
path1 = ('authors', 1, 'name') |
| 109 |
path2 = ('authors', 1, 'name') |
| 110 |
self.assertTrue(is_super_path(path1, path2)) |
| 111 |
|
| 112 |
path1 = ('authors', 1) |
| 113 |
path2 = ('authors', 1, 'name') |
| 114 |
self.assertTrue(is_super_path(path1, path2)) |
| 115 |
|
| 116 |
path1 = ('authors',) |
| 117 |
path2 = ('authors', 1, 'name') |
| 118 |
self.assertTrue(is_super_path(path1, path2)) |
| 119 |
|
| 120 |
# # False |
| 121 |
path1 = ('authors', 1, 'name') |
| 122 |
path2 = ('authors', 1, 'surname') |
| 123 |
self.assertFalse(is_super_path(path1, path2)) |
| 124 |
|
| 125 |
path1 = ('authors', 2) |
| 126 |
path2 = ('authors', 1, 'surname') |
| 127 |
self.assertFalse(is_super_path(path1, path2)) |
| 128 |
|
| 129 |
path1 = ('author',) |
| 130 |
path2 = ('authors', 1, 'surname') |
| 131 |
self.assertFalse(is_super_path(path1, path2)) |
| 132 |
|
| 133 |
def test_dot_lookup(self): |
| 134 |
self.assertEqual(dot_lookup({'a': {'b': 'hello'}}, 'a.b'), 'hello') |
| 135 |
self.assertEqual(dot_lookup({'a': {'b': 'hello'}}, ['a', 'b']), |
| 136 |
'hello') |
| 137 |
|
| 138 |
self.assertEqual(dot_lookup({'a': {'b': 'hello'}}, 'a.b', parent=True), |
| 139 |
{'b': 'hello'}) |
| 140 |
self.assertEqual(dot_lookup({'a': {'b': 'hello'}}, ''), |
| 141 |
{'a': {'b': 'hello'}}) |
| 142 |
|
| 143 |
def test_nested_hash(self): |
| 144 |
# No reasonable way to test this |
| 145 |
nested_hash([1, 2, 3]) |
| 146 |
nested_hash((1, 2, 3)) |
| 147 |
nested_hash(set([1, 2, 3])) |
| 148 |
nested_hash({'foo': 'bar'}) |