Skip to content

Commit 8582f61

Browse files
committed
Merge pull request #2761 from m-kuhn/ptest
Updates to processing tests
2 parents 50aa805 + 5f0173e commit 8582f61

22 files changed

+210
-2660
lines changed

ci/travis/linux/before_install.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ sudo apt-get install --force-yes --no-install-recommends --no-install-suggests \
1111
cmake-data \
1212
doxygen \
1313
flex \
14+
gdal-bin \
1415
git \
1516
graphviz \
1617
grass-dev \

python/plugins/processing/gui/TestTools.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def createTest(text):
102102
definition['name'] = 'Test ({})'.format(cmdname)
103103
definition['algorithm'] = cmdname
104104

105-
params = []
105+
params = {}
106106
results = {}
107107

108108
i = 0
@@ -123,7 +123,7 @@ def createTest(text):
123123
if not schema:
124124
p['location'] = '[The source data is not in the testdata directory. Please use data in the processing/tests/testdata folder.]'
125125

126-
params.append(p)
126+
params[param.name] = p
127127
elif isinstance(param, ParameterRaster):
128128
filename = token[1:-1]
129129
schema, filepath = extractSchemaPath(filename)
@@ -134,7 +134,7 @@ def createTest(text):
134134
if not schema:
135135
p['location'] = '[The source data is not in the testdata directory. Please use data in the processing/tests/testdata folder.]'
136136

137-
params.append(p)
137+
params[param.name] = p
138138
elif isinstance(param, ParameterMultipleInput):
139139
multiparams = token[1:-1].split(';')
140140
newparam = []
@@ -151,9 +151,9 @@ def createTest(text):
151151
if not schema:
152152
p['location'] = '[The source data is not in the testdata directory. Please use data in the processing/tests/testdata folder.]'
153153

154-
params.append(p)
154+
params[param.name] = p
155155
else:
156-
params.append(token)
156+
params[param.name] = token
157157

158158
definition['params'] = params
159159

python/plugins/processing/tests/AlgorithmsTest.py renamed to python/plugins/processing/tests/AlgorithmsTestBase.py

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
"""
44
***************************************************************************
5-
test_algorithms.py
5+
AlgorithmsTest.py
66
---------------------
77
Date : January 2016
88
Copyright : (C) 2016 by Matthias Kuhn
@@ -27,12 +27,12 @@
2727

2828
import qgis
2929
import os
30-
import shutil
3130
import yaml
3231
import nose2
3332
import gdal
3433
import hashlib
3534
import tempfile
35+
import re
3636

3737
from osgeo.gdalconst import GA_ReadOnly
3838

@@ -46,11 +46,6 @@
4646
QgsMapLayerRegistry
4747
)
4848

49-
from qgis.testing import (
50-
start_app,
51-
unittest
52-
)
53-
5449
from utilities import (
5550
unitTestDataPath
5651
)
@@ -60,25 +55,13 @@ def processingTestDataPath():
6055
return os.path.join(os.path.dirname(__file__), 'testdata')
6156

6257

63-
class TestAlgorithms(unittest.TestCase):
64-
65-
@classmethod
66-
def setUpClass(cls):
67-
start_app()
68-
from processing.core.Processing import Processing
69-
Processing.initialize()
70-
cls.cleanup_paths = []
71-
72-
@classmethod
73-
def tearDownClass(cls):
74-
for path in cls.cleanup_paths:
75-
shutil.rmtree(path)
58+
class AlgorithmsTest():
7659

7760
def test_algorithms(self):
7861
"""
7962
This is the main test function. All others will be executed based on the definitions in testdata/algorithm_tests.yaml
8063
"""
81-
with open(os.path.join(processingTestDataPath(), 'algorithm_tests.yaml'), 'r') as stream:
64+
with open(os.path.join(processingTestDataPath(), self.test_definition_file()), 'r') as stream:
8265
algorithm_tests = yaml.load(stream)
8366

8467
for algtest in algorithm_tests['tests']:
@@ -104,8 +87,8 @@ def check_algorithm(self, name, defs):
10487
for r, p in defs['results'].iteritems():
10588
alg.setOutputValue(r, self.load_result_param(p))
10689

107-
self.assertTrue(AlgorithmExecutor.runalg(alg))
10890
print(alg.getAsCommand())
91+
self.assertTrue(AlgorithmExecutor.runalg(alg))
10992
self.check_results(alg.getOutputValuesAsDictionary(), defs['results'])
11093

11194
def load_params(self, params):
@@ -133,21 +116,27 @@ def load_param(self, param):
133116
# No type specified, use whatever is there
134117
return param
135118

136-
raise KeyError("Unknown type '{}' specified for parameter '{}'".format(param['type'], param['name']))
119+
raise KeyError("Unknown type '{}' specified for parameter".format(param['type']))
137120

138121
def load_result_param(self, param):
139122
"""
140123
Loads a result parameter. Creates a temporary destination where the result should go to and returns this location
141124
so it can be sent to the algorithm as parameter.
142125
"""
143-
if param['type'] in ['vector', 'file']:
126+
if param['type'] in ['vector', 'file', 'regex']:
144127
outdir = tempfile.mkdtemp()
145128
self.cleanup_paths.append(outdir)
146129
basename = os.path.basename(param['name'])
147130
filepath = os.path.join(outdir, basename)
148131
return filepath
132+
elif param['type'] == 'rasterhash':
133+
outdir = tempfile.mkdtemp()
134+
self.cleanup_paths.append(outdir)
135+
basename = 'raster.tif'
136+
filepath = os.path.join(outdir, basename)
137+
return filepath
149138

150-
raise KeyError("Unknown type '{}' specified for parameter '{}'".format(param['type'], param['name']))
139+
raise KeyError("Unknown type '{}' specified for parameter".format(param['type']))
151140

152141
def load_layer(self, param):
153142
"""
@@ -188,10 +177,7 @@ def check_results(self, results, expected):
188177

