Skip to content

Commit d4ad063

Browse files
committedAug 5, 2017
Allow specifying a 'primary key' field when comparing layers for
processing tests Some algorithms will return results in different orders, e.g. due to the use of dicts or other methods which do not guarantee a fixed return order. Using a primary key to do the feature match allows us to flexibly handle these situations and provide tests for these algorithms.
1 parent 6aa672d commit d4ad063

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed
 

‎python/plugins/processing/tests/AlgorithmsTestBase.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,14 @@ def check_results(self, results, context, params, expected):
287287
self.assertTrue(result_lyr, results[id])
288288

289289
compare = expected_result.get('compare', {})
290+
pk = expected_result.get('pk', None)
290291

291292
if len(expected_lyrs) == 1:
292-
self.assertLayersEqual(expected_lyrs[0], result_lyr, compare=compare)
293+
self.assertLayersEqual(expected_lyrs[0], result_lyr, compare=compare, pk=pk)
293294
else:
294295
res = False
295296
for l in expected_lyrs:
296-
if self.checkLayersEqual(l, result_lyr, compare=compare):
297+
if self.checkLayersEqual(l, result_lyr, compare=compare, pk=pk):
297298
res = True
298299
break
299300
self.assertTrue(res, 'Could not find matching layer in expected results')

‎python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2501,6 +2501,10 @@ tests:
25012501
OUTPUT:
25022502
name: expected/stats_by_category.gml
25032503
type: vector
2504+
pk: id2
2505+
compare:
2506+
fields:
2507+
fid: skip
25042508

25052509
# - algorithm: qgis:zonalstatistics
25062510
# name: simple zonal statistics

‎python/testing/__init__.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ def assertLayersEqual(self, layer_expected, layer_result, **kwargs):
5454
:keyword compare: A map of comparison options. e.g.
5555
{ fields: { a: skip, b: { precision: 2 }, geometry: { precision: 5 } }
5656
{ fields: { __all__: cast( str ) } }
57+
:keyword pk: "Primary key" type field - used to match features
58+
from the expected table to their corresponding features in the result table. If not specified
59+
features are compared by their order in the layer (e.g. first feature compared with first feature,
60+
etc)
5761
"""
5862
self.checkLayersEqual(layer_expected, layer_result, True, **kwargs)
5963

@@ -69,6 +73,10 @@ def checkLayersEqual(self, layer_expected, layer_result, use_asserts=False, **kw
6973
:keyword compare: A map of comparison options. e.g.
7074
{ fields: { a: skip, b: { precision: 2 }, geometry: { precision: 5 } }
7175
{ fields: { __all__: cast( str ) } }
76+
:keyword pk: "Primary key" type field - used to match features
77+
from the expected table to their corresponding features in the result table. If not specified
78+
features are compared by their order in the layer (e.g. first feature compared with first feature,
79+
etc)
7280
"""
7381

7482
try:
@@ -98,8 +106,14 @@ def checkLayersEqual(self, layer_expected, layer_result, use_asserts=False, **kw
98106
except KeyError:
99107
precision = 14
100108

101-
expected_features = sorted(layer_expected.getFeatures(request), key=lambda f: f.id())
102-
result_features = sorted(layer_result.getFeatures(request), key=lambda f: f.id())
109+
def sort_by_pk_or_fid(f):
110+
if 'pk' in kwargs and kwargs['pk'] is not None:
111+
return f[kwargs['pk']]
112+
else:
113+
return f.id()
114+
115+
expected_features = sorted(layer_expected.getFeatures(request), key=sort_by_pk_or_fid)
116+
result_features = sorted(layer_result.getFeatures(request), key=sort_by_pk_or_fid)
103117

104118
for feats in zip(expected_features, result_features):
105119
if feats[0].hasGeometry():

0 commit comments

Comments
 (0)
Please sign in to comment.