From 34910dc0745a26d8f5a7cbc01dc4ac84aa61c211 Mon Sep 17 00:00:00 2001 From: Rodolfo Alonso Hernandez Date: Tue, 27 Nov 2018 18:09:43 +0000 Subject: [PATCH] Add function helpers.compare_dict This function allows to compare two dicts, both keys and values. Change-Id: I0a68d627c00dde49b60e1b47d24822315c16d471 Related-Bug: #1560963 --- neutron_lib/tests/unit/utils/test_helpers.py | 13 +++++++++++ neutron_lib/utils/helpers.py | 23 +++++++++++++++++++ .../add-compare-dict-315a66a65cbce6fd.yaml | 6 +++++ 3 files changed, 42 insertions(+) create mode 100644 releasenotes/notes/add-compare-dict-315a66a65cbce6fd.yaml diff --git a/neutron_lib/tests/unit/utils/test_helpers.py b/neutron_lib/tests/unit/utils/test_helpers.py index 4e7b0503f..1318c36a5 100644 --- a/neutron_lib/tests/unit/utils/test_helpers.py +++ b/neutron_lib/tests/unit/utils/test_helpers.py @@ -90,6 +90,19 @@ class TestCompareElements(base.BaseTestCase): ['juve', 'napoli'])) +class TestCompare(base.BaseTestCase): + + def test_compare(self): + self.assertTrue(helpers.compare_dict({'a': 2}, {'a': 2})) + self.assertFalse(helpers.compare_dict({'a': 2}, {'a': 3})) + self.assertTrue(helpers.compare_dict({'a': [1, 2]}, {'a': [2, 1]})) + self.assertFalse(helpers.compare_dict({'a': [1, 2]}, {'a': [1]})) + self.assertTrue(helpers.compare_dict({'a': 'value1'}, {'a': 'value1'})) + self.assertFalse(helpers.compare_dict({'a': 'value'}, {'a': 'value1'})) + self.assertFalse(helpers.compare_dict({'a': '1'}, + {'a': '1', 'b': '2'})) + + class TestDictUtils(base.BaseTestCase): def test_dict2str(self): diff --git a/neutron_lib/utils/helpers.py b/neutron_lib/utils/helpers.py index 5f999c972..3b921e1e3 100644 --- a/neutron_lib/utils/helpers.py +++ b/neutron_lib/utils/helpers.py @@ -74,6 +74,29 @@ def compare_elements(a, b): return set(a or []) == set(b or []) +def compare_dict(a, b): + """Compare two dicts, including keys and values. + + Useful because dict comparison was removed in Python 3.0.1. + + :param a: The first dict to compare. + :param b: The second dict to compare. + :returns: True if a and b have the same keys and values, False otherwise. + """ + if sorted(list(a.keys())) != sorted(list(b.keys())): + return False + for key, value in a.items(): + if not isinstance(value, type(b[key])): + return False + if isinstance(value, dict) and not compare_dict(value, b[key]): + return False + if isinstance(value, list) and not value.sort() == b[key].sort(): + return False + elif value != b[key]: + return False + return True + + def safe_sort_key(value): """Return value hash or build one for dictionaries. diff --git a/releasenotes/notes/add-compare-dict-315a66a65cbce6fd.yaml b/releasenotes/notes/add-compare-dict-315a66a65cbce6fd.yaml new file mode 100644 index 000000000..476e758fa --- /dev/null +++ b/releasenotes/notes/add-compare-dict-315a66a65cbce6fd.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + Introduced ``compare_dict`` function in ``neutron_lib.utils.helpers``. It + allows you to compare two dictionaries, checking both the keys and the + values they contain.