Merge "return more details on assertJsonEqual fail"
This commit is contained in:
25
nova/test.py
25
nova/test.py
@@ -289,6 +289,21 @@ class TestCase(testtools.TestCase):
|
||||
return svc.service
|
||||
|
||||
def assertJsonEqual(self, expected, observed):
|
||||
"""Asserts that 2 complex data structures are json equivalent.
|
||||
|
||||
We use data structures which serialize down to json throughout
|
||||
the code, and often times we just need to know that these are
|
||||
json equivalent. This means that list order is not important,
|
||||
and should be sorted.
|
||||
|
||||
Because this is a recursive set of assertions, when failure
|
||||
happens we want to expose both the local failure and the
|
||||
global view of the 2 data structures being compared. So a
|
||||
MismatchError which includes the inner failure as the
|
||||
mismatch, and the passed in expected / observed as matchee /
|
||||
matcher.
|
||||
|
||||
"""
|
||||
if isinstance(expected, six.string_types):
|
||||
expected = jsonutils.loads(expected)
|
||||
if isinstance(observed, six.string_types):
|
||||
@@ -325,7 +340,15 @@ class TestCase(testtools.TestCase):
|
||||
else:
|
||||
self.assertEqual(expected, observed)
|
||||
|
||||
inner(expected, observed)
|
||||
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)
|
||||
|
||||
def assertPublicAPISignatures(self, baseinst, inst):
|
||||
def get_public_apis(inst):
|
||||
|
@@ -19,6 +19,7 @@
|
||||
from oslo_config import cfg
|
||||
from oslo_log import log as logging
|
||||
import oslo_messaging as messaging
|
||||
import six
|
||||
|
||||
from nova import rpc
|
||||
from nova import test
|
||||
@@ -87,6 +88,66 @@ class JsonTestCase(test.TestCase):
|
||||
}"""
|
||||
self.assertJsonEqual(expected, observed)
|
||||
|
||||
def test_json_equal_fail_on_length(self):
|
||||
expected = {
|
||||
'top': {
|
||||
'l1': {
|
||||
'l2': ['a', 'b', 'c']
|
||||
}
|
||||
}
|
||||
}
|
||||
observed = {
|
||||
'top': {
|
||||
'l1': {
|
||||
'l2': ['c', 'a', 'b', 'd']
|
||||
}
|
||||
}
|
||||
}
|
||||
try:
|
||||
self.assertJsonEqual(expected, observed)
|
||||
except Exception as e:
|
||||
# error reported is going to be a cryptic length failure
|
||||
# on the level2 structure.
|
||||
self.assertEqual(e.mismatch.describe(), "3 != 4")
|
||||
self.assertIn(
|
||||
"Matchee: {'top': {'l1': {'l2': ['c', 'a', 'b', 'd']}}}",
|
||||
six.text_type(e))
|
||||
self.assertIn(
|
||||
"Matcher: {'top': {'l1': {'l2': ['a', 'b', 'c']}}}",
|
||||
six.text_type(e))
|
||||
else:
|
||||
self.fail("This should have raised a mismatch exception")
|
||||
|
||||
def test_json_equal_fail_on_inner(self):
|
||||
expected = {
|
||||
'top': {
|
||||
'l1': {
|
||||
'l2': ['a', 'b', 'c']
|
||||
}
|
||||
}
|
||||
}
|
||||
observed = {
|
||||
'top': {
|
||||
'l1': {
|
||||
'l2': ['c', 'a', 'd']
|
||||
}
|
||||
}
|
||||
}
|
||||
try:
|
||||
self.assertJsonEqual(expected, observed)
|
||||
except Exception as e:
|
||||
# error reported is going to be a cryptic length failure
|
||||
# on the level2 structure.
|
||||
self.assertEqual(e.mismatch.describe(), "'b' != 'c'")
|
||||
self.assertIn(
|
||||
"Matchee: {'top': {'l1': {'l2': ['c', 'a', 'd']}}}",
|
||||
six.text_type(e))
|
||||
self.assertIn(
|
||||
"Matcher: {'top': {'l1': {'l2': ['a', 'b', 'c']}}}",
|
||||
six.text_type(e))
|
||||
else:
|
||||
self.fail("This should have raised a mismatch exception")
|
||||
|
||||
|
||||
class BadLogTestCase(test.TestCase):
|
||||
"""Make sure a mis-formatted debug log will get caught."""
|
||||
|
Reference in New Issue
Block a user