Skip to content

Commit

Permalink
fix PyQgsVectorLayer test
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Jul 18, 2013
1 parent 6149d34 commit ab4fc78
Showing 1 changed file with 66 additions and 69 deletions.
135 changes: 66 additions & 69 deletions tests/src/python/test_qgsvectorlayer.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
__revision__ = '$Format:%H$' __revision__ = '$Format:%H$'


import os import os
import qgis
from PyQt4.QtCore import QVariant from PyQt4.QtCore import QVariant


from qgis.core import (QgsVectorLayer, from qgis.core import (QgsVectorLayer,
Expand Down Expand Up @@ -42,7 +43,7 @@ def createLayerWithOnePoint():
"addfeat", "memory") "addfeat", "memory")
pr = layer.dataProvider() pr = layer.dataProvider()
f = QgsFeature() f = QgsFeature()
f.setAttributes([QVariant("test"), QVariant(123)]) f.setAttributes(["test", 123])
f.setGeometry(QgsGeometry.fromPoint(QgsPoint(100,200))) f.setGeometry(QgsGeometry.fromPoint(QgsPoint(100,200)))
assert pr.addFeatures([f]) assert pr.addFeatures([f])
assert layer.pendingFeatureCount() == 1 assert layer.pendingFeatureCount() == 1
Expand All @@ -54,16 +55,15 @@ def createJoinLayer():
"joinlayer", "memory") "joinlayer", "memory")
pr = joinLayer.dataProvider() pr = joinLayer.dataProvider()
f1 = QgsFeature() f1 = QgsFeature()
f1.setAttributes([QVariant("foo"), QVariant(123), QVariant(321)]) f1.setAttributes(["foo", 123, 321])
f1.setGeometry(QgsGeometry.fromPoint(QgsPoint(1,1))) f1.setGeometry(QgsGeometry.fromPoint(QgsPoint(1,1)))
f2 = QgsFeature() f2 = QgsFeature()
f2.setAttributes([QVariant("bar"), QVariant(456), QVariant(654)]) f2.setAttributes(["bar", 456, 654])
f2.setGeometry(QgsGeometry.fromPoint(QgsPoint(2,2))) f2.setGeometry(QgsGeometry.fromPoint(QgsPoint(2,2)))
assert pr.addFeatures([f1, f2]) assert pr.addFeatures([f1, f2])
assert joinLayer.pendingFeatureCount() == 2 assert joinLayer.pendingFeatureCount() == 2
return joinLayer return joinLayer



def dumpFeature(f): def dumpFeature(f):
print "--- FEATURE DUMP ---" print "--- FEATURE DUMP ---"
print "valid: %d | id: %d" % (f.isValid(), f.id()) print "valid: %d | id: %d" % (f.isValid(), f.id())
Expand All @@ -74,10 +74,8 @@ def dumpFeature(f):
print "no geometry" print "no geometry"
print "attrs: %s" % str(f.attributes()) print "attrs: %s" % str(f.attributes())



def formatAttributes(attrs): def formatAttributes(attrs):
return repr([ unicode(a.toString()) for a in attrs ]) return repr([ unicode(a) for a in attrs ])



def dumpEditBuffer(layer): def dumpEditBuffer(layer):
editBuffer = layer.editBuffer() editBuffer = layer.editBuffer()
Expand All @@ -93,7 +91,6 @@ def dumpEditBuffer(layer):
for fid, geom in editBuffer.changedGeometries().iteritems(): for fid, geom in editBuffer.changedGeometries().iteritems():
print "%d | %s" % (f.id(), f.geometry().exportToWkt()) print "%d | %s" % (f.id(), f.geometry().exportToWkt())



class TestQgsVectorLayer(TestCase): class TestQgsVectorLayer(TestCase):


def test_FeatureCount(self): def test_FeatureCount(self):
Expand Down Expand Up @@ -260,25 +257,25 @@ def checkAfter():
# check select+nextFeature # check select+nextFeature
fi = layer.getFeatures() fi = layer.getFeatures()
f = fi.next() f = fi.next()
assert f[0].toString() == "good" assert f[0] == "good"


