Skip to content
Browse files
Allow specifying multiple possible vector layer results for processing

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 9968962ab925474da97218eede9162106eb84d41
Showing with 28 additions and 3 deletions.
  1. +28 −3 python/plugins/processing/tests/
@@ -43,6 +43,7 @@

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

import processing

@@ -186,7 +187,10 @@ def load_result_param(self, param):
if param['type'] in ['vector', 'file', 'table', 'regex']:
outdir = tempfile.mkdtemp()
basename = os.path.basename(param['name'])
if isinstance(param['name'], str):
basename = os.path.basename(param['name'])
basename = os.path.basename(param['name'][0])
filepath = os.path.join(outdir, basename)
return filepath
elif param['type'] == 'rasterhash':
@@ -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))
layers.append(self.load_layer(id, param))
return layers

def load_layer(self, id, param):
Loads a layer which was specified as parameter.
@@ -253,7 +270,7 @@ def check_results(self, results, context, params, expected):

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])
@@ -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)
res = False
for l in expected_lyrs:
if self.checkLayersEqual(l, result_lyr, compare=compare):
res = True
self.assertTrue(res, 'Could not find matching layer in expected results')

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

0 comments on commit 9968962

Please sign in to comment.