Skip to content

Commit

Permalink
[processing] Allow test creation from history window
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Feb 4, 2016
1 parent 8adc871 commit f0629ea
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 67 deletions.
1 change: 0 additions & 1 deletion python/plugins/processing/gui/AlgorithmDialog.py
Expand Up @@ -88,7 +88,6 @@ def __init__(self, alg):


def runAsBatch(self): def runAsBatch(self):
dlg = BatchAlgorithmDialog(self.alg) dlg = BatchAlgorithmDialog(self.alg)
dlg.show()
dlg.exec_() dlg.exec_()


def setParamValues(self): def setParamValues(self):
Expand Down
1 change: 0 additions & 1 deletion python/plugins/processing/gui/HistoryDialog.py
Expand Up @@ -128,7 +128,6 @@ def createTest(self):
TestTools.createTest(item.entry.text) TestTools.createTest(item.entry.text)


def showPopupMenu(self, point): def showPopupMenu(self, point):
return
item = self.tree.currentItem() item = self.tree.currentItem()
if isinstance(item, TreeLogEntryItem): if isinstance(item, TreeLogEntryItem):
if item.isAlg: if item.isAlg:
Expand Down
206 changes: 141 additions & 65 deletions python/plugins/processing/gui/TestTools.py
Expand Up @@ -26,88 +26,163 @@
__revision__ = '$Format:%H$' __revision__ = '$Format:%H$'


import os import os
import yaml
import hashlib

from osgeo import gdal from osgeo import gdal
from osgeo.gdalconst import GA_ReadOnly from osgeo.gdalconst import GA_ReadOnly


from PyQt4.QtCore import QCoreApplication, QMetaObject from PyQt4.QtCore import QCoreApplication, QMetaObject
from PyQt4.QtGui import QMessageBox, QDialog, QVBoxLayout, QTextEdit from PyQt4.QtGui import QMessageBox, QDialog, QVBoxLayout, QTextEdit


from processing.core.Processing import Processing from processing.core.Processing import Processing
from processing.core.outputs import OutputNumber from processing.core.outputs import (
from processing.core.outputs import OutputString OutputNumber,
from processing.core.outputs import OutputRaster OutputString,
from processing.core.outputs import OutputVector OutputRaster,
from processing.tools import vector, dataobjects OutputVector
)

from processing.core.parameters import (
ParameterRaster,
ParameterVector,
ParameterMultipleInput
)


def extractSchemaPath(filepath):
"""
Trys to find where the file is relative to the QGIS source code directory.
If it is already placed in the processing or QGIS testdata directory it will
return an appropriate schema and relative filepath
Args:
filepath: The path of the file to examine
Returns:
A tuple (schema, relative_file_path) where the schema is 'qgs' or 'proc'
if we can assume that the file is in this testdata directory.
"""
parts = []
schema = None
localpath = ''
path = filepath
part = True

while part:
(path, part) = os.path.split(path)
if part == 'testdata' and not localpath:
localparts = parts
localparts.reverse()
localpath = os.path.join(*localparts)

parts.append(part)

parts.reverse()

try:
testsindex = parts.index('tests')
except ValueError:
return '', filepath

if parts[testsindex - 1] == 'processing':
schema = 'proc'

return schema, localpath




def createTest(text): def createTest(text):
s = '' definition = {}

tokens = text[len('processing.runalg('):-1].split(',') tokens = text[len('processing.runalg('):-1].split(',')
cmdname = (tokens[0])[1:-1] cmdname = (tokens[0])[1:-1]
methodname = 'test_' + cmdname.replace(':', '')
s += 'def ' + methodname + '(self):\n'
alg = Processing.getAlgorithm(cmdname) alg = Processing.getAlgorithm(cmdname)
execcommand = 'processing.runalg('
definition['name'] = 'Test ({})'.format(cmdname)
definition['algorithm'] = cmdname

params = []
results = {}

i = 0 i = 0
for token in tokens: for param in alg.parameters:
if i < alg.getVisibleParametersCount() + 1: if param.hidden:
if os.path.exists(token[1:-1]): continue
token = os.path.basename(token[1:-1])[:-4] + '()'
execcommand += token + ','
else:
execcommand += 'None,'
i += 1 i += 1
s += '\toutputs=' + execcommand[:-1] + ')\n' token = tokens[i]


i = -1 * len(alg.outputs) if isinstance(param, ParameterVector):
for out in alg.outputs: filename = token[1:-1]
filename = (tokens[i])[1:-1] schema, filepath = extractSchemaPath(filename)
if tokens[i] == unicode(None): p = {
QMessageBox.critical(None, tr('Error'), 'type': 'vector',
tr('Cannot create unit test for that algorithm execution. The ' 'name': filepath
'output cannot be a temporary file')) }
return if not schema:
s += "\toutput=outputs['" + out.name + "']\n" p['location'] = '[The source data is not in the testdata directory. Please use data in the processing/tests/testdata folder.]'

