Allow testing of layer equality without throwing asserts

Sometimes in tests it's required to check for layer equality without
aborting in case of mismatches
This commit is contained in:
Nyall Dawson 2017-08-04 19:07:03 +10:00
parent 5d635d190d
commit e8d667cac3

View File

@ -55,6 +55,21 @@ class TestCase(_TestCase):
{ fields: { a: skip, b: { precision: 2 }, geometry: { precision: 5 } } { fields: { a: skip, b: { precision: 2 }, geometry: { precision: 5 } }
{ fields: { __all__: cast( str ) } } { fields: { __all__: cast( str ) } }
""" """
self.checkLayersEqual(layer_expected, layer_result, True, **kwargs)
def checkLayersEqual(self, layer_expected, layer_result, use_asserts=False, **kwargs):
"""
:param layer_expected: The first layer to compare
:param layer_result: The second layer to compare
:param use_asserts: If true, asserts are used to test conditions, if false, asserts
are not used and the function will only return False if the test fails
:param request: Optional, A feature request. This can be used to specify
an order by clause to make sure features are compared in
a given sequence if they don't match by default.
:keyword compare: A map of comparison options. e.g.
{ fields: { a: skip, b: { precision: 2 }, geometry: { precision: 5 } }
{ fields: { __all__: cast( str ) } }
"""
try: try:
request = kwargs['request'] request = kwargs['request']
@ -67,10 +82,16 @@ class TestCase(_TestCase):
compare = {} compare = {}
# Compare CRS # Compare CRS
if use_asserts:
_TestCase.assertEqual(self, layer_expected.dataProvider().crs().authid(), layer_result.dataProvider().crs().authid()) _TestCase.assertEqual(self, layer_expected.dataProvider().crs().authid(), layer_result.dataProvider().crs().authid())
elif not layer_expected.dataProvider().crs().authid() == layer_result.dataProvider().crs().authid():
return False
# Compare features # Compare features
if use_asserts:
_TestCase.assertEqual(self, layer_expected.featureCount(), layer_result.featureCount()) _TestCase.assertEqual(self, layer_expected.featureCount(), layer_result.featureCount())
elif layer_expected.featureCount() != layer_result.featureCount():
return False
try: try:
precision = compare['geometry']['precision'] precision = compare['geometry']['precision']
@ -89,6 +110,7 @@ class TestCase(_TestCase):
geom1 = feats[1].geometry().geometry().asWkt(precision) geom1 = feats[1].geometry().geometry().asWkt(precision)
else: else:
geom1 = None geom1 = None
if use_asserts:
_TestCase.assertEqual( _TestCase.assertEqual(
self, self,
geom0, geom0,
@ -100,6 +122,8 @@ class TestCase(_TestCase):
geom1 geom1
) )
) )
elif geom0 != geom1:
return False
for attr_expected, field_expected in zip(feats[0].attributes(), layer_expected.fields().toList()): for attr_expected, field_expected in zip(feats[0].attributes(), layer_expected.fields().toList()):
try: try:
@ -134,6 +158,7 @@ class TestCase(_TestCase):
attr_expected = round(attr_expected, cmp['precision']) attr_expected = round(attr_expected, cmp['precision'])
attr_result = round(attr_result, cmp['precision']) attr_result = round(attr_result, cmp['precision'])
if use_asserts:
_TestCase.assertEqual( _TestCase.assertEqual(
self, self,
attr_expected, attr_expected,
@ -149,6 +174,10 @@ class TestCase(_TestCase):
repr(attr_result) repr(attr_result)
) )
) )
elif attr_expected != attr_result:
return False
return True
def assertFilesEqual(self, filepath_expected, filepath_result): def assertFilesEqual(self, filepath_expected, filepath_result):
with open(filepath_expected, 'r') as file_expected: with open(filepath_expected, 'r') as file_expected: