Skip to content

Commit

Permalink
Fix QgsVectorLayer::updateFeature returns true when errors occur
Browse files Browse the repository at this point in the history
Refs #17678
  • Loading branch information
nyalldawson committed Jan 4, 2018
1 parent c94d26a commit 5ecb560
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -974,13 +974,16 @@ bool QgsVectorLayer::addFeature( QgsFeature &feature, Flags )


bool QgsVectorLayer::updateFeature( const QgsFeature &updatedFeature, bool skipDefaultValues ) bool QgsVectorLayer::updateFeature( const QgsFeature &updatedFeature, bool skipDefaultValues )
{ {
bool hasChanged = false; if ( !mEditBuffer || !mDataProvider )
bool hasError = false; {
return false;
}


QgsFeature currentFeature = getFeature( updatedFeature.id() ); QgsFeature currentFeature = getFeature( updatedFeature.id() );
if ( currentFeature.isValid() ) 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() ) ) if ( updatedFeature.hasGeometry() && currentFeature.hasGeometry() && !updatedFeature.geometry().isGeosEqual( currentFeature.geometry() ) )
{ {
Expand Down Expand Up @@ -1012,12 +1015,16 @@ bool QgsVectorLayer::updateFeature( const QgsFeature &updatedFeature, bool skipD
} }
} }
} }
} if ( hasChanged && !mDefaultValueOnUpdateFields.isEmpty() && !skipDefaultValues )

updateDefaultValues( updatedFeature.id(), updatedFeature );
if ( hasChanged && !mDefaultValueOnUpdateFields.isEmpty() && !skipDefaultValues )
updateDefaultValues( updatedFeature.id(), updatedFeature );


return !hasError; return !hasError;
}
else
{
QgsDebugMsgLevel( QStringLiteral( "feature %1 could not be retrieved" ).arg( updatedFeature.id() ), 3 );
return false;
}
} }




Expand Down
25 changes: 25 additions & 0 deletions tests/src/python/test_qgsvectorlayer.py
Expand Up @@ -721,6 +721,31 @@ def checkBefore():
# print "COMMIT ERRORS:" # print "COMMIT ERRORS:"
# for item in list(layer.commitErrors()): print item # 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 # ADD ATTRIBUTE


def test_AddAttribute(self): def test_AddAttribute(self):
Expand Down

0 comments on commit 5ecb560

Please sign in to comment.