Skip to content

Commit e950b7b

Browse files
authored
Merge pull request #4268 from alexbruy/scripts-tests
[processing] support for scripts in the test suite
2 parents 3bcf287 + e1b1465 commit e950b7b

10 files changed

+196
-2
lines changed

python/plugins/processing/tests/AlgorithmsTestBase.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
from numpy import nan_to_num
4646

4747
import processing
48+
49+
from processing.script.ScriptAlgorithm import ScriptAlgorithm # NOQA
50+
4851
from processing.modeler.ModelerAlgorithmProvider import ModelerAlgorithmProvider # NOQA
4952
from processing.algs.qgis.QGISAlgorithmProvider import QGISAlgorithmProvider # NOQA
5053
from processing.algs.grass7.Grass7AlgorithmProvider import Grass7AlgorithmProvider # NOQA
@@ -90,7 +93,11 @@ def check_algorithm(self, name, defs):
9093

9194
params = self.load_params(defs['params'])
9295

93-
alg = processing.Processing.getAlgorithm(defs['algorithm']).getCopy()
96+
if defs['algorithm'].startswith('script:'):
97+
filePath = os.path.join(processingTestDataPath(), 'scripts', '{}.py'.format(defs['algorithm'][len('script:'):]))
98+
alg = ScriptAlgorithm(filePath)
99+
else:
100+
alg = processing.Processing.getAlgorithm(defs['algorithm']).getCopy()
94101

95102
if isinstance(params, list):
96103
for param in zip(alg.parameters, params):

python/plugins/processing/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ IF(ENABLE_TESTS)
1111
ADD_PYTHON_TEST(ProcessingToolsTest ToolsTest.py)
1212
ADD_PYTHON_TEST(ProcessingQgisAlgorithmsTest QgisAlgorithmsTest.py)
1313
ADD_PYTHON_TEST(ProcessingGdalAlgorithmsTest GdalAlgorithmsTest.py)
14+
ADD_PYTHON_TEST(ProcessingScriptAlgorithmsTest ScriptAlgorithmsTest.py)
1415
ADD_PYTHON_TEST(ProcessingGrass7AlgorithmsImageryTest Grass7AlgorithmsImageryTest.py)
1516
ADD_PYTHON_TEST(ProcessingGrass7AlgorithmsRasterTest Grass7AlgorithmsRasterTest.py)
1617
ENDIF(ENABLE_TESTS)

python/plugins/processing/tests/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ The above translates to
5454
name: expected/polys_densify.gml
5555
```
5656

57+
It is also possible to create tests for Processing scripts. Scripts
58+
should be placed in the `scrips` subdirectory in the test data directory
59+
`python/plugins/processing/tests/testdata/`. Script file name
60+
should match script algorithm name.
61+
5762
Params and results
5863
------------------
5964

@@ -163,7 +168,7 @@ OUTPUT:
163168
type: rasterhash
164169
hash: f1fedeb6782f9389cf43590d4c85ada9155ab61fef6dc285aaeb54d6
165170
```
166-
171+
167172
#### Files
168173

