|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + QgisAlgorithmTests.py |
| 6 | + --------------------- |
| 7 | + Date : January 2019 |
| 8 | + Copyright : (C) 2019 by Nyall Dawson |
| 9 | + Email : nyall dot dawson 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__ = 'Nyall Dawson' |
| 21 | +__date__ = 'January 2019' |
| 22 | +__copyright__ = '(C) 2019, Nyall Dawson' |
| 23 | + |
| 24 | +# This will get replaced with a git SHA1 when you do a git archive |
| 25 | + |
| 26 | +__revision__ = ':%H$' |
| 27 | + |
| 28 | +import nose2 |
| 29 | +import shutil |
| 30 | +import gc |
| 31 | + |
| 32 | +from qgis.core import (QgsApplication, |
| 33 | + QgsProcessing, |
| 34 | + QgsProcessingContext, |
| 35 | + QgsVectorLayer) |
| 36 | +from qgis.PyQt import sip |
| 37 | +from qgis.analysis import (QgsNativeAlgorithms) |
| 38 | +from qgis.testing import start_app, unittest |
| 39 | +import processing |
| 40 | +from processing.tests.TestData import points |
| 41 | + |
| 42 | + |
| 43 | +class TestProcessingGeneral(unittest.TestCase): |
| 44 | + |
| 45 | + @classmethod |
| 46 | + def setUpClass(cls): |
| 47 | + start_app() |
| 48 | + from processing.core.Processing import Processing |
| 49 | + Processing.initialize() |
| 50 | + QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) |
| 51 | + cls.cleanup_paths = [] |
| 52 | + cls.in_place_layers = {} |
| 53 | + cls.vector_layer_params = {} |
| 54 | + |
| 55 | + @classmethod |
| 56 | + def tearDownClass(cls): |
| 57 | + from processing.core.Processing import Processing |
| 58 | + Processing.deinitialize() |
| 59 | + for path in cls.cleanup_paths: |
| 60 | + shutil.rmtree(path) |
| 61 | + |
| 62 | + def testRun(self): |
| 63 | + context = QgsProcessingContext() |
| 64 | + |
| 65 | + # try running an alg using processing.run - ownership of result layer should be transferred back to the caller |
| 66 | + res = processing.run('qgis:buffer', |
| 67 | + {'DISTANCE': 1, 'INPUT': points(), 'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT}, |
| 68 | + context=context) |
| 69 | + self.assertIn('OUTPUT', res) |
| 70 | + # output should be the layer instance itself |
| 71 | + self.assertIsInstance(res['OUTPUT'], QgsVectorLayer) |
| 72 | + # Python should have ownership |
| 73 | + self.assertTrue(sip.ispyowned(res['OUTPUT'])) |
| 74 | + del context |
| 75 | + gc.collect() |
| 76 | + self.assertFalse(sip.isdeleted(res['OUTPUT'])) |
| 77 | + |
| 78 | + # now try using processing.run with is_child_algorithm = True. Ownership should remain with the context |
| 79 | + context = QgsProcessingContext() |
| 80 | + res = processing.run('qgis:buffer', |
| 81 | + {'DISTANCE': 1, 'INPUT': points(), 'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT}, |
| 82 | + context=context, is_child_algorithm=True) |
| 83 | + self.assertIn('OUTPUT', res) |
| 84 | + # output should be a layer string reference, NOT the layer itself |
| 85 | + self.assertIsInstance(res['OUTPUT'], str) |
| 86 | + layer = context.temporaryLayerStore().mapLayer(res['OUTPUT']) |
| 87 | + self.assertIsInstance(layer, QgsVectorLayer) |
| 88 | + # context should have ownership |
| 89 | + self.assertFalse(sip.ispyowned(layer)) |
| 90 | + del context |
| 91 | + gc.collect() |
| 92 | + self.assertTrue(sip.isdeleted(layer)) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == '__main__': |
| 96 | + nose2.main() |
0 commit comments