Fix QgsVectorLayer::updateFeature returns true when errors occur

Refs #17678
This commit is contained in:
Nyall Dawson 2018-01-04 09:53:28 +10:00
parent c94d26a5d5
commit 5ecb5603df
2 changed files with 40 additions and 8 deletions

View File

@ -974,13 +974,16 @@ bool QgsVectorLayer::addFeature( QgsFeature &feature, Flags )
bool QgsVectorLayer::updateFeature( const QgsFeature &updatedFeature, bool skipDefaultValues )
{
bool hasChanged = false;
bool hasError = false;
if ( !mEditBuffer || !mDataProvider )
{
return false;
}
QgsFeature currentFeature = getFeature( updatedFeature.id() );
if ( currentFeature.isValid() )
{
QgsDebugMsgLevel( QStringLiteral( "feature %1 could not be retrieved" ).arg( updatedFeature.id() ), 3 );
bool hasChanged = false;
bool hasError = false;
if ( updatedFeature.hasGeometry() && currentFeature.hasGeometry() && !updatedFeature.geometry().isGeosEqual( currentFeature.geometry() ) )
{
@ -1012,13 +1015,17 @@ bool QgsVectorLayer::updateFeature( const QgsFeature &updatedFeature, bool skipD
}
}
}
}
if ( hasChanged && !mDefaultValueOnUpdateFields.isEmpty() && !skipDefaultValues )
updateDefaultValues( updatedFeature.id(), updatedFeature );
return !hasError;
}
else
{
QgsDebugMsgLevel( QStringLiteral( "feature %1 could not be retrieved" ).arg( updatedFeature.id() ), 3 );
return false;
}
}
bool QgsVectorLayer::insertVertex( double x, double y, QgsFeatureId atFeatureId, int beforeVertex )

View File

@ -721,6 +721,31 @@ class TestQgsVectorLayer(unittest.TestCase, FeatureSourceTestCase):
# print "COMMIT ERRORS:"
# for item in list(layer.commitErrors()): print item
# updateFeature
def testUpdateFeature(self):
layer = createLayerWithFivePoints()
features = [f for f in layer.getFeatures()]
# try to change feature without editing mode
self.assertFalse(layer.updateFeature(features[0]))
layer.startEditing()
# no matching feature
f = QgsFeature(1123)
self.assertFalse(layer.updateFeature(f))
# change geometry and attributes
f = features[0]
f.setAttributes(['new',321])
f.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(-200, -200)))
self.assertTrue(layer.updateFeature(f))
new_feature = next(layer.getFeatures(QgsFeatureRequest(f.id())))
self.assertEqual(new_feature.attributes(), ['new',321])
self.assertEqual(new_feature.geometry().asPoint(), QgsPointXY(-200, -200))
# ADD ATTRIBUTE
def test_AddAttribute(self):