Add tests and order ignoring for datadiff
datadiff from tests now allows to ignore order of lists when comparing structures with it. Added some tests Change-Id: I6ef46f7cfe1a617f2b48add37a75202e386bcd89 Related-Bug: #1433691
This commit is contained in:
parent
1d2bd383ca
commit
acd7adbf82
|
@ -935,32 +935,37 @@ class BaseTestCase(TestCase):
|
|||
except exception:
|
||||
self.fail('Exception "{0}" raised.'.format(exception))
|
||||
|
||||
def datadiff(self, node1, node2, path=None, ignore_keys=[]):
|
||||
def datadiff(self, node1, node2, path=None, ignore_keys=[],
|
||||
compare_sorted=False):
|
||||
if path is None:
|
||||
path = []
|
||||
|
||||
print("Path: {0}".format("->".join(path)))
|
||||
def fail(msg, failed_path):
|
||||
self.fail('Path "{0}": {1}'.format("->".join(failed_path), msg))
|
||||
|
||||
if not isinstance(node1, dict) or not isinstance(node2, dict):
|
||||
if isinstance(node1, list):
|
||||
if isinstance(node1, (list, tuple)):
|
||||
newpath = path[:]
|
||||
if compare_sorted:
|
||||
node1 = sorted(node1)
|
||||
node2 = sorted(node2)
|
||||
for i, keys in enumerate(izip(node1, node2)):
|
||||
newpath.append(str(i))
|
||||
self.datadiff(keys[0], keys[1], newpath, ignore_keys)
|
||||
self.datadiff(keys[0], keys[1], newpath, ignore_keys,
|
||||
compare_sorted)
|
||||
newpath.pop()
|
||||
elif node1 != node2:
|
||||
err = "Values differ: {0} != {1}".format(
|
||||
str(node1),
|
||||
str(node2)
|
||||
)
|
||||
raise Exception(err)
|
||||
fail(err, path)
|
||||
else:
|
||||
newpath = path[:]
|
||||
|
||||
if len(node1) != len(node2):
|
||||
raise Exception(
|
||||
'Nodes have different keys number: {0} != {1}'.format(
|
||||
len(node1), len(node2)))
|
||||
fail('Nodes have different keys number: {0} != {1}'.format(
|
||||
len(node1), len(node2)), path)
|
||||
|
||||
for key1, key2 in zip(
|
||||
sorted(node1),
|
||||
|
@ -971,11 +976,12 @@ class BaseTestCase(TestCase):
|
|||
str(key1),
|
||||
str(key2)
|
||||
)
|
||||
raise Exception(err)
|
||||
fail(err, path)
|
||||
if key1 in ignore_keys:
|
||||
continue
|
||||
newpath.append(key1)
|
||||
self.datadiff(node1[key1], node2[key2], newpath, ignore_keys)
|
||||
self.datadiff(node1[key1], node2[key2], newpath, ignore_keys,
|
||||
compare_sorted)
|
||||
newpath.pop()
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,70 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2015 Mirantis, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
import copy
|
||||
|
||||
from nailgun.test import base
|
||||
|
||||
|
||||
class TestDataDiff(base.BaseTestCase):
|
||||
def setUp(self):
|
||||
super(TestDataDiff, self).setUp()
|
||||
|
||||
self.dict_a = {
|
||||
'a': '1',
|
||||
'b': {
|
||||
'c': 'string',
|
||||
'd': {
|
||||
'e': (1, 2, 3),
|
||||
'f': [4, 5, 6],
|
||||
},
|
||||
},
|
||||
}
|
||||
self.dict_b = copy.deepcopy(self.dict_a)
|
||||
|
||||
def test_compare_dict(self):
|
||||
self.datadiff(self.dict_a, self.dict_b)
|
||||
|
||||
def test_compare_different_values(self):
|
||||
self.dict_b['b']['c'] = 'different-string'
|
||||
with self.assertRaisesRegexp(AssertionError, 'Values differ'):
|
||||
self.datadiff(self.dict_a, self.dict_b)
|
||||
|
||||
def test_compare_different_key_number(self):
|
||||
self.dict_b['g'] = 'extra-key'
|
||||
with self.assertRaisesRegexp(AssertionError,
|
||||
'Nodes have different keys number'):
|
||||
self.datadiff(self.dict_a, self.dict_b)
|
||||
|
||||
def test_compare_different_same_key_number(self):
|
||||
del self.dict_b['a']
|
||||
self.dict_b['g'] = 'extra-key'
|
||||
with self.assertRaisesRegexp(AssertionError, 'Keys differ'):
|
||||
self.datadiff(self.dict_a, self.dict_b)
|
||||
|
||||
def test_compare_different_dicts_with_ignore(self):
|
||||
self.dict_b['b']['c'] = 'different-string'
|
||||
self.datadiff(self.dict_a, self.dict_b, ignore_keys=('c', ))
|
||||
|
||||
def test_compare_lists_with_different_order(self):
|
||||
self.dict_b['b']['d']['e'] = self.dict_b['b']['d']['e'][::-1]
|
||||
self.dict_b['b']['d']['f'] = self.dict_b['b']['d']['f'][::-1]
|
||||
with self.assertRaisesRegexp(AssertionError, 'Values differ'):
|
||||
self.datadiff(self.dict_a, self.dict_b)
|
||||
|
||||
def test_compare_sorted_lists(self):
|
||||
self.dict_b['b']['d']['e'] = self.dict_b['b']['d']['e'][::-1]
|
||||
self.dict_b['b']['d']['f'] = self.dict_b['b']['d']['f'][::-1]
|
||||
self.datadiff(self.dict_a, self.dict_b, compare_sorted=True)
|
Loading…
Reference in New Issue