Skip to content
Permalink
Browse files

Add provider test to ensure that features added with missing attributes

are transparently padded out with NULL attributes to the required fields
length

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 6d3b37a85e3a3f2ac713f38e3bf8683e7a4db87d
Showing with 27 additions and 0 deletions.
  1. +27 −0 tests/src/python/providertestbase.py
@@ -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):
return

l = self.getEditableLayer()
self.assertTrue(l.isValid())

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

# 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.')
f1.setId(added[0].id())
f2.setId(added[1].id())

# 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):
return

0 comments on commit 6d3b37a

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