|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""QGIS Unit tests for QgsFeatureIterator. |
| 3 | +
|
| 4 | +.. note:: This program is free software; you can redistribute it and/or modify |
| 5 | +it under the terms of the GNU General Public License as published by |
| 6 | +the Free Software Foundation; either version 2 of the License, or |
| 7 | +(at your option) any later version. |
| 8 | +""" |
| 9 | +__author__ = 'Matthias Kuhn' |
| 10 | +__date__ = '18/09/2013' |
| 11 | +__copyright__ = 'Copyright 2013, The QGIS Project' |
| 12 | +# This will get replaced with a git SHA1 when you do a git archive |
| 13 | +__revision__ = '$Format:%H$' |
| 14 | + |
| 15 | +import os |
| 16 | + |
| 17 | +import qgis |
| 18 | +from qgis.core import QgsFeatureIterator, QgsVectorLayer, QgsFeatureRequest, QgsFeature |
| 19 | +from utilities import (unitTestDataPath, |
| 20 | + getQgisTestApp, |
| 21 | + TestCase, |
| 22 | + unittest, |
| 23 | + #expectedFailure |
| 24 | + ) |
| 25 | +QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp() |
| 26 | +TEST_DATA_DIR = unitTestDataPath() |
| 27 | + |
| 28 | +class TestQgsFeatureIterator(TestCase): |
| 29 | + |
| 30 | + def __init__(self, methodName): |
| 31 | + """Run once on class initialisation.""" |
| 32 | + unittest.TestCase.__init__(self, methodName) |
| 33 | + |
| 34 | + def test_FilterExpression(self): |
| 35 | + # create point layer |
| 36 | + myShpFile = os.path.join(TEST_DATA_DIR, 'points.shp') |
| 37 | + pointLayer = QgsVectorLayer(myShpFile, 'Points', 'ogr') |
| 38 | + |
| 39 | + ids = [ feat.id() for feat in pointLayer.getFeatures( QgsFeatureRequest().setFilterExpression( 'Staff > 3' ) ) ] |
| 40 | + expectedIds = [1L, 5L, 6L, 7L, 8L] |
| 41 | + myMessage = '\nExpected: {0} features\nGot: {1} features'.format( repr( expectedIds ), repr( ids ) ) |
| 42 | + assert ids == expectedIds, myMessage |
| 43 | + |
| 44 | + pointLayer.startEditing() |
| 45 | + self.addFeatures( pointLayer ) |
| 46 | + |
| 47 | + ids = [ feat.id() for feat in pointLayer.getFeatures( QgsFeatureRequest().setFilterExpression( 'Staff > 3' ) ) ] |
| 48 | + expectedIds = [-2L, 1L, 5L, 6L, 7L, 8L] |
| 49 | + myMessage = '\nExpected: {0} features\nGot: {1} features'.format( repr( expectedIds ), repr( ids ) ) |
| 50 | + assert ids == expectedIds, myMessage |
| 51 | + |
| 52 | + pointLayer.rollBack() |
| 53 | + |
| 54 | + ids = [ feat.id() for feat in pointLayer.getFeatures( QgsFeatureRequest().setFilterExpression( 'Staff > 3' ) ) ] |
| 55 | + expectedIds = [1L, 5L, 6L, 7L, 8L] |
| 56 | + myMessage = '\nExpected: {0} features\nGot: {1} features'.format( repr( expectedIds ), repr( ids ) ) |
| 57 | + assert ids == expectedIds, myMessage |
| 58 | + |
| 59 | + def test_FilterFids(self): |
| 60 | + # create point layer |
| 61 | + myShpFile = os.path.join(TEST_DATA_DIR, 'points.shp') |
| 62 | + pointLayer = QgsVectorLayer(myShpFile, 'Points', 'ogr') |
| 63 | + |
| 64 | + ids = [ feat.id() for feat in pointLayer.getFeatures( QgsFeatureRequest().setFilterFids( [7,8,12,30] ) ) ] |
| 65 | + expectedIds = [7L, 8L, 12L] |
| 66 | + myMessage = '\nExpected: {0} features\nGot: {1} features'.format( repr( expectedIds ), repr( ids ) ) |
| 67 | + assert ids == expectedIds, myMessage |
| 68 | + |
| 69 | + pointLayer.startEditing() |
| 70 | + self.addFeatures( pointLayer ) |
| 71 | + |
| 72 | + ids = [ feat.id() for feat in pointLayer.getFeatures( QgsFeatureRequest().setFilterFids( [-4,7,8,12,30] ) ) ] |
| 73 | + expectedIds = [-4L, 7L, 8L, 12L] |
| 74 | + myMessage = '\nExpected: {0} features\nGot: {1} features'.format( repr( expectedIds ), repr( ids ) ) |
| 75 | + assert ids == expectedIds, myMessage |
| 76 | + |
| 77 | + pointLayer.rollBack() |
| 78 | + |
| 79 | + ids = [ feat.id() for feat in pointLayer.getFeatures( QgsFeatureRequest().setFilterFids( [-2,7,8,12,30] ) ) ] |
| 80 | + expectedIds = [7L, 8L, 12L] |
| 81 | + myMessage = '\nExpected: {0} features\nGot: {1} features'.format( repr( expectedIds ), repr( ids ) ) |
| 82 | + assert ids == expectedIds, myMessage |
| 83 | + |
| 84 | + def addFeatures(self,vl): |
| 85 | + feat = QgsFeature() |
| 86 | + fields = vl.pendingFields() |
| 87 | + feat.setFields(fields) |
| 88 | + feat['Staff'] = 4 |
| 89 | + vl.addFeature( feat ) |
| 90 | + |
| 91 | + feat = QgsFeature() |
| 92 | + fields = vl.pendingFields() |
| 93 | + feat.setFields(fields) |
| 94 | + feat['Staff'] = 2 |
| 95 | + vl.addFeature( feat ) |
| 96 | + |
| 97 | +if __name__ == '__main__': |
| 98 | + unittest.main() |
0 commit comments