Skip to content

Commit

Permalink
Added python test for QgsFeature from Germán Carrillo
Browse files Browse the repository at this point in the history
  • Loading branch information
timlinux committed Oct 29, 2012
1 parent 404bce6 commit 3799ee4
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
@@ -1,5 +1,6 @@
INCLUDE(UsePythonTest)
ADD_PYTHON_TEST(PyQgsApplication test_qgsapplication.py)
ADD_PYTHON_TEST(PyQgsFeature test_qgsfeature.py)
ADD_PYTHON_TEST(PyQgsGeometry test_qgsgeometry.py)
ADD_PYTHON_TEST(PyQgsVectorLayer test_qgsvectorlayer.py)
ADD_PYTHON_TEST(PyQgsRasterLayer test_qgsrasterlayer.py)
Expand Down
125 changes: 125 additions & 0 deletions tests/src/python/test_qgsfeature.py
@@ -0,0 +1,125 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsFeature.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Germán Carrillo'
__date__ = '06/10/2012'
__copyright__ = 'Copyright 2012, The Quantum GIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import os

from PyQt4.QtCore import QVariant
from qgis.core import QgsFeature, QgsGeometry, QgsPoint, QgsVectorLayer
from utilities import (unitTestDataPath,
getQgisTestApp,
TestCase,
unittest,
#expectedFailure
)
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()


class TestQgsFeature(TestCase):

def test_CreateFeature(self):
feat = QgsFeature()
feat.addAttribute(1, "text")
feat.setGeometry(QgsGeometry.fromPoint(QgsPoint(123,456)))
myId = feat.id()
myExpectedId = 0
myMessage = '\nExpected: %s\nGot: %s' % (myExpectedId, myId)
assert myId == myExpectedId, myMessage

def test_ValidFeature(self):
myPath = os.path.join(unitTestDataPath(), 'points.shp')
myLayer = QgsVectorLayer(myPath, 'Points', 'ogr')
provider = myLayer.dataProvider()
allAttrs = provider.attributeIndexes()
provider.select(allAttrs)

feat = QgsFeature()
provider.nextFeature(feat)
myValidValue = feat.isValid()
myMessage = '\nExpected: %s\nGot: %s' % ("True", myValidValue)
assert myValidValue == True, myMessage

def test_TypeName(self):
myPath = os.path.join(unitTestDataPath(), 'lines.shp')
myLayer = QgsVectorLayer(myPath, 'Lines', 'ogr')
provider = myLayer.dataProvider()
allAttrs = provider.attributeIndexes()
provider.select(allAttrs)

feat = QgsFeature()
provider.nextFeature(feat)
myTypeName = feat.typeName()
myExpectedTypeName = "lines"
myMessage = '\nExpected: %s\nGot: %s' % (myExpectedTypeName, myTypeName)
assert myTypeName == myExpectedTypeName, myMessage

def test_AttributeMap(self):
myPath = os.path.join(unitTestDataPath(), 'lines.shp')
myLayer = QgsVectorLayer(myPath, 'Lines', 'ogr')
provider = myLayer.dataProvider()
allAttrs = provider.attributeIndexes()
provider.select(allAttrs)
feat = QgsFeature()
provider.nextFeature(feat)

myAttributeMap = feat.attributeMap()
myExpectedAttributeMap = {0: QVariant("Highway"), 1: QVariant(1)}

# Only for printing purposes
myAttributeDict = {
0:str(myAttributeMap[0].toString()),
1:int(myAttributeMap[1].toString())}
myExpectedAttributeDict = {0: "Highway", 1: 1}
myMessage = '\nExpected: %s\nGot: %s' % (myExpectedAttributeDict,
myAttributeDict)

assert myAttributeMap == myExpectedAttributeMap, myMessage

def test_AddAttribute(self):
feat = QgsFeature()
feat.addAttribute(1, "text")
myCount = len(feat.attributeMap())
myExpectedCount = 1
myMessage = '\nExpected: %s\nGot: %s' % (myExpectedCount, myCount)
assert myCount == myExpectedCount, myMessage

def test_ChangeAttribute(self):
feat = QgsFeature()
feat.addAttribute(1, "text")
feat.changeAttribute(1, "changed")
myChangedAttribute = feat.attributeMap()[1].toString()
myExpectedAttribute = "changed"
myMessage = '\nExpected: %s\nGot: %s' % (myExpectedAttribute,
myChangedAttribute)
assert myChangedAttribute == myExpectedAttribute, myMessage

def test_DeleteAttribute(self):
feat = QgsFeature()
feat.addAttribute(1, "text")
feat.deleteAttribute(1)
myCount = len(feat.attributeMap())
myExpectedCount = 0
myMessage = '\nExpected: %s\nGot: %s' % (myExpectedCount, myCount)
assert myCount == myExpectedCount, myMessage

def test_SetGeometry(self):
feat = QgsFeature()
feat.setGeometry(QgsGeometry.fromPoint(QgsPoint(123,456)))
myGeometry = feat.geometry()
myExpectedGeometry = "!None"
myMessage = '\nExpected: %s\nGot: %s' % (myExpectedGeometry, myGeometry)
assert myGeometry != None, myMessage

if __name__ == '__main__':
unittest.main()

0 comments on commit 3799ee4

Please sign in to comment.