QgisAlgorithmsTest1.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. """
  2. ***************************************************************************
  3. QgisAlgorithmTests.py
  4. ---------------------
  5. Date : January 2016
  6. Copyright : (C) 2016 by Matthias Kuhn
  7. Email : matthias@opengis.ch
  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__ = 'Matthias Kuhn'
  18. __date__ = 'January 2016'
  19. __copyright__ = '(C) 2016, Matthias Kuhn'
  20. import AlgorithmsTestBase
  21. import nose2
  22. import shutil
  23. import os
  24. from qgis.core import (QgsApplication,
  25. QgsProcessingAlgorithm,
  26. QgsProcessingFeedback,
  27. QgsProcessingException)
  28. from qgis.analysis import (QgsNativeAlgorithms)
  29. import unittest
  30. from qgis.testing import start_app, QgisTestCase
  31. from processing.tools.dataobjects import createContext
  32. from processing.core.ProcessingConfig import ProcessingConfig
  33. from processing.modeler.ModelerUtils import ModelerUtils
  34. class TestAlg(QgsProcessingAlgorithm):
  35. def __init__(self):
  36. super().__init__()
  37. def name(self):
  38. return 'testalg'
  39. def displayName(self):
  40. return 'testalg'
  41. def initAlgorithm(self, config=None):
  42. pass
  43. def createInstance(self):
  44. return TestAlg()
  45. def processAlgorithm(self, parameters, context, feedback):
  46. raise QgsProcessingException('Exception while processing')
  47. return {}
  48. class TestQgisAlgorithms(QgisTestCase, AlgorithmsTestBase.AlgorithmsTest):
  49. @classmethod
  50. def setUpClass(cls):
  51. start_app()
  52. from processing.core.Processing import Processing
  53. Processing.initialize()
  54. cls.cleanup_paths = []
  55. cls.in_place_layers = {}
  56. cls.vector_layer_params = {}
  57. @classmethod
  58. def tearDownClass(cls):
  59. from processing.core.Processing import Processing
  60. Processing.deinitialize()
  61. for path in cls.cleanup_paths:
  62. shutil.rmtree(path)
  63. def test_definition_file(self):
  64. return 'qgis_algorithm_tests1.yaml'
  65. def testProcessingException(self):
  66. """
  67. Test that Python exception is caught when running an alg
  68. """
  69. alg = TestAlg()
  70. context = createContext()
  71. feedback = QgsProcessingFeedback()
  72. results, ok = alg.run({}, context, feedback)
  73. self.assertFalse(ok)
  74. def testParameterPythonImport(self):
  75. for t in QgsApplication.processingRegistry().parameterTypes():
  76. import_string = t.pythonImportString()
  77. # check that pythonImportString correctly imports
  78. exec(import_string)
  79. # and now we should be able to instantiate an object!
  80. if t.className() == 'QgsProcessingParameterProviderConnection':
  81. exec(f'test = {t.className()}(\'id\',\'name\', \'provider\')\nself.assertIsNotNone(test)')
  82. else:
  83. exec(f'test = {t.className()}(\'id\',\'name\')\nself.assertIsNotNone(test)')
  84. if __name__ == '__main__':
  85. nose2.main()