Skip to content

Commit 9968962

Browse files
committed
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.
1 parent e8d667c commit 9968962

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

python/plugins/processing/tests/AlgorithmsTestBase.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
from osgeo.gdalconst import GA_ReadOnly
4545
from numpy import nan_to_num
46+
from copy import deepcopy
4647

4748
import processing
4849

@@ -186,7 +187,10 @@ def load_result_param(self, param):
186187
if param['type'] in ['vector', 'file', 'table', 'regex']:
187188
outdir = tempfile.mkdtemp()
188189
self.cleanup_paths.append(outdir)
189-
basename = os.path.basename(param['name'])
190+
if isinstance(param['name'], str):
191+
basename = os.path.basename(param['name'])
192+
else:
193+
basename = os.path.basename(param['name'][0])
190194
filepath = os.path.join(outdir, basename)
191195
return filepath
192196
elif param['type'] == 'rasterhash':
@@ -198,6 +202,19 @@ def load_result_param(self, param):
198202

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

205+
def load_layers(self, id, param):
206+
layers = []
207+
if param['type'] in ('vector', 'table') and isinstance(param['name'], str):
208+
layers.append(self.load_layer(id, param))
209+
elif param['type'] in ('vector', 'table'):
210+
for n in param['name']:
211+
layer_param = deepcopy(param)
212+
layer_param['name'] = n
213+
layers.append(self.load_layer(id, layer_param))
214+
else:
215+
layers.append(self.load_layer(id, param))
216+
return layers
217+
201218
def load_layer(self, id, param):
202219
"""
203220
Loads a layer which was specified as parameter.
@@ -253,7 +270,7 @@ def check_results(self, results, context, params, expected):
253270
self.assertTrue(result_lyr.isValid())
254271
continue
255272

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

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

274-
self.assertLayersEqual(expected_lyr, result_lyr, compare=compare)
291+
if len(expected_lyrs) == 1:
292+
self.assertLayersEqual(expected_lyrs[0], result_lyr, compare=compare)
293+
else:
294+
res = False
295+
for l in expected_lyrs:
296+
if self.checkLayersEqual(l, result_lyr, compare=compare):
297+
res = True
298+
break
299+
self.assertTrue(res, 'Could not find matching layer in expected results')
275300

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

0 commit comments

Comments
 (0)