Fix some incorrect return values from QgsFeature python bindings

This commit is contained in:
Nyall Dawson 2016-11-08 12:35:25 +10:00
parent 6f3b0caa81
commit fcb6c2bb9a
2 changed files with 31 additions and 2 deletions

View File

@ -293,6 +293,8 @@ class QgsFeature
PyErr_SetString(PyExc_KeyError, QByteArray::number(a0)); PyErr_SetString(PyExc_KeyError, QByteArray::number(a0));
sipIsErr = 1; sipIsErr = 1;
} }
sipRes = rv;
%End %End
/** Initialize this feature with the given number of fields. Discard any previously set attribute data. /** 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()); PyErr_SetString(PyExc_KeyError, a0->toAscii());
sipIsErr = 1; sipIsErr = 1;
sipRes = false;
} }
else else
{ {
sipCpp->deleteAttribute( fieldIdx ); sipCpp->deleteAttribute( fieldIdx );
sipRes = true;
} }
%End %End
/** Lookup attribute value from attribute name. Field map must be associated using @link setFields @endlink /** Lookup attribute value from attribute name. Field map must be associated using @link setFields @endlink

View File

@ -15,7 +15,7 @@ __revision__ = '$Format:%H$'
import qgis # NOQA import qgis # NOQA
import os 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 qgis.testing import start_app, unittest
from utilities import unitTestDataPath from utilities import unitTestDataPath
@ -66,13 +66,22 @@ class TestQgsFeature(unittest.TestCase):
assert myAttributes == myExpectedAttributes, myMessage assert myAttributes == myExpectedAttributes, myMessage
def test_SetAttribute(self): def test_SetAttributes(self):
feat = QgsFeature() feat = QgsFeature()
feat.initAttributes(1) feat.initAttributes(1)
feat.setAttributes([0]) feat.setAttributes([0])
feat.setAttributes([NULL]) feat.setAttributes([NULL])
assert [NULL] == feat.attributes() 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): def test_DeleteAttribute(self):
feat = QgsFeature() feat = QgsFeature()
feat.initAttributes(3) feat.initAttributes(3)
@ -85,6 +94,22 @@ class TestQgsFeature(unittest.TestCase):
myMessage = '\nExpected: %s\nGot: %s' % (str(myExpectedAttrs), str(myAttrs)) myMessage = '\nExpected: %s\nGot: %s' % (str(myExpectedAttrs), str(myAttrs))
assert myAttrs == myExpectedAttrs, myMessage 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): def test_SetGeometry(self):
feat = QgsFeature() feat = QgsFeature()
feat.setGeometry(QgsGeometry.fromPoint(QgsPoint(123, 456))) feat.setGeometry(QgsGeometry.fromPoint(QgsPoint(123, 456)))