Merge "Improve assertJsonEqual error reporting"
This commit is contained in:
21
nova/test.py
21
nova/test.py
@@ -29,6 +29,7 @@ import contextlib
|
||||
import copy
|
||||
import datetime
|
||||
import inspect
|
||||
import itertools
|
||||
import os
|
||||
import pprint
|
||||
import sys
|
||||
@@ -432,7 +433,15 @@ class TestCase(testtools.TestCase):
|
||||
if isinstance(expected, dict) and isinstance(observed, dict):
|
||||
self.assertEqual(
|
||||
len(expected), len(observed),
|
||||
'path: %s. Dict lengths are not equal' % path)
|
||||
('path: %s. Different dict key sets\n'
|
||||
'expected=%s\n'
|
||||
'observed=%s\n'
|
||||
'difference=%s') %
|
||||
(path,
|
||||
sorted(expected.keys()),
|
||||
sorted(observed.keys()),
|
||||
list(set(expected.keys()).symmetric_difference(
|
||||
set(observed.keys())))))
|
||||
expected_keys = sorted(expected)
|
||||
observed_keys = sorted(observed)
|
||||
self.assertEqual(
|
||||
@@ -444,7 +453,15 @@ class TestCase(testtools.TestCase):
|
||||
isinstance(observed, (list, tuple, set))):
|
||||
self.assertEqual(
|
||||
len(expected), len(observed),
|
||||
'path: %s. List lengths are not equal' % path)
|
||||
('path: %s. Different list items\n'
|
||||
'expected=%s\n'
|
||||
'observed=%s\n'
|
||||
'difference=%s') %
|
||||
(path,
|
||||
sorted(expected, key=sort_key),
|
||||
sorted(observed, key=sort_key),
|
||||
[a for a in itertools.chain(expected, observed) if
|
||||
(a not in expected) or (a not in observed)]))
|
||||
|
||||
expected_values_iter = iter(sorted(expected, key=sort_key))
|
||||
observed_values_iter = iter(sorted(observed, key=sort_key))
|
||||
|
@@ -108,7 +108,10 @@ class JsonTestCase(test.NoDBTestCase):
|
||||
# error reported is going to be a cryptic length failure
|
||||
# on the level2 structure.
|
||||
self.assertEqual(
|
||||
"3 != 4: path: root.top.l1.l2. List lengths are not equal",
|
||||
("3 != 4: path: root.top.l1.l2. Different list items\n"
|
||||
"expected=['a', 'b', 'c']\n"
|
||||
"observed=['a', 'b', 'c', 'd']\n"
|
||||
"difference=['d']"),
|
||||
e.difference)
|
||||
self.assertIn(
|
||||
"actual:\n{'top': {'l1': {'l2': ['c', 'a', 'b', 'd']}}}",
|
||||
@@ -138,7 +141,10 @@ class JsonTestCase(test.NoDBTestCase):
|
||||
self.assertJsonEqual(expected, actual)
|
||||
except Exception as e:
|
||||
self.assertEqual(
|
||||
"3 != 2: path: root.top.l1.l2. Dict lengths are not equal",
|
||||
("3 != 2: path: root.top.l1.l2. Different dict key sets\n"
|
||||
"expected=['a', 'b', 'c']\n"
|
||||
"observed=['a', 'b']\n"
|
||||
"difference=['c']"),
|
||||
e.difference)
|
||||
else:
|
||||
self.fail("This should have raised a mismatch exception")
|
||||
|
Reference in New Issue
Block a user