Skip to content

Commit 3799ee4

Browse files
committed
Added python test for QgsFeature from Germán Carrillo
1 parent 404bce6 commit 3799ee4

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

tests/src/python/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
INCLUDE(UsePythonTest)
22
ADD_PYTHON_TEST(PyQgsApplication test_qgsapplication.py)
3+
ADD_PYTHON_TEST(PyQgsFeature test_qgsfeature.py)
34
ADD_PYTHON_TEST(PyQgsGeometry test_qgsgeometry.py)
45
ADD_PYTHON_TEST(PyQgsVectorLayer test_qgsvectorlayer.py)
56
ADD_PYTHON_TEST(PyQgsRasterLayer test_qgsrasterlayer.py)

tests/src/python/test_qgsfeature.py

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsFeature.
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__ = 'Germán Carrillo'
10+
__date__ = '06/10/2012'
11+
__copyright__ = 'Copyright 2012, The Quantum GIS 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+
from PyQt4.QtCore import QVariant
18+
from qgis.core import QgsFeature, QgsGeometry, QgsPoint, QgsVectorLayer
19+
from utilities import (unitTestDataPath,
20+
getQgisTestApp,
21+
TestCase,
22+
unittest,
23+
#expectedFailure
24+
)
25+
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
26+
27+
28+
class TestQgsFeature(TestCase):
29+
30+
def test_CreateFeature(self):
31+
feat = QgsFeature()
32+
feat.addAttribute(1, "text")
33+
feat.setGeometry(QgsGeometry.fromPoint(QgsPoint(123,456)))
34+
myId = feat.id()
35+
myExpectedId = 0
36+
myMessage = '\nExpected: %s\nGot: %s' % (myExpectedId, myId)
37+
assert myId == myExpectedId, myMessage
38+
39+
def test_ValidFeature(self):
40+
myPath = os.path.join(unitTestDataPath(), 'points.shp')
41+
myLayer = QgsVectorLayer(myPath, 'Points', 'ogr')
42+
provider = myLayer.dataProvider()
43+
allAttrs = provider.attributeIndexes()
44+
provider.select(allAttrs)
45+
46+
feat = QgsFeature()
47+
provider.nextFeature(feat)
48+
myValidValue = feat.isValid()
49+
myMessage = '\nExpected: %s\nGot: %s' % ("True", myValidValue)
50+
assert myValidValue == True, myMessage
51+
52+
def test_TypeName(self):
53+
myPath = os.path.join(unitTestDataPath(), 'lines.shp')
54+
myLayer = QgsVectorLayer(myPath, 'Lines', 'ogr')
55+
provider = myLayer.dataProvider()
56+
allAttrs = provider.attributeIndexes()
57+
provider.select(allAttrs)
58+
59+
feat = QgsFeature()
60+
provider.nextFeature(feat)
61+
myTypeName = feat.typeName()
62+
myExpectedTypeName = "lines"
63+
myMessage = '\nExpected: %s\nGot: %s' % (myExpectedTypeName, myTypeName)
64+
assert myTypeName == myExpectedTypeName, myMessage
65+
66+
def test_AttributeMap(self):
67+
myPath = os.path.join(unitTestDataPath(), 'lines.shp')
68+
myLayer = QgsVectorLayer(myPath, 'Lines', 'ogr')
69+
provider = myLayer.dataProvider()
70+
allAttrs = provider.attributeIndexes()
71+
provider.select(allAttrs)
72+
feat = QgsFeature()
73+
provider.nextFeature(feat)
74+
75+
myAttributeMap = feat.attributeMap()
76+
myExpectedAttributeMap = {0: QVariant("Highway"), 1: QVariant(1)}
77+
78+
# Only for printing purposes
79+
myAttributeDict = {
80+
0:str(myAttributeMap[0].toString()),
81+
1:int(myAttributeMap[1].toString())}
82+
myExpectedAttributeDict = {0: "Highway", 1: 1}
83+
myMessage = '\nExpected: %s\nGot: %s' % (myExpectedAttributeDict,
84+
myAttributeDict)
85+
86+
assert myAttributeMap == myExpectedAttributeMap, myMessage
87+
88+
def test_AddAttribute(self):
89+
feat = QgsFeature()
90+
feat.addAttribute(1, "text")
91+
myCount = len(feat.attributeMap())
92+
myExpectedCount = 1
93+
myMessage = '\nExpected: %s\nGot: %s' % (myExpectedCount, myCount)
94+
assert myCount == myExpectedCount, myMessage
95+
96+
def test_ChangeAttribute(self):
97+
feat = QgsFeature()
98+
feat.addAttribute(1, "text")
99+
feat.changeAttribute(1, "changed")
100+
myChangedAttribute = feat.attributeMap()[1].toString()
101+
myExpectedAttribute = "changed"
102+
myMessage = '\nExpected: %s\nGot: %s' % (myExpectedAttribute,
103+
myChangedAttribute)
104+
assert myChangedAttribute == myExpectedAttribute, myMessage
105+
106+
def test_DeleteAttribute(self):
107+
feat = QgsFeature()
108+
feat.addAttribute(1, "text")
109+
feat.deleteAttribute(1)
110+
myCount = len(feat.attributeMap())
111+
myExpectedCount = 0
112+
myMessage = '\nExpected: %s\nGot: %s' % (myExpectedCount, myCount)
113+
assert myCount == myExpectedCount, myMessage
114+
115+
def test_SetGeometry(self):
116+
feat = QgsFeature()
117+
feat.setGeometry(QgsGeometry.fromPoint(QgsPoint(123,456)))
118+
myGeometry = feat.geometry()
119+
myExpectedGeometry = "!None"
120+
myMessage = '\nExpected: %s\nGot: %s' % (myExpectedGeometry, myGeometry)
121+
assert myGeometry != None, myMessage
122+
123+
if __name__ == '__main__':
124+
unittest.main()
125+

0 commit comments

Comments
 (0)