# check feature at id # check feature at id
f2 = layer.getFeatures(QgsFeatureRequest( f.id() )).next() f2 = layer.getFeatures(QgsFeatureRequest( f.id() )).next()
assert f2[0].toString() == "good" assert f2[0] == "good"


def checkBefore(): def checkBefore():
# check select+nextFeature # check select+nextFeature
f = layer.getFeatures().next() f = layer.getFeatures().next()
assert f[0].toString() == "test" assert f[0] == "test"


checkBefore() checkBefore()


# try to change attribute without editing mode # try to change attribute without editing mode
assert not layer.changeAttributeValue(fid, 0, QVariant("good")) assert not layer.changeAttributeValue(fid, 0, "good")


# change attribute # change attribute
layer.startEditing() layer.startEditing()
assert layer.changeAttributeValue(fid, 0, QVariant("good")) assert layer.changeAttributeValue(fid, 0, "good")


checkAfter() checkAfter()


Expand All @@ -298,7 +295,7 @@ def test_ChangeAttributeAfterAddFeature(self):


newF = QgsFeature() newF = QgsFeature()
newF.setGeometry( QgsGeometry.fromPoint(QgsPoint(1,1)) ) newF.setGeometry( QgsGeometry.fromPoint(QgsPoint(1,1)) )
newF.setAttributes( [ QVariant("hello"), QVariant(42) ] ) newF.setAttributes( ["hello", 42] )


def checkAfter(): def checkAfter():
assert len(layer.pendingFields()) == 2 assert len(layer.pendingFields()) == 2
Expand All @@ -307,15 +304,15 @@ def checkAfter():
f = fi.next() f = fi.next()
attrs = f.attributes() attrs = f.attributes()
assert len(attrs) == 2 assert len(attrs) == 2
assert attrs[0].toString() == "hello" assert attrs[0] == "hello"
assert attrs[1].toInt()[0] == 12 assert attrs[1] == 12


self.assertRaises(StopIteration, fi.next) self.assertRaises(StopIteration, fi.next)


# check feature at id # check feature at id
f2 = layer.getFeatures(QgsFeatureRequest( f.id() )).next() f2 = layer.getFeatures(QgsFeatureRequest( f.id() )).next()
assert f2[0].toString() == "hello" assert f2[0] == "hello"
assert f2[1].toInt()[0] == 12 assert f2[1] == 12


def checkBefore(): def checkBefore():
# check feature # check feature
Expand All @@ -326,7 +323,7 @@ def checkBefore():
layer.startEditing() layer.startEditing()
layer.beginEditCommand("AddFeature + ChangeAttribute") layer.beginEditCommand("AddFeature + ChangeAttribute")
assert layer.addFeature(newF) assert layer.addFeature(newF)
assert layer.changeAttributeValue(newF.id(), 1, QVariant(12)) assert layer.changeAttributeValue(newF.id(), 1, 12)
layer.endEditCommand() layer.endEditCommand()


checkAfter() checkAfter()
Expand Down Expand Up @@ -392,24 +389,24 @@ def checkAfter():
# check select+nextFeature # check select+nextFeature
f = layer.getFeatures().next() f = layer.getFeatures().next()
assert f.geometry().asPoint() == QgsPoint(300,400) assert f.geometry().asPoint() == QgsPoint(300,400)
assert f[0].toString() == "changed" assert f[0] == "changed"
# check feature at id # check feature at id
f2 = layer.getFeatures(QgsFeatureRequest( f.id() )).next() f2 = layer.getFeatures(QgsFeatureRequest( f.id() )).next()
assert f2.geometry().asPoint() == QgsPoint(300,400) assert f2.geometry().asPoint() == QgsPoint(300,400)
assert f2[0].toString() == "changed" assert f2[0] == "changed"


def checkBefore(): def checkBefore():
# check select+nextFeature # check select+nextFeature
f = layer.getFeatures().next() f = layer.getFeatures().next()
assert f.geometry().asPoint() == QgsPoint(100,200) assert f.geometry().asPoint() == QgsPoint(100,200)
assert f[0].toString() == "test" assert f[0] == "test"


checkBefore() checkBefore()


