Skip to content

Commit

Permalink
Add file comparison to processing tests
Browse files Browse the repository at this point in the history
And a test for basic numeric statistics
  • Loading branch information
m-kuhn committed Feb 21, 2016
1 parent f699564 commit 20fc30b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
28 changes: 18 additions & 10 deletions python/plugins/processing/tests/AlgorithmsTest.py
Expand Up @@ -137,10 +137,10 @@ def load_param(self, param):

def load_result_param(self, param):
"""
Lodas a result parameter. Creates a temporary destination where the result should go to and returns this location
Loads a result parameter. Creates a temporary destination where the result should go to and returns this location
so it can be sent to the algorithm as parameter.
"""
if param['type'] == 'vector':
if param['type'] in ['vector', 'file']:
outdir = tempfile.mkdtemp()
self.cleanup_paths.append(outdir)
basename = os.path.basename(param['name'])
Expand All @@ -153,14 +153,7 @@ def load_layer(self, param):
"""
Loads a layer which was specified as parameter.
"""
prefix = processingTestDataPath()
try:
if param['location'] == 'qgs':
prefix = unitTestDataPath()
except KeyError:
pass

filepath = os.path.join(prefix, param['name'])
filepath = self.filepath_from_param(param)

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

def filepath_from_param(self, param):
"""
Creates a filepath from a param
"""
prefix = processingTestDataPath()
if 'location' in param and param['location'] == 'qgs':
prefix = unitTestDataPath()

return os.path.join(prefix, param['name'])

def check_results(self, results, expected):
"""
Checks if result produced by an algorithm matches with the expected specification.
Expand All @@ -197,6 +200,11 @@ def check_results(self, results, expected):
strhash = hashlib.sha224(dataset.ReadAsArray(0).data).hexdigest()

self.assertEqual(strhash, expected_result['hash'])
elif 'file' == expected_result['type']:
expected_filepath = self.filepath_from_param(expected_result)
result_filepath = results[id]

self.assertFilesEqual(expected_filepath, result_filepath)


if __name__ == '__main__':
Expand Down
10 changes: 10 additions & 0 deletions python/plugins/processing/tests/testdata/algorithm_tests.yaml
Expand Up @@ -63,3 +63,13 @@ tests:
OUTPUT:
name: expected/polys_to_lines.gml
type: vector
- algorithm: qgis:basicstatisticsfornumericfields
name: Test (qgis:basicstatisticsfornumericfields)
params:
- name: multipolys.gml
type: vector
- 'Bfloatval'
results:
OUTPUT_HTML_FILE:
name: expected/basic_statistics_numeric_float.html
type: file
@@ -0,0 +1,21 @@
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>
<p>Analyzed layer: multipolys.gml</p>
<p>Analyzed field: Bfloatval</p>
<p>Count: 3</p>
<p>Unique values: 3</p>
<p>Minimum value: -0.123</p>
<p>Maximum value: 0.123</p>
<p>Range: 0.246</p>
<p>Sum: 0.0</p>
<p>Mean value: 0.0</p>
<p>Median value: 0.0</p>
<p>Standard deviation: 0.100429079454</p>
<p>Coefficient of Variation: 0</p>
<p>Minority (rarest occurring value): -0.123</p>
<p>Majority (most frequently occurring value): -0.123</p>
<p>First quartile: -0.0615</p>
<p>Third quartile: 0.0615</p>
<p>NULL (missed) values: 1</p>
<p>Interquartile Range (IQR): 0.123</p>
</body></html>

0 comments on commit 20fc30b

Please sign in to comment.