Debian Codegraph @

tests/test_dictdiffer.py

raw
1 # SPDX-FileCopyrightText: 2013 Fatih Erikli.
2 # SPDX-FileCopyrightText: 2013, 2014, 2015, 2016 CERN.
3 # SPDX-FileCopyrightText: 2017-2019 ETH Zurich, Swiss Data Science Center, Jiri Kuncar.
4 # SPDX-License-Identifier: MIT
5
6 import unittest
7 from collections import OrderedDict
8 from collections.abc import MutableMapping, MutableSequence
9
10 import pytest
11
12 from dictdiffer import HAS_NUMPY, diff, dot_lookup, patch, revert, swap
13 from dictdiffer.utils import PathLimit
14
15
16 class DictDifferTests(unittest.TestCase):
17 def test_without_dot_notation(self):
18 (change1,) = diff({'a': {'x': 1}},
19 {'a': {'x': 2}},
20 dot_notation=False)
21
22 assert change1 == ('change', ['a', 'x'], (1, 2))
23
24 def test_with_dot_notation(self):
25 (change1,) = diff({'a': {'x': 1}},
26 {'a': {'x': 2}})
27
28 assert change1 == ('change', 'a.x', (1, 2))
29
30 def test_addition(self):
31 first = {}
32 second = {'a': 'b'}
33 diffed = next(diff(first, second))
34 assert ('add', '', [('a', 'b')]) == diffed
35
36 def test_deletion(self):
37 first = {'a': 'b'}
38 second = {}
39 diffed = next(diff(first, second))
40 assert ('remove', '', [('a', 'b')]) == diffed
41
42 def test_change(self):
43 first = {'a': 'b'}
44 second = {'a': 'c'}
45 diffed = next(diff(first, second))
46 assert ('change', 'a', ('b', 'c')) == diffed
47
48 first = {'a': None}
49 second = {'a': 'c'}
50 diffed = next(diff(first, second))
51 assert ('change', 'a', (None, 'c')) == diffed
52
53 first = {'a': 'c'}
54 second = {'a': None}
55 diffed = next(diff(first, second))
56 assert ('change', 'a', ('c', None)) == diffed
57
58 first = {'a': 'c'}
59 second = {'a': u'c'}
60 diffed = list(diff(first, second))
61 assert [] == diffed
62
63 first = {'a': 'b'}
64 second = {'a': None}
65 diffed = next(diff(first, second))
66 assert ('change', 'a', ('b', None)) == diffed
67
68 first = {'a': 10.0}
69 second = {'a': 10.5}
70 diffed = next(diff(first, second))
71 assert ('change', 'a', (10.0, 10.5)) == diffed
72
73 def test_immutable_diffs(self):
74 first = {'a': 'a'}
75 second = {'a': {'b': 'b'}}
76 result = list(diff(first, second))
77 assert result[0][2][1]['b'] == 'b'
78 second['a']['b'] = 'c' # result MUST stay unchanged
79 assert result[0][2][1]['b'] == 'b'
80
81 def test_tolerance(self):
82 first = {'a': 'b'}
83 second = {'a': 'c'}
84 diffed = next(diff(first, second, tolerance=0.1))
85 assert ('change', 'a', ('b', 'c')) == diffed
86
87 first = {'a': None}
88 second = {'a': 'c'}
89 diffed = next(diff(first, second, tolerance=0.1))
90 assert ('change', 'a', (None, 'c')) == diffed
91
92 first = {'a': 10.0}
93 second = {'a': 10.5}
94 diffed = list(diff(first, second, tolerance=0.1))
95 assert [] == diffed
96
97 diffed = next(diff(first, second, tolerance=0.01))
98 assert ('change', 'a', (10.0, 10.5)) == diffed
99
100 first = {'a': 10.0, 'b': 1.0e-15}
101 second = {'a': 10.5, 'b': 2.5e-15}
102 diffed = sorted(diff(
103 first, second, tolerance=0.01
104 ))
105 assert [
106 ('change', 'a', (10.0, 10.5)),
107 ('change', 'b', (1.0e-15, 2.5e-15)),
108 ] == diffed
109
110 diffed = sorted(diff(
111 first, second, tolerance=0.01, absolute_tolerance=1e-12
112 ))
113 assert [('change', 'a', (10.0, 10.5))] == diffed
114
115 diffed = sorted(diff(
116 first, second, tolerance=0.01, absolute_tolerance=1e-18
117 ))
118 assert [
119 ('change', 'a', (10.0, 10.5)),
120 ('change', 'b', (1.0e-15, 2.5e-15)),
121 ] == diffed
122
123 diffed = sorted(diff(
124 first, second, tolerance=0.1, absolute_tolerance=1e-18
125 ))
126 assert [('change', 'b', (1.0e-15, 2.5e-15))] == diffed
127
128 diffed = sorted(diff(
129 first, second, tolerance=0.1, absolute_tolerance=1e-12
130 ))
131 assert [] == diffed
132
133 diffed = sorted(diff(
134 first, second, tolerance=None, absolute_tolerance=None
135 ))
136 assert [
137 ('change', 'a', (10.0, 10.5)),
138 ('change', 'b', (1.0e-15, 2.5e-15)),
139 ] == diffed
140
141 first = {'a': 10.0, 'b': 1.0e-15}
142 second = {'a': 10.0, 'b': 1.0e-15}
143 diffed = sorted(diff(
144 first, second, tolerance=None, absolute_tolerance=None
145 ))
146 assert [] == diffed
147
148 def test_path_limit_as_list(self):
149 first = {}
150 second = {'author': {'last_name': 'Doe', 'first_name': 'John'}}
151 diffed = list(diff(first, second, path_limit=[('author',)]))
152
153 res = [('add', '', [('author',
154 {'first_name': 'John', 'last_name': 'Doe'})])]
155
156 assert res == diffed
157
158 def test_path_limit_addition(self):
159 first = {}
160 second = {'author': {'last_name': 'Doe', 'first_name': 'John'}}
161 p = PathLimit([('author',)])
162 diffed = list(diff(first, second, path_limit=p))
163
164 res = [('add', '', [('author',
165 {'first_name': 'John', 'last_name': 'Doe'})])]
166
167 assert res == diffed
168
169 first = {}
170 second = {'author': {'last_name': 'Doe', 'first_name': 'John'}}
171 p = PathLimit([('author',)])
172 diffed = list(diff(first, second, path_limit=p, expand=True))
173
174 res = [('add', '', [('author',
175 {'first_name': 'John', 'last_name': 'Doe'})])]
176
177 assert res == diffed
178
179 first = {}
180 second = {'author': {'last_name': 'Doe', 'first_name': 'John'}}
181 p = PathLimit()
182 diffed = list(diff(first, second, path_limit=p, expand=True))
183 res = [('add', '', [('author', {})]),
184 ('add', 'author', [('first_name', 'John')]),
185 ('add', 'author', [('last_name', 'Doe')])]
186
187 assert len(diffed) == 3
188 for patch in res:
189 assert patch in diffed
190
191 def test_path_limit_deletion(self):
192 first = {'author': {'last_name': 'Doe', 'first_name': 'John'}}
193 second = {}
194 p = PathLimit([('author',)])
195 diffed = list(diff(first, second, path_limit=p, expand=True))
196
197 res = [('remove', '', [('author',
198 {'first_name': 'John', 'last_name': 'Doe'})])]
199
200 assert res == diffed
201
202 def test_path_limit_change(self):
203 first = {'author': {'last_name': 'Do', 'first_name': 'John'}}
204 second = {'author': {'last_name': 'Do', 'first_name': 'John'}}
205 p = PathLimit([('author',)])
206 diffed = list(diff(first, second, path_limit=p, expand=True))
207 assert [] == diffed
208
209 first = {'author': {'last_name': 'Do', 'first_name': 'John'}}
210 second = {'author': {'last_name': 'Doe', 'first_name': 'John'}}
211 p = PathLimit([('author',)])
212 diffed = list(diff(first, second, path_limit=p, expand=True))
213
214 res = [('change',
215 ['author'],
216 ({'first_name': 'John', 'last_name': 'Do'},
217 {'first_name': 'John', 'last_name': 'Doe'}))]
218
219 assert res == diffed
220
221 first = {'author': {'last_name': 'Do', 'first_name': 'John'}}
222 second = {'author': {'last_name': 'Doe', 'first_name': 'John'}}
223 p = PathLimit()
224 diffed = list(diff(first, second, path_limit=p, expand=True))
225
226 res = [('change', 'author.last_name', ('Do', 'Doe'))]
227
228 assert res == diffed
229
230 def test_expand_addition(self):
231 first = {}
232 second = {'foo': 'bar', 'apple': 'banana'}
233 diffed = list(diff(first, second, expand=True))
234 res = [('add', '', [('foo', 'bar')]),
235 ('add', '', [('apple', 'banana')])]
236
237 assert len(diffed) == 2
238 for patch in res:
239 assert patch in diffed
240
241 def test_expand_deletion(self):
242 first = {'foo': 'bar', 'apple': 'banana'}
243 second = {}
244 diffed = list(diff(first, second, expand=True))
245 res = [('remove', '', [('foo', 'bar')]),
246 ('remove', '', [('apple', 'banana')])]
247
248 assert len(diffed) == 2
249 for patch in res:
250 assert patch in diffed
251
252 def test_nodes(self):
253 first = {'a': {'b': {'c': 'd'}}}
254 second = {'a': {'b': {'c': 'd', 'e': 'f'}}}
255 diffed = next(diff(first, second))
256 assert ('add', 'a.b', [('e', 'f')]) == diffed
257
258 def test_add_list(self):
259 first = {'a': []}
260 second = {'a': ['b']}
261 diffed = next(diff(first, second))
262 assert ('add', 'a', [(0, 'b')]) == diffed
263
264 def test_remove_list(self):
265 first = {'a': ['b', 'c']}
266 second = {'a': []}
267 diffed = next(diff(first, second))
268 assert ('remove', 'a', [(1, 'c'), (0, 'b'), ]) == diffed
269
270 def test_add_set(self):
271 first = {'a': {1, 2, 3}}
272 second = {'a': {0, 1, 2, 3}}
273 diffed = next(diff(first, second))
274 assert ('add', 'a', [(0, set([0]))]) == diffed
275
276 def test_remove_set(self):
277 first = {'a': set([0, 1, 2, 3])}
278 second = {'a': set([1, 2, 3])}
279 diffed = next(diff(first, second))
280 assert ('remove', 'a', [(0, set([0]))]) == diffed
281
282 def test_change_set(self):
283 first = {'a': set([0, 1, 2, 3])}
284 second = {'a': set([1, 2, 3, 4])}
285 diffed = list(diff(first, second))
286 assert ('add', 'a', [(0, set([4]))]) in diffed
287 assert ('remove', 'a', [(0, set([0]))]) in diffed
288
289 def test_add_set_shift_order(self):
290 first = set(["changeA", "changeB"])
291 second = set(["changeA", "changeC", "changeB"])
292 diffed = list(diff(first, second))
293 # There should only be 1 change reported
294 assert len(diffed) == 1
295 assert ('add', '', [(0, {'changeC'})]) in diffed
296
297 def test_change_set_order(self):
298 first = set(["changeA", "changeC", "changeB"])
299 second = set(["changeB", "changeC", "changeA"])
300 diffed = list(diff(first, second))
301 # There should be zero reported diffs
302 assert len(diffed) == 0
303
304 def test_types(self):
305 first = {'a': ['a']}
306 second = {'a': 'a'}
307 diffed = next(diff(first, second))
308 assert ('change', 'a', (['a'], 'a')) == diffed
309
310 def test_nan(self):
311 value = float('nan')
312 diffed = list(diff([value], [value]))
313 assert [] == diffed
314
315 diffed = list(diff([value], [3.5]))
316 assert [('change', [0], (value, 3.5))] == diffed
317
318 @unittest.skipIf(not HAS_NUMPY, 'NumPy is not installed')
319 def test_numpy_nan(self):
320 """Compare NumPy NaNs (#114)."""
321 import numpy as np
322 first = {'a': np.float32('nan')}
323 second = {'a': float('nan')}
324 result = list(diff(first, second))
325 assert result == []
326
327 def test_unicode_keys(self):
328 first = {u'привет': 1}
329 second = {'hello': 1}
330 diffed = list(diff(first, second))
331 assert ('add', '', [('hello', 1)]) in diffed
332 assert ('remove', '', [(u'привет', 1)]) in diffed
333
334 diffed = list(diff(first, second, ignore=['hello']))
335 assert ('remove', '', [(u'привет', 1)]) == diffed[0]
336
337 diffed = list(diff(first, second, ignore=[u'привет']))
338 assert ('add', '', [('hello', 1)]) == diffed[0]
339
340 def test_dotted_key(self):
341 first = {'a.b': {'c.d': 1}}
342 second = {'a.b': {'c.d': 2}}
343 diffed = list(diff(first, second))
344 assert [('change', ['a.b', 'c.d'], (1, 2))] == diffed
345
346 def test_ignore_key(self):
347 first = {'a': 'a', 'b': 'b', 'c': 'c'}
348 second = {'a': 'a', 'b': 2, 'c': 3}
349 diffed = next(diff(first, second, ignore=['b']))
350 assert ('change', 'c', ('c', 3)) == diffed
351
352 def test_ignore_dotted_key(self):
353 first = {'a': {'aa': 'A', 'ab': 'B', 'ac': 'C'}}
354 second = {'a': {'aa': 1, 'ab': 'B', 'ac': 3}}
355 diffed = next(diff(first, second, ignore=['a.aa']))
356 assert ('change', 'a.ac', ('C', 3)) == diffed
357
358 def test_ignore_with_unicode_sub_keys(self):
359 first = {u'a': {u'aא': {u'aa': 'A'}}}
360 second = {u'a': {u'aא': {u'aa': 'B'}}}
361
362 assert len(list(diff(first, second))) == 1
363 assert len(list(diff(first, second, ignore=[u'a.aא.aa']))) == 0
364 assert len(
365 list(diff(first, second, ignore=[[u'a', u'aא', u'aa']
366 ]))) == 0
367
368 def test_ignore_complex_key(self):
369 first = {'a': {1: {'a': 'a', 'b': 'b'}}}
370 second = {'a': {1: {'a': 1, 'b': 2}}}
371 diffed = next(diff(first, second, ignore=[['a', 1, 'a']]))
372 assert ('change', ['a', 1, 'b'], ('b', 2)) == diffed
373
374 def test_ignore_missing_keys(self):
375 first = {'a': 'a'}
376 second = {'a': 'a', 'b': 'b'}
377 assert len(list(diff(first, second, ignore=['b']))) == 0
378 assert len(list(diff(second, first, ignore=['b']))) == 0
379
380 def test_ignore_missing_complex_keys(self):
381 first = {'a': {1: {'a': 'a', 'b': 'b'}}}
382 second = {'a': {1: {'a': 1}}}
383 diffed = next(diff(first, second, ignore=[['a', 1, 'b']]))
384 assert ('change', ['a', 1, 'a'], ('a', 1)) == diffed
385 diffed = next(diff(second, first, ignore=[['a', 1, 'b']]))
386 assert ('change', ['a', 1, 'a'], (1, 'a')) == diffed
387
388 def test_ignore_stringofintegers_keys(self):
389 a = {'1': '1', '2': '2', '3': '3'}
390 b = {'1': '1', '2': '2', '3': '99', '4': '100'}
391
392 assert list(diff(a, b, ignore={'3', '4'})) == []
393
394 def test_ignore_integers_keys(self):
395 a = {1: 1, 2: 2, 3: 3}
396 b = {1: 1, 2: 2, 3: 99, 4: 100}
397
398 assert len(list(diff(a, b, ignore={3, 4}))) == 0
399
400 def test_ignore_with_ignorecase(self):
401 class IgnoreCase(set):
402 def __contains__(self, key):
403 return set.__contains__(self, str(key).lower())
404
405 assert list(diff({'a': 1, 'b': 2}, {'A': 3, 'b': 4},
406 ignore=IgnoreCase('a'))) == [('change', 'b', (2, 4))]
407
408 def test_complex_diff(self):
409 """Check regression on issue #4."""
410 from decimal import Decimal
411
412 d1 = {
413 'id': 1,
414 'code': None,
415 'type': u'foo',
416 'bars': [
417 {'id': 6934900},
418 {'id': 6934977},
419 {'id': 6934992},
420 {'id': 6934993},
421 {'id': 6935014}],
422 'n': 10,
423 'date_str': u'2013-07-08 00:00:00',
424 'float_here': 0.454545,
425 'complex': [{
426 'id': 83865,
427 'goal': Decimal('2.000000'),
428 'state': u'active'}],
429 'profile_id': None,
430 'state': u'active'
431 }
432
433 d2 = {
434 'id': u'2',
435 'code': None,
436 'type': u'foo',
437 'bars': [
438 {'id': 6934900},
439 {'id': 6934977},
440 {'id': 6934992},
441 {'id': 6934993},
442 {'id': 6935014}],
443 'n': 10,
444 'date_str': u'2013-07-08 00:00:00',
445 'float_here': 0.454545,
446 'complex': [{
447 'id': 83865,
448 'goal': Decimal('2.000000'),
449 'state': u'active'}],
450 'profile_id': None,
451 'state': u'active'
452 }
453
454 assert len(list(diff(d1, {}))) > 0
455 assert d1['id'] == 1
456 assert d2['id'] == u'2'
457 assert d1 is not d2
458 assert d1 != d2
459 assert len(list(diff(d1, d2))) > 0
460
461 def test_list_change(self):
462 """Produced diffs should not contain empty list instructions (#30)."""
463 first = {"a": {"b": [100, 101, 201]}}
464 second = {"a": {"b": [100, 101, 202]}}
465 result = list(diff(first, second))
466 assert len(result) == 1
467 assert result == [('change', ['a', 'b', 2], (201, 202))]
468
469 def test_list_same(self):
470 """Diff for the same list should be empty."""
471 first = {1: [1]}
472 assert len(list(diff(first, first))) == 0
473
474 @unittest.skipIf(not HAS_NUMPY, 'NumPy is not installed')
475 def test_numpy_array(self):
476 """Compare NumPy arrays (#68)."""
477 import numpy as np
478 first = np.array([1, 2, 3])
479 second = np.array([1, 2, 4])
480 result = list(diff(first, second))
481 assert result == [('change', [2], (3, 4))]
482
483 def test_dict_subclasses(self):
484 class Foo(dict):
485 pass
486
487 first = Foo({2014: [
488 dict(month=6, category=None, sum=672.00),
489 dict(month=6, category=1, sum=-8954.00),
490 dict(month=7, category=None, sum=7475.17),
491 dict(month=7, category=1, sum=-11745.00),
492 dict(month=8, category=None, sum=-12140.00),
493 dict(month=8, category=1, sum=-11812.00),
494 dict(month=9, category=None, sum=-31719.41),
495 dict(month=9, category=1, sum=-11663.00),
496 ]})
497
498 second = Foo({2014: [
499 dict(month=6, category=None, sum=672.00),
500 dict(month=6, category=1, sum=-8954.00),
501 dict(month=7, category=None, sum=7475.17),
502 dict(month=7, category=1, sum=-11745.00),
503 dict(month=8, category=None, sum=-12141.00),
504 dict(month=8, category=1, sum=-11812.00),
505 dict(month=9, category=None, sum=-31719.41),
506 dict(month=9, category=2, sum=-11663.00),
507 ]})
508
509 diffed = next(diff(first, second))
510 assert ('change', [2014, 4, 'sum'], (-12140.0, -12141.0)) == diffed
511
512 def test_collection_subclasses(self):
513 class DictA(MutableMapping):
514
515 def __init__(self, *args, **kwargs):
516 self.__dict__.update(*args, **kwargs)
517
518 def __setitem__(self, key, value):
519 self.__dict__[key] = value
520
521 def __getitem__(self, key):
522 return self.__dict__[key]
523
524 def __delitem__(self, key):
525 del self.__dict__[key]
526
527 def __iter__(self):
528 return iter(self.__dict__)
529
530 def __len__(self):
531 return len(self.__dict__)
532
533 class DictB(MutableMapping):
534
535 def __init__(self, *args, **kwargs):
536 self.__dict__.update(*args, **kwargs)
537
538 def __setitem__(self, key, value):
539 self.__dict__[key] = value
540
541 def __getitem__(self, key):
542 return self.__dict__[key]
543
544 def __delitem__(self, key):
545 del self.__dict__[key]
546
547 def __iter__(self):
548 return iter(self.__dict__)
549
550 def __len__(self):
551 return len(self.__dict__)
552
553 class ListA(MutableSequence):
554
555 def __init__(self, *args, **kwargs):
556 self._list = list(*args, **kwargs)
557
558 def __getitem__(self, index):
559 return self._list[index]
560
561 def __setitem__(self, index, value):
562 self._list[index] = value
563
564 def __delitem__(self, index):
565 del self._list[index]
566
567 def __iter__(self):
568 for value in self._list:
569 yield value
570
571 def __len__(self):
572 return len(self._list)
573
574 def insert(self, index, value):
575 self._list.insert(index, value)
576
577 daa = DictA(a=ListA(['a', 'A']))
578 dba = DictB(a=ListA(['a', 'A']))
579 dbb = DictB(a=ListA(['b', 'A']))
580 assert list(diff(daa, dba)) == []
581 assert list(diff(daa, dbb)) == [('change', ['a', 0], ('a', 'b'))]
582 assert list(diff(dba, dbb)) == [('change', ['a', 0], ('a', 'b'))]
583
584
585 class DiffPatcherTests(unittest.TestCase):
586 def test_addition(self):
587 first = {}
588 second = {'a': 'b'}
589 assert second == patch(
590 [('add', '', [('a', 'b')])], first)
591
592 first = {'a': {'b': 'c'}}
593 second = {'a': {'b': 'c', 'd': 'e'}}
594 assert second == patch(
595 [('add', 'a', [('d', 'e')])], first)
596
597 def test_changes(self):
598 first = {'a': 'b'}
599 second = {'a': 'c'}
600 assert second == patch(
601 [('change', 'a', ('b', 'c'))], first)
602
603 first = {'a': {'b': {'c': 'd'}}}
604 second = {'a': {'b': {'c': 'e'}}}
605 assert second == patch(
606 [('change', 'a.b.c', ('d', 'e'))], first)
607
608 def test_remove(self):
609 first = {'a': {'b': 'c'}}
610 second = {'a': {}}
611 assert second == patch(
612 [('remove', 'a', [('b', 'c')])], first)
613
614 first = {'a': 'b'}
615 second = {}
616 assert second == patch(
617 [('remove', '', [('a', 'b')])], first)
618
619 def test_remove_list(self):
620 first = {'a': [1, 2, 3]}
621 second = {'a': [1, ]}
622 assert second == patch(
623 [('remove', 'a', [(2, 3), (1, 2), ]), ], first)
624
625 def test_add_list(self):
626 first = {'a': [1]}
627 second = {'a': [1, 2]}
628 assert second == patch(
629 [('add', 'a', [(1, 2)])], first)
630
631 first = {'a': {'b': [1]}}
632 second = {'a': {'b': [1, 2]}}
633 assert second == patch(
634 [('add', 'a.b', [(1, 2)])], first)
635
636 def test_change_list(self):
637 first = {'a': ['b']}
638 second = {'a': ['c']}
639 assert second == patch(
640 [('change', 'a.0', ('b', 'c'))], first)
641
642 first = {'a': {'b': {'c': ['d']}}}
643 second = {'a': {'b': {'c': ['e']}}}
644 assert second == patch(
645 [('change', 'a.b.c.0', ('d', 'e'))], first)
646
647 first = {'a': {'b': {'c': [{'d': 'e'}]}}}
648 second = {'a': {'b': {'c': [{'d': 'f'}]}}}
649 assert second == patch(
650 [('change', 'a.b.c.0.d', ('e', 'f'))], first)
651
652 def test_remove_set(self):
653 first = {'a': set([1, 2, 3])}
654 second = {'a': set([1])}
655 assert second == patch(
656 [('remove', 'a', [(0, set([2, 3]))])], first)
657
658 def test_add_set(self):
659 first = {'a': set([1])}
660 second = {'a': set([1, 2])}
661 assert second == patch(
662 [('add', 'a', [(0, set([2]))])], first)
663
664 def test_dict_int_key(self):
665 first = {0: 0}
666 second = {0: 'a'}
667 first_patch = [('change', [0], (0, 'a'))]
668 assert second == patch(first_patch, first)
669
670 def test_dict_combined_key_type(self):
671 first = {0: {'1': {2: 3}}}
672 second = {0: {'1': {2: '3'}}}
673 first_patch = [('change', [0, '1', 2], (3, '3'))]
674 assert second == patch(first_patch, first)
675 assert first_patch[0] == list(diff(first, second))[0]
676
677 def test_in_place_patch_and_revert(self):
678 first = {'a': 1}
679 second = {'a': 2}
680 changes = list(diff(first, second))
681 patched_copy = patch(changes, first)
682 assert first != patched_copy
683 reverted_in_place = revert(changes, patched_copy, in_place=True)
684 assert first == reverted_in_place
685 assert patched_copy == reverted_in_place
686 patched_in_place = patch(changes, first, in_place=True)
687 assert first == patched_in_place
688
689
690 class SwapperTests(unittest.TestCase):
691 def test_addition(self):
692 result = 'add', '', [('a', 'b')]
693 swapped = 'remove', '', [('a', 'b')]
694 assert next(swap([result])) == swapped
695
696 result = 'remove', 'a.b', [('c', 'd')]
697 swapped = 'add', 'a.b', [('c', 'd')]
698 assert next(swap([result])) == swapped
699
700 def test_changes(self):
701 result = 'change', '', ('a', 'b')
702 swapped = 'change', '', ('b', 'a')
703 assert next(swap([result])) == swapped
704
705 def test_revert(self):
706 first = {'a': [1, 2]}
707 second = {'a': []}
708 diffed = diff(first, second)
709 patched = patch(diffed, first)
710 assert patched == second
711 diffed = diff(first, second)
712 reverted = revert(diffed, second)
713 assert reverted == first
714
715 def test_list_of_different_length(self):
716 """Check that one can revert list with different length."""
717 first = [1]
718 second = [1, 2, 3]
719 result = list(diff(first, second))
720 assert first == revert(result, second)
721
722
723 class DotLookupTest(unittest.TestCase):
724 def test_list_lookup(self):
725 source = {0: '0'}
726 assert dot_lookup(source, [0]) == '0'
727
728 def test_invalit_lookup_type(self):
729 self.assertRaises(TypeError, dot_lookup, {0: '0'}, 0)
730
731
732 @pytest.mark.parametrize(
733 'ignore,dot_notation,diff_size', [
734 (u'nifi.zookeeper.session.timeout', True, 1),
735 (u'nifi.zookeeper.session.timeout', False, 0),
736 ((u'nifi.zookeeper.session.timeout', ), True, 0),
737 ((u'nifi.zookeeper.session.timeout', ), False, 0),
738 ],
739 )
740 def test_ignore_dotted_ignore_key(ignore, dot_notation, diff_size):
741 key_to_ignore = u'nifi.zookeeper.session.timeout'
742 config_dict = OrderedDict(
743 [('address', 'devops011-slv-01.gvs.ggn'),
744 (key_to_ignore, '3 secs')])
745
746 ref_dict = OrderedDict(
747 [('address', 'devops011-slv-01.gvs.ggn'),
748 (key_to_ignore, '4 secs')])
749
750 assert diff_size == len(
751 list(diff(config_dict, ref_dict,
752 dot_notation=dot_notation,
753 ignore=[ignore])))
754
755
756 if __name__ == "__main__":
757 unittest.main()