Skip to content

Commit c603df1

Browse files
committedMar 16, 2017
[processing] allow tests for scripts
1 parent 3bcf287 commit c603df1

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
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('scrips:'):
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)
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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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.geometry():
25+
outputGeometry = f.geometry().centroid()
26+
outputFeature.setGeometry(outputGeometry)
27+
28+
writer.addFeature(outputFeature)
29+
feedback.setProgress(int(count * total))
30+
31+
del writer

0 commit comments

Comments
 (0)
Please sign in to comment.