Skip to content

Commit

Permalink
Improve Test generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Médéric RIBREUX committed Jun 12, 2016
1 parent bf02d34 commit 0cd4498
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 582 deletions.
57 changes: 39 additions & 18 deletions python/plugins/processing/gui/TestTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
__revision__ = '$Format:%H$'

import os
import re
import yaml
import hashlib

Expand All @@ -50,7 +51,9 @@
ParameterRaster,
ParameterVector,
ParameterMultipleInput,
ParameterFile
ParameterFile,
ParameterString,
ParameterBoolean
)


Expand Down Expand Up @@ -95,11 +98,31 @@ def extractSchemaPath(filepath):
return schema, localpath


def parseParameters(command):
"""
Parse alg string to grab parameters value.
Can handle quotes and comma.
"""
pos = 0
exp = re.compile(r"""(['"]?)(.*?)\1(,|$)""")
while True:
m = exp.search(command, pos)
result = m.group(2)
separator = m.group(3)

yield result

if not separator:
break

pos = m.end(0)


def createTest(text):
definition = {}

tokens = text[len('processing.runalg('):-1].split(',')
cmdname = (tokens[0])[1:-1]
tokens = list(parseParameters(text[len('processing.runalg('):-1]))
cmdname = tokens[0]
alg = Processing.getAlgorithm(cmdname)

definition['name'] = 'Test ({})'.format(cmdname)
Expand All @@ -117,8 +140,7 @@ def createTest(text):
token = tokens[i]

if isinstance(param, ParameterVector):
filename = token[1:-1]
schema, filepath = extractSchemaPath(filename)
schema, filepath = extractSchemaPath(token)
p = {
'type': 'vector',
'name': filepath
Expand All @@ -128,8 +150,7 @@ def createTest(text):

params[param.name] = p
elif isinstance(param, ParameterRaster):
filename = token[1:-1]
schema, filepath = extractSchemaPath(filename)
schema, filepath = extractSchemaPath(token)
p = {
'type': 'raster',
'name': filepath
Expand All @@ -139,7 +160,7 @@ def createTest(text):

params[param.name] = p
elif isinstance(param, ParameterMultipleInput):
multiparams = token[1:-1].split(';')
multiparams = token.split(';')
newparam = []

# Handle datatype detection
Expand All @@ -164,8 +185,7 @@ def createTest(text):

params[param.name] = p
elif isinstance(param, ParameterFile):
filename = token[1:-1]
schema, filepath = extractSchemaPath(filename)
schema, filepath = extractSchemaPath(token)
p = {
'type': 'file',
'name': filepath
Expand All @@ -174,6 +194,10 @@ def createTest(text):
p['location'] = '[The source data is not in the testdata directory. Please use data in the processing/tests/testdata folder.]'

params[param.name] = p
elif isinstance(param, ParameterString):
params[param.name] = token
elif isinstance(param, ParameterBoolean):
params[param.name] = token == 'True'
else:
try:
params[param.name] = int(token)
Expand All @@ -195,8 +219,7 @@ def createTest(text):
if isinstance(out, (OutputNumber, OutputString)):
results[out.name] = unicode(out)
elif isinstance(out, OutputRaster):
filename = token[1:-1]
dataset = gdal.Open(filename, GA_ReadOnly)
dataset = gdal.Open(token, GA_ReadOnly)
dataArray = nan_to_num(dataset.ReadAsArray(0))
strhash = hashlib.sha224(dataArray.data).hexdigest()

Expand All @@ -205,17 +228,15 @@ def createTest(text):
'hash': strhash
}
elif isinstance(out, OutputVector):
filename = token[1:-1]
schema, filepath = extractSchemaPath(filename)
schema, filepath = extractSchemaPath(token)
results[out.name] = {
'type': 'vector',
'name': filepath
}
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)
schema, filepath = extractSchemaPath(token)
results[out.name] = {
'type': 'file',
'name': filepath
Expand All @@ -224,7 +245,6 @@ def createTest(text):
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

dlg = ShowTestDialog(yaml.dump([definition], default_flow_style=False))
dlg.exec_()

Expand All @@ -244,7 +264,8 @@ def __init__(self, s):
self.text = QTextEdit()
self.text.setFontFamily("monospace")
self.text.setEnabled(True)
self.text.setText(s)
# Add two spaces in front of each text for faster copy/paste
self.text.setText(' {}'.format(s.replace('\n', '\n ')))
layout.addWidget(self.text)
self.setLayout(layout)
QMetaObject.connectSlotsByName(self)
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<PAMDataset>
<PAMRasterBand band="1">
<Histograms>
<HistItem>
<HistMin>1</HistMin>
<HistMax>1</HistMax>
<BucketCount>1</BucketCount>
<IncludeOutOfRange>0</IncludeOutOfRange>
<Approximate>0</Approximate>
<HistCounts>0</HistCounts>
</HistItem>
</Histograms>
<Metadata>
<MDI key="COLOR_TABLE_RULES_COUNT">1</MDI>
<MDI key="COLOR_TABLE_RULE_RGB_0">1.000000e+00 1.000000e+00 255 0 127 255 0 127</MDI>
<MDI key="Generated_with">GRASS GIS 7.0.4</MDI>
<MDI key="STATISTICS_MAXIMUM">1</MDI>
<MDI key="STATISTICS_MEAN">1</MDI>
<MDI key="STATISTICS_MINIMUM">1</MDI>
<MDI key="STATISTICS_STDDEV">0</MDI>
</Metadata>
</PAMRasterBand>
</PAMDataset>
Loading

0 comments on commit 0cd4498

Please sign in to comment.