From b3416956adfa14f80da84a0f003ca371ef5383f6 Mon Sep 17 00:00:00 2001 From: yoyonel Date: Sun, 17 Mar 2019 17:53:29 +0100 Subject: [PATCH] diff: respect dot_notation flag in ignore argument (closes #107) --- dictdiffer/__init__.py | 7 ++++--- tests/test_dictdiffer.py | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/dictdiffer/__init__.py b/dictdiffer/__init__.py index f91caf6..3895a93 100644 --- a/dictdiffer/__init__.py +++ b/dictdiffer/__init__.py @@ -2,7 +2,7 @@ # # Copyright (C) 2013 Fatih Erikli. # Copyright (C) 2013, 2014, 2015, 2016 CERN. -# Copyright (C) 2017, 2018 ETH Zurich, Swiss Data Science Center, Jiri Kuncar. +# Copyright (C) 2017-2019 ETH Zurich, Swiss Data Science Center, Jiri Kuncar. # # Dictdiffer is free software; you can redistribute it and/or modify # it under the terms of the MIT License; see LICENSE file for more @@ -134,6 +134,8 @@ def _process_ignore_value(value): return value, elif isinstance(value, list): return tuple(value) + elif not dot_notation and isinstance(value, string_types): + return value, return value ignore = type(ignore)(_process_ignore_value(value) for value in ignore) @@ -159,8 +161,7 @@ def _diff_recursive(_first, _second, _node=None): def check(key): """Test if key in current node should be ignored.""" return ignore is None or ( - dotted(_node + [key], - default_type=tuple) not in ignore and + dotted(_node + [key], default_type=tuple) not in ignore and tuple(_node + [key]) not in ignore ) diff --git a/tests/test_dictdiffer.py b/tests/test_dictdiffer.py index bbf6fff..e2ef610 100644 --- a/tests/test_dictdiffer.py +++ b/tests/test_dictdiffer.py @@ -11,6 +11,9 @@ # details. import unittest +from collections import OrderedDict + +import pytest from dictdiffer import HAS_NUMPY, diff, dot_lookup, patch, revert, swap from dictdiffer._compat import MutableMapping, MutableSequence, MutableSet @@ -664,5 +667,29 @@ def test_invalit_lookup_type(self): self.assertRaises(TypeError, dot_lookup, {0: '0'}, 0) +@pytest.mark.parametrize( + 'ignore,dot_notation,diff_size', [ + (u'nifi.zookeeper.session.timeout', True, 1), + (u'nifi.zookeeper.session.timeout', False, 0), + ((u'nifi.zookeeper.session.timeout', ), True, 0), + ((u'nifi.zookeeper.session.timeout', ), False, 0), + ], +) +def test_ignore_dotted_ignore_key(ignore, dot_notation, diff_size): + key_to_ignore = u'nifi.zookeeper.session.timeout' + config_dict = OrderedDict( + [('address', 'devops011-slv-01.gvs.ggn'), + (key_to_ignore, '3 secs')]) + + ref_dict = OrderedDict( + [('address', 'devops011-slv-01.gvs.ggn'), + (key_to_ignore, '4 secs')]) + + assert diff_size == len( + list(diff(config_dict, ref_dict, + dot_notation=dot_notation, + ignore=[ignore]))) + + if __name__ == "__main__": unittest.main()