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
Expand Up @@ -13,6 +13,7 @@
__revision__ = '$Format:%H$'

import os
import qgis
from PyQt4.QtCore import QVariant

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


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


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

return repr([ unicode(a) for a in attrs ])

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


class TestQgsVectorLayer(TestCase):

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

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

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

checkBefore()

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

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

checkAfter()

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

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

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

self.assertRaises(StopIteration, fi.next)

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

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

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

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

checkBefore()

# change geometry
layer.startEditing()
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)))
layer.endEditCommand()

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

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

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

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

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

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

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

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

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

layer.startEditing()

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

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

layer.startEditing()

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

checkAfterOneDelete()

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

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

checkAfter()

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

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

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

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

layer.startEditing()

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

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

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

layer.startEditing()

checkBefore()

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

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

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

# CHANGE VALUE

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

# ADD ATTRIBUTE

Expand Down

0 comments on commit ab4fc78

Please sign in to comment.