/python/plugins/processing/tests/QgisAlgorithmsTest1.py

https://github.com/ricardogsilva/Quantum-GIS · Python · 108 lines · 63 code · 23 blank · 22 comment · 5 complexity · d41865823a3e72be269b3447a01d5a3e MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. """
  3. ***************************************************************************
  4. QgisAlgorithmTests.py
  5. ---------------------
  6. Date : January 2016
  7. Copyright : (C) 2016 by Matthias Kuhn
  8. Email : matthias@opengis.ch
  9. ***************************************************************************
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. ***************************************************************************
  17. """
  18. __author__ = 'Matthias Kuhn'
  19. __date__ = 'January 2016'
  20. __copyright__ = '(C) 2016, Matthias Kuhn'
  21. import AlgorithmsTestBase
  22. import nose2
  23. import shutil
  24. import os
  25. from qgis.core import (QgsApplication,
  26. QgsProcessingAlgorithm,
  27. QgsProcessingFeedback,
  28. QgsProcessingException)
  29. from qgis.analysis import (QgsNativeAlgorithms)
  30. from qgis.testing import start_app, unittest
  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(unittest.TestCase, 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('test = {}(\'id\',\'name\', \'provider\')\nself.assertIsNotNone(test)'.format(t.className()))
  82. else:
  83. exec('test = {}(\'id\',\'name\')\nself.assertIsNotNone(test)'.format(t.className()))
  84. if __name__ == '__main__':
  85. nose2.main()