Skip to content
Permalink
Browse files

Merge pull request #2825 from m-kuhn/processingTestFiles

Add file comparison for HTML output to processing
  • Loading branch information
m-kuhn committed Feb 21, 2016
2 parents 4c63c85 + 23802eb commit a0a20eaf607bdda350c01ad1643956ddb050b825
@@ -186,10 +186,10 @@ def processAlgorithm(self, progress):

def createHTML(self, outputFile, algData):
f = codecs.open(outputFile, 'w', encoding='utf-8')
f.write('<html><head>')
f.write('<html><head>\n')
f.write('<meta http-equiv="Content-Type" content="text/html; \
charset=utf-8" /></head><body>')
charset=utf-8" /></head><body>\n')
for s in algData:
f.write('<p>' + unicode(s) + '</p>')
f.write('</body></html>')
f.write('<p>' + unicode(s) + '</p>\n')
f.write('</body></html>\n')
f.close()
@@ -145,10 +145,10 @@ def processAlgorithm(self, progress):

def createHTML(self, outputFile, algData):
f = codecs.open(outputFile, 'w', encoding='utf-8')
f.write('<html><head>')
f.write('<html><head>\n')
f.write('<meta http-equiv="Content-Type" content="text/html; \
charset=utf-8" /></head><body>')
charset=utf-8" /></head><body>\n')
for s in algData:
f.write('<p>' + unicode(s) + '</p>')
f.write('<p>' + unicode(s) + '</p>\n')
f.write('</body></html>')
f.close()
@@ -40,7 +40,8 @@
OutputNumber,
OutputString,
OutputRaster,
OutputVector
OutputVector,
OutputHTML
)

from processing.core.parameters import (
@@ -156,8 +157,8 @@ def createTest(text):

definition['params'] = params

for i, out in enumerate(alg.outputs):
token = tokens[i - len(alg.outputs)]
for i, out in enumerate([out for out in alg.outputs if not out.hidden]):
token = tokens[i - alg.getVisibleOutputsCount()]

if isinstance(out, (OutputNumber, OutputString)):
results[out.name] = unicode(out)
@@ -179,6 +180,15 @@ def createTest(text):
}
if not schema:
results[out.name]['location'] = '[The expected result data is not in the testdata directory. Please write it to processing/tests/testdata/expected. Prefer gml files.]'
elif isinstance(out, OutputHTML):
filename = token[1:-1]
schema, filepath = extractSchemaPath(filename)
results[out.name] = {
'type': 'file',
'name': filepath
}
if not schema:
results[out.name]['location'] = '[The expected result file is not in the testdata directory. Please redirect the output to processing/tests/testdata/expected.]'

definition['results'] = results

@@ -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'])
@@ -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')
@@ -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.
@@ -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__':
@@ -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>
@@ -27,6 +27,7 @@

import os
import sys
import difflib

from PyQt4.QtCore import QVariant
from qgis.core import QgsApplication, QgsFeatureRequest, QgsVectorLayer
@@ -140,6 +141,19 @@ def assertLayersEqual(self, layer_expected, layer_result, **kwargs):
)
)

def assertFilesEqual(self, filepath_expected, filepath_result):
with open(filepath_expected, 'r') as file_expected:
with open(filepath_result, 'r') as file_result:
diff = difflib.unified_diff(
file_expected.readlines(),
file_result.readlines(),
fromfile='expected',
tofile='result',
)
diff = list(diff)
self.assertEqual(0, len(diff), ''.join(diff))


# Patch unittest
unittest.TestCase = TestCase

0 comments on commit a0a20ea

Please sign in to comment.
You can’t perform that action at this time.