Replace assertDictMatch with assertDictEqual

assertDictMatch was introduced for Python 2.6 only. Python 2.7+
contains assertDictEqual.

Change-Id: I4914981029995a28cffa8b39e8757f77890a3c2b
Closes-bug: #1646433
This commit is contained in:
wangxiyuan 2016-12-22 11:10:37 +08:00
parent 58956d7c86
commit 73a694ae54
2 changed files with 1 additions and 59 deletions

View File

@ -103,61 +103,3 @@ class TestCase(testtools.TestCase):
result = patcher.start()
self.addCleanup(patcher.stop)
return result
# Useful assertions
def assertDictMatch(self, d1, d2, approx_equal=False, tolerance=0.001):
"""Assert two dicts are equivalent.
This is a 'deep' match in the sense that it handles nested
dictionaries appropriately.
NOTE:
If you don't care (or don't know) a given value, you can specify
the string DONTCARE as the value. This will cause that dict-item
to be skipped.
"""
def raise_assertion(msg):
d1str = d1
d2str = d2
base_msg = ('Dictionaries do not match. %(msg)s d1: %(d1str)s '
'd2: %(d2str)s' %
{'msg': msg, 'd1str': d1str, 'd2str': d2str})
raise AssertionError(base_msg)
d1keys = set(d1.keys())
d2keys = set(d2.keys())
if d1keys != d2keys:
d1only = d1keys - d2keys
d2only = d2keys - d1keys
raise_assertion('Keys in d1 and not d2: %(d1only)s. '
'Keys in d2 and not d1: %(d2only)s' %
{'d1only': d1only, 'd2only': d2only})
for key in d1keys:
d1value = d1[key]
d2value = d2[key]
try:
error = abs(float(d1value) - float(d2value))
within_tolerance = error <= tolerance
except (ValueError, TypeError):
# If both values aren't convertible to float, just ignore
# ValueError if arg is a str, TypeError if it's something else
# (like None)
within_tolerance = False
if hasattr(d1value, 'keys') and hasattr(d2value, 'keys'):
self.assertDictMatch(d1value, d2value)
elif 'DONTCARE' in (d1value, d2value):
continue
elif approx_equal and within_tolerance:
continue
elif d1value != d2value:
raise_assertion("d1['%(key)s']=%(d1value)s != "
"d2['%(key)s']=%(d2value)s" %
{
'key': key,
'd1value': d1value,
'd2value': d2value,
})

View File

@ -99,7 +99,7 @@ class AoEConnectorTestCase(test_connector.ConnectorTestCase):
volume_info = self.connector.connect_volume(
self.connection_properties)
self.assertDictMatch(volume_info, expected_info)
self.assertDictEqual(volume_info, expected_info)
@mock.patch.object(os.path, 'exists', return_value=False)
def test_connect_volume_could_not_discover_path(self, exists_mock):