Skip to content

Commit

Permalink
Add provider test to ensure that features added with missing attributes
Browse files Browse the repository at this point in the history
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 6d3b37a
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tests/src/python/providertestbase.py
Expand Up @@ -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
Expand Down

0 comments on commit 6d3b37a

Please sign in to comment.