# change geometry # change geometry
layer.startEditing() layer.startEditing()
layer.beginEditCommand("ChangeGeometry + ChangeAttribute") layer.beginEditCommand("ChangeGeometry + ChangeAttribute")
assert layer.changeAttributeValue(fid, 0, QVariant("changed")) assert layer.changeAttributeValue(fid, 0, "changed")
assert layer.changeGeometry(fid, QgsGeometry.fromPoint(QgsPoint(300,400))) assert layer.changeGeometry(fid, QgsGeometry.fromPoint(QgsPoint(300,400)))
layer.endEditCommand() layer.endEditCommand()


Expand All @@ -431,7 +428,7 @@ def test_ChangeGeometryAfterAddFeature(self):


newF = QgsFeature() newF = QgsFeature()
newF.setGeometry( QgsGeometry.fromPoint(QgsPoint(1,1)) ) newF.setGeometry( QgsGeometry.fromPoint(QgsPoint(1,1)) )
newF.setAttributes( [ QVariant("hello"), QVariant(42) ] ) newF.setAttributes(["hello", 42])


def checkAfter(): def checkAfter():
assert len(layer.pendingFields()) == 2 assert len(layer.pendingFields()) == 2
Expand Down Expand Up @@ -486,8 +483,8 @@ def checkBefore():
f = layer.getFeatures().next() f = layer.getFeatures().next()
attrs = f.attributes() attrs = f.attributes()
assert len(attrs) == 2 assert len(attrs) == 2
assert attrs[0].toString() == "test" assert attrs[0] == "test"
assert attrs[1].toInt()[0] == 123 assert attrs[1] == 123


def checkAfter(): def checkAfter():
# check fields # check fields
Expand All @@ -501,15 +498,15 @@ def checkAfter():
f = layer.getFeatures().next() f = layer.getFeatures().next()
attrs = f.attributes() attrs = f.attributes()
assert len(attrs) == 3 assert len(attrs) == 3
assert attrs[0].toString() == "test" assert attrs[0] == "test"
assert attrs[1].toInt()[0] == 123 assert attrs[1] == 123
assert not attrs[2].isValid() assert attrs[2] is None


# check feature at id # check feature at id
f2 = layer.getFeatures(QgsFeatureRequest( f.id() )).next() f2 = layer.getFeatures(QgsFeatureRequest( f.id() )).next()
assert f2[0].toString() == "test" assert f2[0] == "test"
assert f2[1].toInt()[0] == 123 assert f2[1] == 123
assert not f2[2].isValid() assert f2[2] is None


#for nt in layer.dataProvider().nativeTypes(): #for nt in layer.dataProvider().nativeTypes():
# print (nt.mTypeDesc, nt.mTypeName, nt.mType, nt.mMinLen, # print (nt.mTypeDesc, nt.mTypeName, nt.mType, nt.mMinLen,
Expand Down Expand Up @@ -542,7 +539,7 @@ def test_AddAttributeAfterAddFeature(self):


newF = QgsFeature() newF = QgsFeature()
newF.setGeometry( QgsGeometry.fromPoint(QgsPoint(1,1)) ) newF.setGeometry( QgsGeometry.fromPoint(QgsPoint(1,1)) )
newF.setAttributes( [ QVariant("hello"), QVariant(42) ] ) newF.setAttributes(["hello", 42])


fld1 = QgsField("fld1", QVariant.Int, "integer") fld1 = QgsField("fld1", QVariant.Int, "integer")


Expand All @@ -557,14 +554,14 @@ def checkAfter():
f = layer.getFeatures().next() f = layer.getFeatures().next()
attrs = f.attributes() attrs = f.attributes()
assert len(attrs) == 3 assert len(attrs) == 3
assert attrs[0].toString() == "hello" assert attrs[0] == "hello"
assert attrs[1].toInt()[0] == 42 assert attrs[1] == 42
assert not attrs[2].isValid() assert attrs[2] is None
# check feature at id # check feature at id
f2 = layer.getFeatures(QgsFeatureRequest( f.id() )).next() f2 = layer.getFeatures(QgsFeatureRequest( f.id() )).next()
assert f2[0].toString() == "hello" assert f2[0] == "hello"
assert f2[1].toInt()[0] == 42 assert f2[1] == 42
assert not f2[2].isValid() assert f2[2] is None