params.append(p)
elif isinstance(param, ParameterRaster):
filename = token[1:-1]
schema, filepath = extractSchemaPath(filename)
p = {
'type': 'raster',
'name': filepath
}
if not schema:
p['location'] = '[The source data is not in the testdata directory. Please use data in the processing/tests/testdata folder.]'

params.append(p)
elif isinstance(param, ParameterMultipleInput):
multiparams = token[1:-1].split(';')
newparam = []
for mp in multiparams:
schema, filepath = extractSchemaPath(mp)
newparam.append({
'type': 'vector',
'name': filepath
})
p = {
'type': 'multi',
'params': newparam
}
if not schema:
p['location'] = '[The source data is not in the testdata directory. Please use data in the processing/tests/testdata folder.]'

params.append(p)
else:
params.append(token)

definition['params'] = params

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

if isinstance(out, (OutputNumber, OutputString)): if isinstance(out, (OutputNumber, OutputString)):
s += 'self.assertTrue(' + unicode(out) + ', output.value)\n' results[out.name] = unicode(out)
if isinstance(out, OutputRaster): elif isinstance(out, OutputRaster):
filename = token[1:-1]
dataset = gdal.Open(filename, GA_ReadOnly) dataset = gdal.Open(filename, GA_ReadOnly)
strhash = hash(unicode(dataset.ReadAsArray(0).tolist())) strhash = hashlib.sha224(dataset.ReadAsArray(0).data).hexdigest()
s += '\tself.assertTrue(os.path.isfile(output))\n'
s += '\tdataset=gdal.Open(output, GA_ReadOnly)\n' results[out.name] = {
s += '\tstrhash=hash(unicode(dataset.ReadAsArray(0).tolist()))\n' 'type': 'rasterhash',
s += '\tself.assertEqual(strhash,' + unicode(strhash) + ')\n' 'hash': strhash
if isinstance(out, OutputVector): }
layer = dataobjects.getObject(filename) elif isinstance(out, OutputVector):
fields = layer.pendingFields() filename = token[1:-1]
s += '\tlayer=dataobjects.getObjectFromUri(output, True)\n' schema, filepath = extractSchemaPath(filename)
s += '\tfields=layer.pendingFields()\n' results[out.name] = {
s += '\texpectednames=[' + ','.join(["'" + unicode(f.name()) + "'" 'type': 'vector',
for f in fields]) + ']\n' 'name': filepath
s += '\texpectedtypes=[' + ','.join(["'" + unicode(f.typeName()) + "'" }
for f in fields]) + ']\n' if not schema:
s += '\tnames=[unicode(f.name()) for f in fields]\n' 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.]'
s += '\ttypes=[unicode(f.typeName()) for f in fields]\n'
s += '\tself.assertEqual(expectednames, names)\n' definition['results'] = results
s += '\tself.assertEqual(expectedtypes, types)\n'
features = vector.features(layer) dlg = ShowTestDialog(yaml.dump([definition], default_flow_style=False))
numfeat = len(features)
s += '\tfeatures=processing.features(layer)\n'
s += '\tself.assertEqual(' + unicode(numfeat) + ', len(features))\n'
if numfeat > 0:
feature = features.next()
attrs = feature.attributes()
s += '\tfeature=features.next()\n'
s += '\tattrs=feature.attributes()\n'
s += '\texpectedvalues=[' + ','.join(['"' + unicode(attr) + '"'
for attr in attrs]) + ']\n'
s += '\tvalues=[unicode(attr) for attr in attrs]\n'
s += '\tself.assertEqual(expectedvalues, values)\n'
s += "\twkt='" + unicode(feature.geometry().exportToWkt()) + "'\n"
s += '\tself.assertEqual(wkt, \
unicode(feature.geometry().exportToWkt()))'

dlg = ShowTestDialog(s)
dlg.exec_() dlg.exec_()




Expand All @@ -124,6 +199,7 @@ def __init__(self, s):
self.setWindowTitle(self.tr('Unit test')) self.setWindowTitle(self.tr('Unit test'))
layout = QVBoxLayout() layout = QVBoxLayout()
self.text = QTextEdit() self.text = QTextEdit()
self.text.setFontFamily("monospace")
self.text.setEnabled(True) self.text.setEnabled(True)
self.text.setText(s) self.text.setText(s)
layout.addWidget(self.text) layout.addWidget(self.text)
Expand Down

0 comments on commit f0629ea

Please sign in to comment.