Skip to content
Permalink
Browse files

[processing] Fix processing.runAndLoadResults

Fixes #21551
  • Loading branch information
nyalldawson committed Mar 13, 2019
1 parent ab530e4 commit db1645cf1260d83b2dc411983e392df16a19d19d
@@ -96,6 +96,8 @@ def handleAlgorithmResults(alg, context, feedback=None, showResults=True, parame
scope = QgsExpressionContextScope()
expcontext.appendScope(scope)
for out in alg.outputDefinitions():
if out.name() not in parameters:
continue
outValue = parameters[out.name()]
if hasattr(outValue, "sink"):
outValue = outValue.sink.valueAsString(expcontext)[0]
@@ -32,7 +32,8 @@
from qgis.core import (QgsApplication,
QgsProcessing,
QgsProcessingContext,
QgsVectorLayer)
QgsVectorLayer,
QgsProject)
from qgis.PyQt import sip
from qgis.analysis import (QgsNativeAlgorithms)
from qgis.testing import start_app, unittest
@@ -91,6 +92,26 @@ def testRun(self):
gc.collect()
self.assertTrue(sip.isdeleted(layer))

def testRunAndLoadResults(self):
QgsProject.instance().removeAllMapLayers()
context = QgsProcessingContext()

# try running an alg using processing.runAndLoadResults - ownership of result layer should be transferred to
# project, and layer should be present in project
res = processing.runAndLoadResults('qgis:buffer',
{'DISTANCE': 1, 'INPUT': points(), 'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT},
context=context)
self.assertIn('OUTPUT', res)
# output should be the layer path
self.assertIsInstance(res['OUTPUT'], str)

self.assertEqual(context.layersToLoadOnCompletion()[res['OUTPUT']].project, QgsProject.instance())
layer = QgsProject.instance().mapLayer(res['OUTPUT'])
self.assertIsInstance(layer, QgsVectorLayer)

# Python should NOT have ownership
self.assertFalse(sip.ispyowned(layer))


if __name__ == '__main__':
nose2.main()

0 comments on commit db1645c

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