layer.startEditing() layer.startEditing()


Expand Down Expand Up @@ -602,7 +599,7 @@ def test_DeleteAttribute(self):
layer.dataProvider().addAttributes( layer.dataProvider().addAttributes(
[QgsField("flddouble", QVariant.Double, "double")] ) [QgsField("flddouble", QVariant.Double, "double")] )
layer.dataProvider().changeAttributeValues( layer.dataProvider().changeAttributeValues(
{1:{2: QVariant(5.5)}}) {1:{2: 5.5}})


# without editing mode # without editing mode
assert not layer.deleteAttribute(0) assert not layer.deleteAttribute(0)
Expand All @@ -617,9 +614,9 @@ def checkBefore():
f = layer.getFeatures().next() f = layer.getFeatures().next()
attrs = f.attributes() attrs = f.attributes()
assert len(attrs) == 3 assert len(attrs) == 3
assert attrs[0].toString() == "test" assert attrs[0] == "test"
assert attrs[1].toInt()[0] == 123 assert attrs[1] == 123
assert attrs[2].toDouble()[0] == 5.5 assert attrs[2] == 5.5


layer.startEditing() layer.startEditing()


Expand All @@ -638,8 +635,8 @@ def checkAfterOneDelete():
f = layer.getFeatures().next() f = layer.getFeatures().next()
attrs = f.attributes() attrs = f.attributes()
assert len(attrs) == 2 assert len(attrs) == 2
assert attrs[0].toInt()[0] == 123 assert attrs[0] == 123
assert attrs[1].toDouble()[0] == 5.5 assert attrs[1] == 5.5


checkAfterOneDelete() checkAfterOneDelete()


Expand All @@ -656,11 +653,11 @@ def checkAfterTwoDeletes():
f = layer.getFeatures().next() f = layer.getFeatures().next()
attrs = f.attributes() attrs = f.attributes()
assert len(attrs) == 1 assert len(attrs) == 1
assert attrs[0].toDouble()[0] == 5.5 assert attrs[0] == 5.5
# check feature at id # check feature at id
f2 = layer.getFeatures(QgsFeatureRequest( f.id() )).next() f2 = layer.getFeatures(QgsFeatureRequest( f.id() )).next()
assert len(f2.attributes()) == 1 assert len(f2.attributes()) == 1
assert f2[0].toDouble()[0] == 5.5 assert f2[0] == 5.5


checkAfterTwoDeletes() checkAfterTwoDeletes()
layer.undoStack().undo() layer.undoStack().undo()
Expand Down Expand Up @@ -689,13 +686,13 @@ def checkAfter(): # layer should be unchanged
f = layer.getFeatures().next() f = layer.getFeatures().next()
attrs = f.attributes() attrs = f.attributes()
assert len(attrs) == 2 assert len(attrs) == 2
assert attrs[0].toString() == "test" assert attrs[0] == "test"
assert attrs[1].toInt()[0] == 123 assert attrs[1] == 123
# check feature at id # check feature at id
f2 = layer.getFeatures(QgsFeatureRequest( f.id() )).next() f2 = layer.getFeatures(QgsFeatureRequest( f.id() )).next()
assert len(f2.attributes()) == 2 assert len(f2.attributes()) == 2
assert f2[0].toString() == "test" assert f2[0] == "test"
assert f2[1].toInt()[0] == 123 assert f2[1] == 123


checkAfter() checkAfter()


Expand Down Expand Up @@ -723,7 +720,7 @@ def test_DeleteAttributeAfterAddFeature(self):


newF = QgsFeature() newF = QgsFeature()
newF.setGeometry( QgsGeometry.fromPoint(QgsPoint(1,1)) ) newF.setGeometry( QgsGeometry.fromPoint(QgsPoint(1,1)) )
newF.setAttributes( [ QVariant("hello"), QVariant(42) ] ) newF.setAttributes(["hello", 42])