169174
You can compare the content of an output file by an expected result reference file
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
ScriptAlgorithmsTests.py
6+
---------------------
7+
Date : March 2017
8+
Copyright : (C) 2017 by Alexander Bruy
9+
Email : alexander dot bruy at gmail dot com
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__ = 'Alexander Bruy'
21+
__date__ = 'March 2017'
22+
__copyright__ = '(C) 2017, Alexander Bruy'
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 start_app, unittest
34+
35+
36+
class TestScriptAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest):
37+
38+
@classmethod
39+
def setUpClass(cls):
40+
start_app()
41+
from processing.core.Processing import Processing
42+
Processing.initialize()
43+
cls.cleanup_paths = []
44+
45+
@classmethod
46+
def tearDownClass(cls):
47+
for path in cls.cleanup_paths:
48+
shutil.rmtree(path)
49+
50+
def test_definition_file(self):
51+
return 'script_algorithm_tests.yaml'
52+
53+
54+
if __name__ == '__main__':
55+
nose2.main()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<GMLFeatureClassList>
2+
<GMLFeatureClass>
3+
<Name>selected_points</Name>
4+
<ElementPath>selected_points</ElementPath>
5+
<!--POINT-->
6+
<GeometryType>1</GeometryType>
7+
<SRSName>EPSG:4326</SRSName>
8+
<DatasetSpecificInfo>
9+
<FeatureCount>2</FeatureCount>
10+
<ExtentXMin>1.00000</ExtentXMin>
11+
<ExtentXMax>5.00000</ExtentXMax>
12+
<ExtentYMin>1.00000</ExtentYMin>
13+
<ExtentYMax>2.00000</ExtentYMax>
14+
</DatasetSpecificInfo>
15+
<PropertyDefn>
16+
<Name>id</Name>
17+
<ElementPath>id</ElementPath>
18+
<Type>Integer</Type>
19+
</PropertyDefn>
20+
<PropertyDefn>
21+
<Name>id2</Name>
22+
<ElementPath>id2</ElementPath>
23+
<Type>Integer</Type>
24+
</PropertyDefn>
25+
</GMLFeatureClass>
26+
</GMLFeatureClassList>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ogr:FeatureCollection
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation=""
5+
xmlns:ogr="http://ogr.maptools.org/"
6+
xmlns:gml="http://www.opengis.net/gml">
7+
<gml:boundedBy>
8+
<gml:Box>
9+
<gml:coord><gml:X>1</gml:X><gml:Y>1</gml:Y></gml:coord>
10+
<gml:coord><gml:X>5</gml:X><gml:Y>2</gml:Y></gml:coord>
11+
</gml:Box>
12+
</gml:boundedBy>
13+
14+
<gml:featureMember>
15+
<ogr:selected_points fid="points.0">
16+
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>1,1</gml:coordinates></gml:Point></ogr:geometryProperty>
17+
<ogr:id>1</ogr:id>
18+
<ogr:id2>2</ogr:id2>
19+
</ogr:selected_points>
20+
</gml:featureMember>
21+
<gml:featureMember>
22+
<ogr:selected_points fid="points.3">
23+
<ogr:geometryProperty><gml:Point srsName="EPSG:4326"><gml:coordinates>5,2</gml:coordinates></gml:Point></ogr:geometryProperty>
24+
<ogr:id>4</ogr:id>
25+
<ogr:id2>2</ogr:id2>
26+
</ogr:selected_points>
27+
</gml:featureMember>
28+
</ogr:FeatureCollection>

python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,3 +2514,14 @@ tests:
25142514
MIN_DISTANCE: 0.0
25152515
POINT_NUMBER: 5
25162516
results: {}
2517+
2518+
- algorithm: script:selectbyattribute
2519+
name: Select by attribute
2520+
params:
2521+
INPUT_LAYER:
2522+
name: points.gml
2523+
type: vector
2524+
results:
2525+
OUTPUT_LAYER:
2526+
name: expected/selected_points.gml
2527+
type: vector
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# See ../README.md for a description of the file format
2+
3+
tests:
4+
- algorithm: script:centroids
5+
name: Centroids script test
6+
params:
7+
INPUT_LAYER:
8+
name: polys.gml
9+
type: vector
10+
results:
11+
OUTPUT_LAYER:
12+
name: expected/centroid_polys.gml
13+
type: vector
14+
compare:
15+
geometry:
16+
precision: 7
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
##Centroids=name
2+
##Geometry=group
3+
##INPUT_LAYER=vector
4+
##OUTPUT_LAYER=output vector
5+
6+
from qgis.core import QgsWkbTypes, QgsGeometry
7+
8+
from processing.tools.vector import VectorWriter
9+
10+
layer = processing.getObject(INPUT_LAYER)
11+
fields = layer.fields()
12+
13+
writer = VectorWriter(OUTPUT_LAYER, 'utf-8', fields, QgsWkbTypes.Point, layer.crs())
14+
15+
features = processing.features(layer)
16+
count = len(features)
17+
if count == 0:
18+
raise GeoAlgorithmExecutionException('Input layer contains no features.')
19+
20+
total = 100.0 / len(features)
21+
22+
for count, f in enumerate(features):
23+
outputFeature = f
24+
if f.hasGeometry():
25+
outputGeometry = f.geometry().centroid()
26+
outputFeature.setGeometry(outputGeometry)
27+
28+
writer.addFeature(outputFeature)
29+
feedback.setProgress(int(count * total))
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
##Select by attribute=name
2+
##Tests=group
3+
##INPUT_LAYER=vector
4+
##OUTPUT_LAYER=output vector
5+
6+
import processing
7+
8+
result = processing.runalg("qgis:selectbyattribute",
9+
INPUT_LAYER,
10+
"id2",
11+
0,
12+
"2")
13+
14+
processing.runalg("qgis:saveselectedfeatures",
15+
result["OUTPUT"],
16+
OUTPUT_LAYER)

0 commit comments

Comments
 (0)