Skip to content
Permalink
Browse files

Improve Test generation

  • Loading branch information
Médéric RIBREUX
Médéric RIBREUX committed Jun 12, 2016
1 parent bf02d34 commit 0cd449865806d136fb19a1e6451adffd9660111a
@@ -26,6 +26,7 @@
__revision__ = '$Format:%H$'

import os
import re
import yaml
import hashlib

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


@@ -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)
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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)
@@ -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()

@@ -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
@@ -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_()

@@ -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.
@@ -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>

0 comments on commit 0cd4498

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