Merge "Tests: improve assertJsonEqual diagnostic message"
This commit is contained in:
commit
2a1aea3322
43
nova/test.py
43
nova/test.py
@ -30,6 +30,7 @@ import copy
|
||||
import inspect
|
||||
import mock
|
||||
import os
|
||||
import pprint
|
||||
|
||||
import fixtures
|
||||
from oslo_cache import core as cache
|
||||
@ -358,7 +359,7 @@ class TestCase(testtools.TestCase):
|
||||
|
||||
return svc.service
|
||||
|
||||
def assertJsonEqual(self, expected, observed):
|
||||
def assertJsonEqual(self, expected, observed, message=''):
|
||||
"""Asserts that 2 complex data structures are json equivalent.
|
||||
|
||||
We use data structures which serialize down to json throughout
|
||||
@ -388,37 +389,49 @@ class TestCase(testtools.TestCase):
|
||||
return sorted(items)
|
||||
return x
|
||||
|
||||
def inner(expected, observed):
|
||||
def inner(expected, observed, path='root'):
|
||||
if isinstance(expected, dict) and isinstance(observed, dict):
|
||||
self.assertEqual(len(expected), len(observed))
|
||||
self.assertEqual(
|
||||
len(expected), len(observed),
|
||||
'path: %s. Dict lengths are not equal' % path)
|
||||
expected_keys = sorted(expected)
|
||||
observed_keys = sorted(observed)
|
||||
self.assertEqual(expected_keys, observed_keys)
|
||||
|
||||
self.assertEqual(
|
||||
expected_keys, observed_keys,
|
||||
'path: %s. Dict keys are not equal' % path)
|
||||
for key in list(six.iterkeys(expected)):
|
||||
inner(expected[key], observed[key])
|
||||
inner(expected[key], observed[key], path + '.%s' % key)
|
||||
elif (isinstance(expected, (list, tuple, set)) and
|
||||
isinstance(observed, (list, tuple, set))):
|
||||
self.assertEqual(len(expected), len(observed))
|
||||
self.assertEqual(
|
||||
len(expected), len(observed),
|
||||
'path: %s. List lengths are not equal' % path)
|
||||
|
||||
expected_values_iter = iter(sorted(expected, key=sort_key))
|
||||
observed_values_iter = iter(sorted(observed, key=sort_key))
|
||||
|
||||
for i in range(len(expected)):
|
||||
inner(next(expected_values_iter),
|
||||
next(observed_values_iter))
|
||||
next(observed_values_iter), path + '[%s]' % i)
|
||||
else:
|
||||
self.assertEqual(expected, observed)
|
||||
self.assertEqual(expected, observed, 'path: %s' % path)
|
||||
|
||||
try:
|
||||
inner(expected, observed)
|
||||
except testtools.matchers.MismatchError as e:
|
||||
inner_mismatch = e.mismatch
|
||||
# inverting the observed / expected because testtools
|
||||
# error messages assume expected is second. Possibly makes
|
||||
# reading the error messages less confusing.
|
||||
raise testtools.matchers.MismatchError(observed, expected,
|
||||
inner_mismatch, verbose=True)
|
||||
difference = e.mismatch.describe()
|
||||
if message:
|
||||
message = 'message: %s\n' % message
|
||||
msg = "\nexpected:\n%s\nactual:\n%s\ndifference:\n%s\n%s" % (
|
||||
pprint.pformat(expected),
|
||||
pprint.pformat(observed),
|
||||
difference,
|
||||
message)
|
||||
error = AssertionError(msg)
|
||||
error.expected = expected
|
||||
error.observed = observed
|
||||
error.difference = difference
|
||||
raise error
|
||||
|
||||
def assertPublicAPISignatures(self, baseinst, inst):
|
||||
def get_public_apis(inst):
|
||||
|
Loading…
x
Reference in New Issue
Block a user