189178
result_lyr = QgsVectorLayer(results[id], id, 'ogr')
190179

191-
try:
192-
compare = expected_result['compare']
193-
except KeyError:
194-
compare = {}
180+
compare = expected_result.get('compare', {})
195181

196182
self.assertLayersEqual(expected_lyr, result_lyr, compare=compare)
197183

@@ -205,6 +191,12 @@ def check_results(self, results, expected):
205191
result_filepath = results[id]
206192

207193
self.assertFilesEqual(expected_filepath, result_filepath)
194+
elif 'regex' == expected_result['type']:
195+
with open(results[id], 'r') as file:
196+
data = file.read()
197+
198+
for rule in expected_result.get('rules', []):
199+
self.assertRegexpMatches(data, rule)
208200

209201

210202
if __name__ == '__main__':

python/plugins/processing/tests/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ PLUGIN_INSTALL(processing tests/data ${TEST_DATA_FILES})
77
IF(ENABLE_TESTS)
88
INCLUDE(UsePythonTest)
99
ADD_PYTHON_TEST(ProcessingParametersTest ParametersTest.py)
10-
ADD_PYTHON_TEST(ProcessingAlgorithmsTest AlgorithmsTest.py)
10+
ADD_PYTHON_TEST(ProcessingQgisAlgorithmsTest QgisAlgorithmsTest.py)
11+
ADD_PYTHON_TEST(ProcessingGdalAlgorithmsTest GdalAlgorithmsTest.py)
1112
ENDIF(ENABLE_TESTS)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
GdalAlgorithmTests.py
6+
---------------------
7+
Date : January 2016
8+
Copyright : (C) 2016 by Matthias Kuhn
9+
Email : matthias@opengis.ch
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Matthias Kuhn'
21+
__date__ = 'January 2016'
22+
__copyright__ = '(C) 2016, Matthias Kuhn'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = ':%H$'
27+
28+
import AlgorithmsTestBase
29+
30+
import nose2
31+
import shutil
32+
33+
from qgis.testing import (
34+
start_app,
35+
unittest
36+
)
37+
38+
39+
class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest):
40+
41+
@classmethod
42+
def setUpClass(cls):
43+
start_app()
44+
from processing.core.Processing import Processing
45+
Processing.initialize()
46+
cls.cleanup_paths = []
47+
48+
@classmethod
49+
def tearDownClass(cls):
50+
for path in cls.cleanup_paths:
51+
shutil.rmtree(path)
52+
53+
def test_definition_file(self):
54+
return 'gdal_algorithm_tests.yaml'
55+
56+
57+
if __name__ == '__main__':
58+
nose2.main()

0 commit comments

Comments
 (0)