Skip to content

Commit 6d3b37a

Browse files
committed
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.
1 parent 8628ba1 commit 6d3b37a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

tests/src/python/providertestbase.py

+27
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,33 @@ def testAddFeatureFastInsert(self):
440440
self.assertTrue(result, 'Provider reported AddFeatures capability, but returned False to addFeatures')
441441
self.assertEqual(l.dataProvider().featureCount(), 7)
442442

443+
def testAddFeatureMissingAttributes(self):
444+
if not getattr(self, 'getEditableLayer', None):
445+
return
446+
447+
l = self.getEditableLayer()
448+
self.assertTrue(l.isValid())
449+
450+
if not l.dataProvider().capabilities() & QgsVectorDataProvider.AddFeatures:
451+
return
452+
453+
# test that adding features with missing attributes pads out these
454+
# attributes with NULL values to the correct length
455+
f1 = QgsFeature()
456+
f1.setAttributes([6, -220, NULL, 'String'])
457+
f2 = QgsFeature()
458+
f2.setAttributes([7, 330])
459+
460+
result, added = l.dataProvider().addFeatures([f1, f2])
461+
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.')
462+
f1.setId(added[0].id())
463+
f2.setId(added[1].id())
464+
465+
# check result - feature attributes MUST be padded out to required number of fields
466+
f1.setAttributes([6, -220, NULL, 'String', NULL])
467+
f2.setAttributes([7, 330, NULL, NULL, NULL])
468+
self.testGetFeatures(l.dataProvider(), [f1, f2])
469+
443470
def testAddFeaturesUpdateExtent(self):
444471
if not getattr(self, 'getEditableLayer', None):
445472
return

0 commit comments

Comments
 (0)