Skip to content
Permalink
Browse files

Fix some incorrect return values from QgsFeature python bindings

  • Loading branch information
nyalldawson committed Nov 8, 2016
1 parent 6f3b0ca commit fcb6c2bb9ae17663047134d41befaac7b45eb048
Showing with 31 additions and 2 deletions.
  1. +4 −0 python/core/qgsfeature.sip
  2. +27 −2 tests/src/python/test_qgsfeature.py
@@ -293,6 +293,8 @@ class QgsFeature
PyErr_SetString(PyExc_KeyError, QByteArray::number(a0));
sipIsErr = 1;
}

sipRes = rv;
%End

/** Initialize this feature with the given number of fields. Discard any previously set attribute data.
@@ -412,10 +414,12 @@ class QgsFeature
{
PyErr_SetString(PyExc_KeyError, a0->toAscii());
sipIsErr = 1;
sipRes = false;
}
else
{
sipCpp->deleteAttribute( fieldIdx );
sipRes = true;
}
%End
/** Lookup attribute value from attribute name. Field map must be associated using @link setFields @endlink
@@ -15,7 +15,7 @@
import qgis # NOQA

import os
from qgis.core import QgsFeature, QgsGeometry, QgsPoint, QgsVectorLayer, NULL
from qgis.core import QgsFeature, QgsGeometry, QgsPoint, QgsVectorLayer, NULL, QgsFields, QgsField
from qgis.testing import start_app, unittest
from utilities import unitTestDataPath

@@ -66,13 +66,22 @@ def test_Attributes(self):

assert myAttributes == myExpectedAttributes, myMessage

def test_SetAttribute(self):
def test_SetAttributes(self):
feat = QgsFeature()
feat.initAttributes(1)
feat.setAttributes([0])
feat.setAttributes([NULL])
assert [NULL] == feat.attributes()

def test_setAttribute(self):
feat = QgsFeature()
feat.initAttributes(1)
with self.assertRaises(KeyError):
feat.setAttribute(-1, 5)
with self.assertRaises(KeyError):
feat.setAttribute(10, 5)
self.assertTrue(feat.setAttribute(0, 5))

def test_DeleteAttribute(self):
feat = QgsFeature()
feat.initAttributes(3)
@@ -85,6 +94,22 @@ def test_DeleteAttribute(self):
myMessage = '\nExpected: %s\nGot: %s' % (str(myExpectedAttrs), str(myAttrs))
assert myAttrs == myExpectedAttrs, myMessage

def test_DeleteAttributeByName(self):
fields = QgsFields()
field1 = QgsField('my_field')
fields.append(field1)
field2 = QgsField('my_field2')
fields.append(field2)

feat = QgsFeature(fields)
feat.initAttributes(2)
feat[0] = "text1"
feat[1] = "text2"
with self.assertRaises(KeyError):
feat.deleteAttribute('not present')
self.assertTrue(feat.deleteAttribute('my_field'))
self.assertEqual(feat.attributes(), ['text2'])

def test_SetGeometry(self):
feat = QgsFeature()
feat.setGeometry(QgsGeometry.fromPoint(QgsPoint(123, 456)))

0 comments on commit fcb6c2b

Please sign in to comment.
You can’t perform that action at this time.