ToolsTest.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """
  2. ***************************************************************************
  3. ToolsTest
  4. ---------------------
  5. Date : July 2016
  6. Copyright : (C) 2016 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__ = 'July 2016'
  19. __copyright__ = '(C) 2016, Nyall Dawson'
  20. import os
  21. import shutil
  22. from qgis.core import NULL, QgsVectorLayer
  23. import unittest
  24. from qgis.testing import start_app, QgisTestCase
  25. from processing.tests.TestData import points
  26. from processing.tools import vector
  27. testDataPath = os.path.join(os.path.dirname(__file__), 'testdata')
  28. start_app()
  29. class VectorTest(QgisTestCase):
  30. @classmethod
  31. def setUpClass(cls):
  32. cls.cleanup_paths = []
  33. @classmethod
  34. def tearDownClass(cls):
  35. for path in cls.cleanup_paths:
  36. shutil.rmtree(path)
  37. def testValues(self):
  38. test_data = points()
  39. test_layer = QgsVectorLayer(test_data, 'test', 'ogr')
  40. # field by index
  41. res = vector.values(test_layer, 1)
  42. self.assertEqual(res[1], [1, 2, 3, 4, 5, 6, 7, 8, 9])
  43. # field by name
  44. res = vector.values(test_layer, 'id')
  45. self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])
  46. # two fields
  47. res = vector.values(test_layer, 1, 2)
  48. self.assertEqual(res[1], [1, 2, 3, 4, 5, 6, 7, 8, 9])
  49. self.assertEqual(res[2], [2, 1, 0, 2, 1, 0, 0, 0, 0])
  50. # two fields by name
  51. res = vector.values(test_layer, 'id', 'id2')
  52. self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])
  53. self.assertEqual(res['id2'], [2, 1, 0, 2, 1, 0, 0, 0, 0])
  54. # two fields by name and index
  55. res = vector.values(test_layer, 'id', 2)
  56. self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])
  57. self.assertEqual(res[2], [2, 1, 0, 2, 1, 0, 0, 0, 0])
  58. def testConvertNulls(self):
  59. self.assertEqual(vector.convert_nulls([]), [])
  60. self.assertEqual(vector.convert_nulls([], '_'), [])
  61. self.assertEqual(vector.convert_nulls([NULL]), [None])
  62. self.assertEqual(vector.convert_nulls([NULL], '_'), ['_'])
  63. self.assertEqual(vector.convert_nulls([NULL], -1), [-1])
  64. self.assertEqual(vector.convert_nulls([1, 2, 3]), [1, 2, 3])
  65. self.assertEqual(vector.convert_nulls([1, None, 3]), [1, None, 3])
  66. self.assertEqual(vector.convert_nulls([1, NULL, 3, NULL]), [1, None, 3, None])
  67. self.assertEqual(vector.convert_nulls([1, NULL, 3, NULL], '_'), [1, '_', 3, '_'])
  68. if __name__ == '__main__':
  69. unittest.main()