ProcessingGeneralTest.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. """
  2. ***************************************************************************
  3. QgisAlgorithmTests.py
  4. ---------------------
  5. Date : January 2019
  6. Copyright : (C) 2019 by Nyall Dawson
  7. Email : nyall dot dawson at gmail dot com
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Nyall Dawson'
  18. __date__ = 'January 2019'
  19. __copyright__ = '(C) 2019, Nyall Dawson'
  20. import nose2
  21. import shutil
  22. import gc
  23. from qgis.core import (QgsApplication,
  24. QgsProcessing,
  25. QgsProcessingContext,
  26. QgsVectorLayer,
  27. QgsProject)
  28. from qgis.PyQt import sip
  29. from qgis.analysis import (QgsNativeAlgorithms)
  30. import unittest
  31. from qgis.testing import start_app, QgisTestCase
  32. import processing
  33. from processing.tests.TestData import points
  34. class TestProcessingGeneral(QgisTestCase):
  35. @classmethod
  36. def setUpClass(cls):
  37. start_app()
  38. from processing.core.Processing import Processing
  39. Processing.initialize()
  40. cls.cleanup_paths = []
  41. cls.in_place_layers = {}
  42. cls.vector_layer_params = {}
  43. @classmethod
  44. def tearDownClass(cls):
  45. from processing.core.Processing import Processing
  46. Processing.deinitialize()
  47. for path in cls.cleanup_paths:
  48. shutil.rmtree(path)
  49. def testRun(self):
  50. context = QgsProcessingContext()
  51. # try running an alg using processing.run - ownership of result layer should be transferred back to the caller
  52. res = processing.run('qgis:buffer',
  53. {'DISTANCE': 1, 'INPUT': points(), 'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT},
  54. context=context)
  55. self.assertIn('OUTPUT', res)
  56. # output should be the layer instance itself
  57. self.assertIsInstance(res['OUTPUT'], QgsVectorLayer)
  58. # Python should have ownership
  59. self.assertTrue(sip.ispyowned(res['OUTPUT']))
  60. del context
  61. gc.collect()
  62. self.assertFalse(sip.isdeleted(res['OUTPUT']))
  63. # now try using processing.run with is_child_algorithm = True. Ownership should remain with the context
  64. context = QgsProcessingContext()
  65. res = processing.run('qgis:buffer',
  66. {'DISTANCE': 1, 'INPUT': points(), 'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT},
  67. context=context, is_child_algorithm=True)
  68. self.assertIn('OUTPUT', res)
  69. # output should be a layer string reference, NOT the layer itself
  70. self.assertIsInstance(res['OUTPUT'], str)
  71. layer = context.temporaryLayerStore().mapLayer(res['OUTPUT'])
  72. self.assertIsInstance(layer, QgsVectorLayer)
  73. # context should have ownership
  74. self.assertFalse(sip.ispyowned(layer))
  75. del context
  76. gc.collect()
  77. self.assertTrue(sip.isdeleted(layer))
  78. def testRunAndLoadResults(self):
  79. QgsProject.instance().removeAllMapLayers()
  80. context = QgsProcessingContext()
  81. # try running an alg using processing.runAndLoadResults - ownership of result layer should be transferred to
  82. # project, and layer should be present in project
  83. res = processing.runAndLoadResults('qgis:buffer',
  84. {'DISTANCE': 1, 'INPUT': points(), 'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT},
  85. context=context)
  86. self.assertIn('OUTPUT', res)
  87. # output should be the layer path
  88. self.assertIsInstance(res['OUTPUT'], str)
  89. self.assertEqual(context.layersToLoadOnCompletion()[res['OUTPUT']].project, QgsProject.instance())
  90. layer = QgsProject.instance().mapLayer(res['OUTPUT'])
  91. self.assertIsInstance(layer, QgsVectorLayer)
  92. # Python should NOT have ownership
  93. self.assertFalse(sip.ispyowned(layer))
  94. def testProviders(self):
  95. """
  96. When run from a standalone script (like this test), ensure that the providers from separate plugins are available
  97. """
  98. providers = [p.id() for p in QgsApplication.processingRegistry().providers()]
  99. self.assertIn('qgis', providers)
  100. self.assertIn('native', providers)
  101. self.assertIn('gdal', providers)
  102. self.assertIn('project', providers)
  103. self.assertIn('script', providers)
  104. self.assertIn('model', providers)
  105. self.assertIn('grass7', providers)
  106. self.assertIn('otb', providers)
  107. if __name__ == '__main__':
  108. nose2.main()