Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow specifying multiple possible vector layer results for processing
tests

Some algorithms are non-deterministic and the results may vary from
run to run. In this case we allow specifying multiple possible valid
results, and the test will pass if the result layer matches any of these.
  • Loading branch information
nyalldawson committed Aug 5, 2017
1 parent e8d667c commit 9968962
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions python/plugins/processing/tests/AlgorithmsTestBase.py
Expand Up @@ -43,6 +43,7 @@

from osgeo.gdalconst import GA_ReadOnly
from numpy import nan_to_num
from copy import deepcopy

import processing

Expand Down Expand Up @@ -186,7 +187,10 @@ def load_result_param(self, param):
if param['type'] in ['vector', 'file', 'table', 'regex']:
outdir = tempfile.mkdtemp()
self.cleanup_paths.append(outdir)
basename = os.path.basename(param['name'])
if isinstance(param['name'], str):
basename = os.path.basename(param['name'])
else:
basename = os.path.basename(param['name'][0])
filepath = os.path.join(outdir, basename)
return filepath
elif param['type'] == 'rasterhash':
Expand All @@ -198,6 +202,19 @@ def load_result_param(self, param):

raise KeyError("Unknown type '{}' specified for parameter".format(param['type']))

def load_layers(self, id, param):
layers = []
if param['type'] in ('vector', 'table') and isinstance(param['name'], str):
layers.append(self.load_layer(id, param))
elif param['type'] in ('vector', 'table'):
for n in param['name']:
layer_param = deepcopy(param)
layer_param['name'] = n
layers.append(self.load_layer(id, layer_param))
else:
layers.append(self.load_layer(id, param))
return layers

def load_layer(self, id, param):
"""
Loads a layer which was specified as parameter.
Expand Down Expand Up @@ -253,7 +270,7 @@ def check_results(self, results, context, params, expected):
self.assertTrue(result_lyr.isValid())
continue

expected_lyr = self.load_layer(id, expected_result)
expected_lyrs = self.load_layers(id, expected_result)
if 'in_place_result' in expected_result:
result_lyr = QgsProcessingUtils.mapLayerFromString(self.in_place_layers[id], context)
self.assertTrue(result_lyr.isValid(), self.in_place_layers[id])
Expand All @@ -271,7 +288,15 @@ def check_results(self, results, context, params, expected):

compare = expected_result.get('compare', {})

self.assertLayersEqual(expected_lyr, result_lyr, compare=compare)
if len(expected_lyrs) == 1:
self.assertLayersEqual(expected_lyrs[0], result_lyr, compare=compare)
else:
res = False
for l in expected_lyrs:
if self.checkLayersEqual(l, result_lyr, compare=compare):
res = True
break
self.assertTrue(res, 'Could not find matching layer in expected results')

elif 'rasterhash' == expected_result['type']:
print("id:{} result:{}".format(id, results[id]))
Expand Down

0 comments on commit 9968962

Please sign in to comment.