I have two functions that must return the same dictionary (one is the old method and the other one, is a new method that will replace the old one). As I am testing that the new function gives me the same result as the old one. I am trying to create a program that takes as input, two dictionaries and verifies that they have the same information and tells me any differences between them. My scope is to understand which info of the new function is wrong.
Those two dictionaries can change each time. They could have keys that contains just a string or a dictionary or a list of dictionaries. Also, those list of dictionaries could contain keys that have string values or a dictionary or another list of dictionaries. The dictionaries can arbitrarily complex.
I have tried this, but the result is not good.
attr_prod
is an example of the dictionary that returns the old function.
attr_test
is an example of the dictionary that returns the new function.
def main():
attr_prod = {
'key1': 'a',
'key2': [ {
'key3': 'n1',
'key4': 'n2',
'key5': [ {
'key6': 'n1',
'key7': 'n2',
'key8': {
'key10': 'g1',
'key11': 'g2',
'key12': 'g3'
}
},
{
'key6': 'n5',
'key7': 'n6',
'key8': {
'key10': 'g4',
'key11': 'g5',
'key12': 'g6'
}
}
]
}
],
'key20': {
'key21': 'g4',
'key22': 'g5',
'key23': 'g6'
}
}
attr_test = {
'key1': 'a',
'key2': [ {
'key3': 'different info',
'key4': 'n2',
'key5': [ {
'key6': 'n1',
'key7': 'n2',
'key8': {
'key10': 'g1',
'key11': 'g2',
'key12': 'g3'
}
},
{
'key6': 'n5',
'key7': 'n6',
'key8': {
'key10': 'different info',
'key11': 'g5',
'key12': 'g6'
}
},
{
'key6': 'this is new',
'key7': 'this is new',
'key8': {
'key10': 'new',
'key11': 'new',
'key12': 'new'
}
}
]
}
],
'key20': {
'key21': 'g4',
'key22': 'different info',
'key23': 'g6'
}
}
for key, value_prod in attr_prod.items():
if isinstance(value_prod, basestring):
check(key, value_prod, attr_test[key])
elif isinstance(value_prod, list):
n_rec = 0
for rec in value_prod:
# if isinstance(rec, basestring):
# check(key, rec, attr_test[key][n_rec])
print rec
if isinstance(rec, dict):
for key2, value_prod2 in rec.items():
if isinstance(value_prod2, basestring):
check(key, value_prod2, attr_test[key][n_rec][key2])
elif isinstance(value_prod2, list):
n_rec2 = 0
for rec2 in value_prod2:
for key3, value_prod3 in rec2.items():
if isinstance(value_prod3, basestring):
check(key, value_prod3, attr_test[key][n_rec][key2][n_rec2][key3])
n_rec2 += 1
n_rec += 1
def check(key, value_prod, value_test):
if value_test != value_prod:
print "KO key: %s test: -%s- prod: -%s-" % (key, value_test, value_prod)