Skip to content

Commit 4acb34f

Browse files
elpasonyalldawson
authored andcommittedSep 14, 2018
Initial test for in-place processing algorithms
1 parent db98a38 commit 4acb34f

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
 
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for Processing In-Place algorithms.
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = 'Alessandro Pasotti'
10+
__date__ = '2018-09'
11+
__copyright__ = 'Copyright 2018, The QGIS Project'
12+
# This will get replaced with a git SHA1 when you do a git archive
13+
__revision__ = '$Format:%H$'
14+
15+
from qgis.PyQt.QtCore import QCoreApplication, QVariant
16+
from qgis.core import QgsFeature, QgsGeometry, QgsSettings, QgsApplication, QgsMemoryProviderUtils, QgsWkbTypes, QgsField, QgsFields, QgsProcessingFeatureSourceDefinition, QgsProcessingContext, QgsProcessingFeedback
17+
from processing.core.Processing import Processing
18+
from qgis.testing import start_app, unittest
19+
from qgis.PyQt.QtTest import QSignalSpy
20+
from qgis.analysis import QgsNativeAlgorithms
21+
22+
start_app()
23+
24+
25+
class TestQgsProcessingRecentAlgorithmLog(unittest.TestCase):
26+
27+
@classmethod
28+
def setUpClass(cls):
29+
"""Run before all tests"""
30+
QCoreApplication.setOrganizationName("QGIS_Test")
31+
QCoreApplication.setOrganizationDomain(
32+
"QGIS_TestPyQgsProcessingInPlace.com")
33+
QCoreApplication.setApplicationName("QGIS_TestPyQgsProcessingInPlace")
34+
QgsSettings().clear()
35+
Processing.initialize()
36+
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
37+
cls.registry = QgsApplication.instance().processingRegistry()
38+
fields = QgsFields()
39+
fields.append(QgsField('int_f', QVariant.Int))
40+
cls.vl = QgsMemoryProviderUtils.createMemoryLayer(
41+
'mylayer', fields, QgsWkbTypes.Point)
42+
43+
f1 = QgsFeature(cls.vl.fields())
44+
f1['int_f'] = 1
45+
f1.setGeometry(QgsGeometry.fromWkt('point(9 45)'))
46+
f2 = QgsFeature(cls.vl.fields())
47+
f2['int_f'] = 2
48+
f2.setGeometry(QgsGeometry.fromWkt('point(9.5 45.6)'))
49+
cls.vl.dataProvider().addFeatures([f1, f2])
50+
51+
assert cls.vl.isValid()
52+
assert cls.vl.featureCount() == 2
53+
54+
def test_algs(self):
55+
# Clone?
56+
alg = self.registry.algorithmById('native:translategeometry')
57+
self.assertIsNotNone(alg)
58+
parameters = {}
59+
parameters['INPUT'] = QgsProcessingFeatureSourceDefinition(self.vl.id(), True)
60+
parameters['OUTPUT'] = 'memory:'
61+
62+
context = QgsProcessingContext()
63+
feedback = QgsProcessingFeedback()
64+
self.assertTrue(alg.prepare(parameters, context, feedback))
65+
66+
for f in self.vl.getFeatures():
67+
new_f = alg.processFeature(f, context, feedback)[0]
68+
self.assertEqual(new_f.id(), f.id())
69+
70+
from IPython import embed; embed()
71+
72+
73+
74+
75+
76+
77+
if __name__ == '__main__':
78+
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.