|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + ToolsTest |
| 6 | + --------------------- |
| 7 | + Date : July 2017 |
| 8 | + Copyright : (C) 2017 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__ = 'July 2016' |
| 22 | +__copyright__ = '(C) 2016, Nyall Dawson' |
| 23 | + |
| 24 | +# This will get replaced with a git SHA1 when you do a git archive |
| 25 | + |
| 26 | +__revision__ = '$Format:%H$' |
| 27 | + |
| 28 | +from qgis.testing import start_app, unittest |
| 29 | +from processing.tests.TestData import points2 |
| 30 | +from processing.tools import vector |
| 31 | +from qgis.core import (QgsVectorLayer, QgsFeatureRequest) |
| 32 | +from processing.core.ProcessingConfig import ProcessingConfig |
| 33 | + |
| 34 | +start_app() |
| 35 | + |
| 36 | + |
| 37 | +class VectorTest(unittest.TestCase): |
| 38 | + |
| 39 | + def testFeatures(self): |
| 40 | + ProcessingConfig.initialize() |
| 41 | + |
| 42 | + test_data = points2() |
| 43 | + test_layer = QgsVectorLayer(test_data, 'test', 'ogr') |
| 44 | + |
| 45 | + # test with all features |
| 46 | + features = vector.features(test_layer) |
| 47 | + self.assertEqual(len(features), 8) |
| 48 | + self.assertEqual(set([f.id() for f in features]), set([0, 1, 2, 3, 4, 5, 6, 7])) |
| 49 | + |
| 50 | + # test with selected features |
| 51 | + previous_value = ProcessingConfig.getSetting(ProcessingConfig.USE_SELECTED) |
| 52 | + ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, True) |
| 53 | + test_layer.selectByIds([2, 4, 6]) |
| 54 | + features = vector.features(test_layer) |
| 55 | + self.assertEqual(len(features), 3) |
| 56 | + self.assertEqual(set([f.id() for f in features]), set([2, 4, 6])) |
| 57 | + |
| 58 | + # selection, but not using selected features |
| 59 | + ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, False) |
| 60 | + test_layer.selectByIds([2, 4, 6]) |
| 61 | + features = vector.features(test_layer) |
| 62 | + self.assertEqual(len(features), 8) |
| 63 | + self.assertEqual(set([f.id() for f in features]), set([0, 1, 2, 3, 4, 5, 6, 7])) |
| 64 | + |
| 65 | + # using selected features, but no selection |
| 66 | + ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, True) |
| 67 | + test_layer.removeSelection() |
| 68 | + features = vector.features(test_layer) |
| 69 | + self.assertEqual(len(features), 8) |
| 70 | + self.assertEqual(set([f.id() for f in features]), set([0, 1, 2, 3, 4, 5, 6, 7])) |
| 71 | + |
| 72 | + # test that feature request is honored |
| 73 | + ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, False) |
| 74 | + features = vector.features(test_layer, QgsFeatureRequest().setFilterFids([1, 3, 5])) |
| 75 | + self.assertEqual(set([f.id() for f in features]), set([1, 3, 5])) |
| 76 | + |
| 77 | + # test that feature request is honored when using selections |
| 78 | + ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, True) |
| 79 | + test_layer.selectByIds([2, 4, 6]) |
| 80 | + features = vector.features(test_layer, QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry)) |
| 81 | + self.assertTrue(all([f.geometry() == None for f in features])) |
| 82 | + features = vector.features(test_layer, QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry)) |
| 83 | + self.assertEqual(set([f.id() for f in features]), set([2, 4, 6])) |
| 84 | + |
| 85 | + ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, previous_value) |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == '__main__': |
| 89 | + unittest.main() |
0 commit comments