Skip to content

Commit 20fc30b

Browse files
committed
Add file comparison to processing tests
And a test for basic numeric statistics
1 parent f699564 commit 20fc30b

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

python/plugins/processing/tests/AlgorithmsTest.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ def load_param(self, param):
137137

138138
def load_result_param(self, param):
139139
"""
140-
Lodas a result parameter. Creates a temporary destination where the result should go to and returns this location
140+
Loads a result parameter. Creates a temporary destination where the result should go to and returns this location
141141
so it can be sent to the algorithm as parameter.
142142
"""
143-
if param['type'] == 'vector':
143+
if param['type'] in ['vector', 'file']:
144144
outdir = tempfile.mkdtemp()
145145
self.cleanup_paths.append(outdir)
146146
basename = os.path.basename(param['name'])
@@ -153,14 +153,7 @@ def load_layer(self, param):
153153
"""
154154
Loads a layer which was specified as parameter.
155155
"""
156-
prefix = processingTestDataPath()
157-
try:
158-
if param['location'] == 'qgs':
159-
prefix = unitTestDataPath()
160-
except KeyError:
161-
pass
162-
163-
filepath = os.path.join(prefix, param['name'])
156+
filepath = self.filepath_from_param(param)
164157

165158
if param['type'] == 'vector':
166159
lyr = QgsVectorLayer(filepath, param['name'], 'ogr')
@@ -171,6 +164,16 @@ def load_layer(self, param):
171164
QgsMapLayerRegistry.instance().addMapLayer(lyr)
172165
return lyr
173166

167+
def filepath_from_param(self, param):
168+
"""
169+
Creates a filepath from a param
170+
"""
171+
prefix = processingTestDataPath()
172+
if 'location' in param and param['location'] == 'qgs':
173+
prefix = unitTestDataPath()
174+
175+
return os.path.join(prefix, param['name'])
176+
174177
def check_results(self, results, expected):
175178
"""
176179
Checks if result produced by an algorithm matches with the expected specification.
@@ -197,6 +200,11 @@ def check_results(self, results, expected):
197200
strhash = hashlib.sha224(dataset.ReadAsArray(0).data).hexdigest()
198201

199202
self.assertEqual(strhash, expected_result['hash'])
203+
elif 'file' == expected_result['type']:
204+
expected_filepath = self.filepath_from_param(expected_result)
205+
result_filepath = results[id]
206+
207+
self.assertFilesEqual(expected_filepath, result_filepath)
200208

201209

202210
if __name__ == '__main__':

python/plugins/processing/tests/testdata/algorithm_tests.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,13 @@ tests:
6363
OUTPUT:
6464
name: expected/polys_to_lines.gml
6565
type: vector
66+
- algorithm: qgis:basicstatisticsfornumericfields
67+
name: Test (qgis:basicstatisticsfornumericfields)
68+
params:
69+
- name: multipolys.gml
70+
type: vector
71+
- 'Bfloatval'
72+
results:
73+
OUTPUT_HTML_FILE:
74+
name: expected/basic_statistics_numeric_float.html
75+
type: file
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<html><head>
2+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>
3+
<p>Analyzed layer: multipolys.gml</p>
4+
<p>Analyzed field: Bfloatval</p>
5+
<p>Count: 3</p>
6+
<p>Unique values: 3</p>
7+
<p>Minimum value: -0.123</p>
8+
<p>Maximum value: 0.123</p>
9+
<p>Range: 0.246</p>
10+
<p>Sum: 0.0</p>
11+
<p>Mean value: 0.0</p>
12+
<p>Median value: 0.0</p>
13+
<p>Standard deviation: 0.100429079454</p>
14+
<p>Coefficient of Variation: 0</p>
15+
<p>Minority (rarest occurring value): -0.123</p>
16+
<p>Majority (most frequently occurring value): -0.123</p>
17+
<p>First quartile: -0.0615</p>
18+
<p>Third quartile: 0.0615</p>
19+
<p>NULL (missed) values: 1</p>
20+
<p>Interquartile Range (IQR): 0.123</p>
21+
</body></html>

0 commit comments

Comments
 (0)