topotest: always show diff on json_cmp failure

Append diffs on all json_cmp failures so the reader can compare JSON
field differences.
This commit is contained in:
Rafael Zalamena 2017-11-09 18:10:54 -02:00 committed by Donald Sharp
parent 479b98aacd
commit 08533b7bce

View File

@ -92,21 +92,22 @@ def json_cmp(d1, d2):
s2_req = set([key for key in nd2 if nd2[key] is not None]) s2_req = set([key for key in nd2 if nd2[key] is not None])
diff = s2_req - s1 diff = s2_req - s1
if diff != set({}): if diff != set({}):
result.add_error('expected key(s) {} in {} (have {})'.format( result.add_error('expected key(s) {} in {} (have {}):\n{}'.format(
str(list(diff)), parent, str(list(s1)))) str(list(diff)), parent, str(list(s1)), json_diff(nd1, nd2)))
for key in s2.intersection(s1): for key in s2.intersection(s1):
# Test for non existence of key in d2 # Test for non existence of key in d2
if nd2[key] is None: if nd2[key] is None:
result.add_error('"{}" should not exist in {} (have {})'.format( result.add_error('"{}" should not exist in {} (have {}):\n{}'.format(
key, parent, str(s1))) key, parent, str(s1), json_diff(nd1[key], nd2[key])))
continue continue
# If nd1 key is a dict, we have to recurse in it later. # If nd1 key is a dict, we have to recurse in it later.
if isinstance(nd2[key], type({})): if isinstance(nd2[key], type({})):
if not isinstance(nd1[key], type({})): if not isinstance(nd1[key], type({})):
result.add_error( result.add_error(
'{}["{}"] has different type than expected '.format(parent, key) + '{}["{}"] has different type than expected '.format(parent, key) +
'(have {}, expected {})'.format(type(nd1[key]), type(nd2[key]))) '(have {}, expected {}):\n{}'.format(
type(nd1[key]), type(nd2[key]), json_diff(nd1[key], nd2[key])))
continue continue
nparent = '{}["{}"]'.format(parent, key) nparent = '{}["{}"]'.format(parent, key)
squeue.append((nd1[key], nd2[key], nparent)) squeue.append((nd1[key], nd2[key], nparent))
@ -116,7 +117,8 @@ def json_cmp(d1, d2):
if not isinstance(nd1[key], type([])): if not isinstance(nd1[key], type([])):
result.add_error( result.add_error(
'{}["{}"] has different type than expected '.format(parent, key) + '{}["{}"] has different type than expected '.format(parent, key) +
'(have {}, expected {})'.format(type(nd1[key]), type(nd2[key]))) '(have {}, expected {}):\n{}'.format(
type(nd1[key]), type(nd2[key]), json_diff(nd1[key], nd2[key])))
continue continue
# Check list size # Check list size
if len(nd2[key]) > len(nd1[key]): if len(nd2[key]) > len(nd1[key]):