Skip to content
Browse files
Add provider test to ensure that features added with missing attributes
are transparently padded out with NULL attributes to the required fields

Currently the behavior is inconsistent - some providers reject these
features, others pad them out, and worse -- some add them with
missing attributes (memory provider), causing ALL sorts of flow-on,
difficult to debug issues.
  • Loading branch information
nyalldawson committed Feb 16, 2018
1 parent 8628ba1 commit 6d3b37a
Showing 1 changed file with 27 additions and 0 deletions.
@@ -440,6 +440,33 @@ def testAddFeatureFastInsert(self):
self.assertTrue(result, 'Provider reported AddFeatures capability, but returned False to addFeatures')
self.assertEqual(l.dataProvider().featureCount(), 7)

def testAddFeatureMissingAttributes(self):
if not getattr(self, 'getEditableLayer', None):

l = self.getEditableLayer()

if not l.dataProvider().capabilities() & QgsVectorDataProvider.AddFeatures:

# test that adding features with missing attributes pads out these
# attributes with NULL values to the correct length
f1 = QgsFeature()
f1.setAttributes([6, -220, NULL, 'String'])
f2 = QgsFeature()
f2.setAttributes([7, 330])

result, added = l.dataProvider().addFeatures([f1, f2])
self.assertTrue(result, 'Provider returned False to addFeatures with missing attributes. Providers should accept these features but add NULL attributes to the end of the existing attributes to the required field length.')

# check result - feature attributes MUST be padded out to required number of fields
f1.setAttributes([6, -220, NULL, 'String', NULL])
f2.setAttributes([7, 330, NULL, NULL, NULL])
self.testGetFeatures(l.dataProvider(), [f1, f2])

def testAddFeaturesUpdateExtent(self):
if not getattr(self, 'getEditableLayer', None):

0 comments on commit 6d3b37a

Please sign in to comment.