def checkBefore(): def checkBefore():
assert len(layer.pendingFields()) == 2 assert len(layer.pendingFields()) == 2
Expand All @@ -736,16 +733,16 @@ def checkAfter1():
f = layer.getFeatures().next() f = layer.getFeatures().next()
attrs = f.attributes() attrs = f.attributes()
assert len(attrs) == 2 assert len(attrs) == 2
assert attrs[0].toString() == "hello" assert attrs[0] == "hello"
assert attrs[1].toInt()[0] == 42 assert attrs[1] == 42


def checkAfter2(): def checkAfter2():
assert len(layer.pendingFields()) == 1 assert len(layer.pendingFields()) == 1
# check feature # check feature
f = layer.getFeatures().next() f = layer.getFeatures().next()
attrs = f.attributes() attrs = f.attributes()
assert len(attrs) == 1 assert len(attrs) == 1
assert attrs[0].toInt()[0] == 42 assert attrs[0] == 42


layer.startEditing() layer.startEditing()


Expand Down Expand Up @@ -777,29 +774,29 @@ def checkBefore():
f = layer.getFeatures().next() f = layer.getFeatures().next()
attrs = f.attributes() attrs = f.attributes()
assert len(attrs) == 2 assert len(attrs) == 2
assert attrs[0].toString() == "test" assert attrs[0] == "test"
assert attrs[1].toInt()[0] == 123 assert attrs[1] == 123


def checkAfter1(): def checkAfter1():
# check feature # check feature
f = layer.getFeatures().next() f = layer.getFeatures().next()
attrs = f.attributes() attrs = f.attributes()
assert len(attrs) == 2 assert len(attrs) == 2
assert attrs[0].toString() == "changed" assert attrs[0] == "changed"
assert attrs[1].toInt()[0] == 123 assert attrs[1] == 123


def checkAfter2(): def checkAfter2():
# check feature # check feature
f = layer.getFeatures().next() f = layer.getFeatures().next()
attrs = f.attributes() attrs = f.attributes()
assert len(attrs) == 1 assert len(attrs) == 1
assert attrs[0].toInt()[0] == 123 assert attrs[0] == 123


layer.startEditing() layer.startEditing()


checkBefore() checkBefore()


assert layer.changeAttributeValue(1, 0, QVariant("changed")) assert layer.changeAttributeValue(1, 0, "changed")
checkAfter1() checkAfter1()
assert layer.deleteAttribute(0) assert layer.deleteAttribute(0)
checkAfter2() checkAfter2()
Expand Down Expand Up @@ -870,16 +867,16 @@ def test_join(self):
assert fi.nextFeature(f) == True assert fi.nextFeature(f) == True
attrs = f.attributes() attrs = f.attributes()
assert len(attrs) == 4 assert len(attrs) == 4
assert attrs[0].toString() == "test" assert attrs[0] == "test"
assert attrs[1].toInt()[0] == 123 assert attrs[1] == 123
assert attrs[2].toString() == "foo" assert attrs[2] == "foo"
assert attrs[3].toInt()[0] == 321 assert attrs[3] == 321
assert fi.nextFeature(f) == False assert fi.nextFeature(f) == False


f2 = layer.getFeatures(QgsFeatureRequest( f.id() )).next() f2 = layer.getFeatures(QgsFeatureRequest( f.id() )).next()
assert len(f2.attributes()) == 4 assert len(f2.attributes()) == 4
assert f2[2].toString() == "foo" assert f2[2] == "foo"
assert f2[3].toInt()[0] == 321 assert f2[3] == 321


def test_InvalidOperations(self): def test_InvalidOperations(self):
layer = createLayerWithOnePoint() layer = createLayerWithOnePoint()
Expand All @@ -905,8 +902,8 @@ def test_InvalidOperations(self):


# CHANGE VALUE # CHANGE VALUE


assert not layer.changeAttributeValue(-333, 0, QVariant(1)) assert not layer.changeAttributeValue(-333, 0, 1)
assert not layer.changeAttributeValue(1, -1, QVariant(1)) assert not layer.changeAttributeValue(1, -1, 1)


# ADD ATTRIBUTE # ADD ATTRIBUTE


Expand Down

0 comments on commit ab4fc78

Please sign in to comment.