Skip to content
Permalink
Browse files

Test for regression #21304 "default value" is not applied when pastin…

…g features
  • Loading branch information
elpaso committed Feb 20, 2019
1 parent bf1575f commit 2604fc70c54a040be23506c8583158a6edc6a17f
Showing with 31 additions and 2 deletions.
  1. +31 −2 tests/src/python/test_qgsvectorlayerutils.py
@@ -574,8 +574,6 @@ def test_create_multiple_unique_constraint(self):
"""Test create multiple features with unique constraint"""

vl = createLayerWithOnePoint()

# field expression check
vl.setFieldConstraint(1, QgsFieldConstraints.ConstraintUnique)

features_data = []
@@ -587,6 +585,37 @@ def test_create_multiple_unique_constraint(self):
self.assertEqual(features[0].attributes()[1], 124)
self.assertEqual(features[1].attributes()[1], 125)

def test_create_nulls_and_defaults(self):
"""Test bug #21304 when pasting features from another layer and default values are not honored"""

vl = createLayerWithOnePoint()
vl.setDefaultValueDefinition(1, QgsDefaultValue('300'))

features_data = []
context = vl.createExpressionContext()
features_data.append(QgsVectorLayerUtils.QgsFeatureData(QgsGeometry.fromWkt('Point (7 44)'), {0: 'test_1', 1: None}))
features_data.append(QgsVectorLayerUtils.QgsFeatureData(QgsGeometry.fromWkt('Point (7 45)'), {0: 'test_2', 1: QVariant()}))
features_data.append(QgsVectorLayerUtils.QgsFeatureData(QgsGeometry.fromWkt('Point (7 46)'), {0: 'test_3', 1: QVariant(QVariant.Int)}))
features_data.append(QgsVectorLayerUtils.QgsFeatureData(QgsGeometry.fromWkt('Point (7 46)'), {0: 'test_4'}))
features = QgsVectorLayerUtils.createFeatures(vl, features_data, context)

for f in features:
self.assertEqual(f.attributes()[1], 300, f.id())

vl = createLayerWithOnePoint()
vl.setDefaultValueDefinition(0, QgsDefaultValue("'my_default'"))

features_data = []
context = vl.createExpressionContext()
features_data.append(QgsVectorLayerUtils.QgsFeatureData(QgsGeometry.fromWkt('Point (7 44)'), {0: None}))
features_data.append(QgsVectorLayerUtils.QgsFeatureData(QgsGeometry.fromWkt('Point (7 45)'), {0: QVariant()}))
features_data.append(QgsVectorLayerUtils.QgsFeatureData(QgsGeometry.fromWkt('Point (7 46)'), {0: QVariant(QVariant.String)}))
features_data.append(QgsVectorLayerUtils.QgsFeatureData(QgsGeometry.fromWkt('Point (7 46)'), {}))
features = QgsVectorLayerUtils.createFeatures(vl, features_data, context)

for f in features:
self.assertEqual(f.attributes()[0], 'my_default', f.id())


if __name__ == '__main__':
unittest.main()

0 comments on commit 2604fc7

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