Added assertJsonEqual method to TestCase class
new assertion method allows to compare 2 objects, ignoring order or nested collections element, 'expected' and 'observed' also could be json-strings Co-Authored-By: Timofey Durakov <tdurakov@mirantis.com> Change-Id: I9a16805b0c580caea200509564b7710116c4ac4e
This commit is contained in:
parent
c4726e1960
commit
4e0d61feab
43
nova/test.py
43
nova/test.py
|
@ -20,7 +20,7 @@ Allows overriding of flags for use of fakes, and some black magic for
|
|||
inline callbacks.
|
||||
|
||||
"""
|
||||
|
||||
import datetime
|
||||
import eventlet
|
||||
eventlet.monkey_patch(os=False)
|
||||
|
||||
|
@ -36,6 +36,7 @@ from oslo_config import cfg
|
|||
from oslo_config import fixture as config_fixture
|
||||
from oslo_log.fixture import logging_error as log_fixture
|
||||
from oslo_log import log as logging
|
||||
from oslo_serialization import jsonutils
|
||||
from oslo_utils import timeutils
|
||||
from oslotest import moxstubout
|
||||
import six
|
||||
|
@ -270,6 +271,46 @@ class TestCase(testtools.TestCase):
|
|||
nova_fixtures.ServiceFixture(name, host, **kwargs))
|
||||
return svc.service
|
||||
|
||||
def assertJsonEqual(self, expected, observed):
|
||||
if isinstance(expected, six.string_types):
|
||||
expected = jsonutils.loads(expected)
|
||||
if isinstance(observed, six.string_types):
|
||||
observed = jsonutils.loads(observed)
|
||||
|
||||
def sort(what):
|
||||
return sorted(what,
|
||||
key=lambda x: str(x) if isinstance(
|
||||
x, set) or isinstance(x,
|
||||
datetime.datetime) else x)
|
||||
|
||||
def inner(expected, observed):
|
||||
if isinstance(expected, dict) and isinstance(observed, dict):
|
||||
self.assertEqual(len(expected), len(observed))
|
||||
expected_keys = sorted(expected.iterkeys())
|
||||
observed_keys = sorted(expected.iterkeys())
|
||||
self.assertEqual(expected_keys, observed_keys)
|
||||
|
||||
expected_values_iter = iter(sort(expected.values()))
|
||||
observed_values_iter = iter(sort(observed.values()))
|
||||
|
||||
for i in range(len(expected)):
|
||||
inner(expected_values_iter.next(),
|
||||
observed_values_iter.next())
|
||||
elif (isinstance(expected, (list, tuple, set)) and
|
||||
isinstance(observed, (list, tuple, set))):
|
||||
self.assertEqual(len(expected), len(observed))
|
||||
|
||||
expected_values_iter = iter(sort(expected))
|
||||
observed_values_iter = iter(sort(observed))
|
||||
|
||||
for i in range(len(expected)):
|
||||
inner(expected_values_iter.next(),
|
||||
observed_values_iter.next())
|
||||
else:
|
||||
self.assertEqual(expected, observed)
|
||||
|
||||
inner(expected, observed)
|
||||
|
||||
def assertPublicAPISignatures(self, baseinst, inst):
|
||||
def get_public_apis(inst):
|
||||
methods = {}
|
||||
|
|
|
@ -53,6 +53,41 @@ class IsolationTestCase(test.TestCase):
|
|||
server.start()
|
||||
|
||||
|
||||
class JsonTestCase(test.TestCase):
|
||||
def test_json_equal(self):
|
||||
expected = {
|
||||
"employees": [
|
||||
{"firstName": "Anna", "lastName": "Smith"},
|
||||
{"firstName": "John", "lastName": "Doe"},
|
||||
{"firstName": "Peter", "lastName": "Jones"}
|
||||
],
|
||||
"locations": set(['Boston', 'Mumbai', 'Beijing', 'Perth'])
|
||||
}
|
||||
observed = """{
|
||||
"employees": [
|
||||
{
|
||||
"lastName": "Doe",
|
||||
"firstName": "John"
|
||||
},
|
||||
{
|
||||
"lastName": "Smith",
|
||||
"firstName": "Anna"
|
||||
},
|
||||
{
|
||||
"lastName": "Jones",
|
||||
"firstName": "Peter"
|
||||
}
|
||||
],
|
||||
"locations": [
|
||||
"Perth",
|
||||
"Boston",
|
||||
"Mumbai",
|
||||
"Beijing"
|
||||
]
|
||||
}"""
|
||||
self.assertJsonEqual(expected, observed)
|
||||
|
||||
|
||||
class BadLogTestCase(test.TestCase):
|
||||
"""Make sure a mis-formatted debug log will get caught."""
|
||||
|
||||
|
|
Loading…
Reference in New Issue