@@ -574,8 +574,6 @@ def test_create_multiple_unique_constraint(self):
574
574
"""Test create multiple features with unique constraint"""
575
575
576
576
vl = createLayerWithOnePoint ()
577
-
578
- # field expression check
579
577
vl .setFieldConstraint (1 , QgsFieldConstraints .ConstraintUnique )
580
578
581
579
features_data = []
@@ -587,6 +585,37 @@ def test_create_multiple_unique_constraint(self):
587
585
self .assertEqual (features [0 ].attributes ()[1 ], 124 )
588
586
self .assertEqual (features [1 ].attributes ()[1 ], 125 )
589
587
588
+ def test_create_nulls_and_defaults (self ):
589
+ """Test bug #21304 when pasting features from another layer and default values are not honored"""
590
+
591
+ vl = createLayerWithOnePoint ()
592
+ vl .setDefaultValueDefinition (1 , QgsDefaultValue ('300' ))
593
+
594
+ features_data = []
595
+ context = vl .createExpressionContext ()
596
+ features_data .append (QgsVectorLayerUtils .QgsFeatureData (QgsGeometry .fromWkt ('Point (7 44)' ), {0 : 'test_1' , 1 : None }))
597
+ features_data .append (QgsVectorLayerUtils .QgsFeatureData (QgsGeometry .fromWkt ('Point (7 45)' ), {0 : 'test_2' , 1 : QVariant ()}))
598
+ features_data .append (QgsVectorLayerUtils .QgsFeatureData (QgsGeometry .fromWkt ('Point (7 46)' ), {0 : 'test_3' , 1 : QVariant (QVariant .Int )}))
599
+ features_data .append (QgsVectorLayerUtils .QgsFeatureData (QgsGeometry .fromWkt ('Point (7 46)' ), {0 : 'test_4' }))
600
+ features = QgsVectorLayerUtils .createFeatures (vl , features_data , context )
601
+
602
+ for f in features :
603
+ self .assertEqual (f .attributes ()[1 ], 300 , f .id ())
604
+
605
+ vl = createLayerWithOnePoint ()
606
+ vl .setDefaultValueDefinition (0 , QgsDefaultValue ("'my_default'" ))
607
+
608
+ features_data = []
609
+ context = vl .createExpressionContext ()
610
+ features_data .append (QgsVectorLayerUtils .QgsFeatureData (QgsGeometry .fromWkt ('Point (7 44)' ), {0 : None }))
611
+ features_data .append (QgsVectorLayerUtils .QgsFeatureData (QgsGeometry .fromWkt ('Point (7 45)' ), {0 : QVariant ()}))
612
+ features_data .append (QgsVectorLayerUtils .QgsFeatureData (QgsGeometry .fromWkt ('Point (7 46)' ), {0 : QVariant (QVariant .String )}))
613
+ features_data .append (QgsVectorLayerUtils .QgsFeatureData (QgsGeometry .fromWkt ('Point (7 46)' ), {}))
614
+ features = QgsVectorLayerUtils .createFeatures (vl , features_data , context )
615
+
616
+ for f in features :
617
+ self .assertEqual (f .attributes ()[0 ], 'my_default' , f .id ())
618
+
590
619
591
620
if __name__ == '__main__' :
592
621
unittest .main ()
0 commit comments