@@ -369,6 +369,41 @@ def testEditGeoJsonAddField(self):
369
369
# all in the GeoJSON file, so it has disappeared
370
370
self .assertEqual (len (vl .fields ()), 1 )
371
371
372
+ def testEditGeoJsonAddFieldAndThenAddFeatures (self ):
373
+ """ Test bugfix of https://issues.qgis.org/issues/18596 (adding a new field)"""
374
+
375
+ datasource = os .path .join (self .basetestpath , 'testEditGeoJsonAddField.json' )
376
+ with open (datasource , 'wt' ) as f :
377
+ f .write ("""{
378
+ "type": "FeatureCollection",
379
+ "features": [
380
+ { "type": "Feature", "properties": { "x": 1 }, "geometry": { "type": "Point", "coordinates": [ 0, 0 ] } } ] }""" )
381
+
382
+ vl = QgsVectorLayer (datasource , 'test' , 'ogr' )
383
+ self .assertTrue (vl .isValid ())
384
+ self .assertTrue (vl .startEditing ())
385
+ self .assertTrue (vl .addAttribute (QgsField ('strfield' , QVariant .String )))
386
+ self .assertTrue (vl .commitChanges ())
387
+ self .assertEqual (len (vl .dataProvider ().fields ()), 1 + 1 )
388
+ self .assertEqual ([f .name () for f in vl .dataProvider ().fields ()], ['x' , 'strfield' ])
389
+
390
+ f = QgsFeature ()
391
+ self .assertTrue (vl .getFeatures (QgsFeatureRequest ()).nextFeature (f ))
392
+ self .assertIsNone (f ['strfield' ])
393
+ self .assertEqual ([field .name () for field in f .fields ()], ['x' , 'strfield' ])
394
+
395
+ self .assertTrue (vl .startEditing ())
396
+ vl .changeAttributeValue (f .id (), 1 , 'x' )
397
+ self .assertTrue (vl .commitChanges ())
398
+ f = QgsFeature ()
399
+ self .assertTrue (vl .getFeatures (QgsFeatureRequest ()).nextFeature (f ))
400
+ self .assertEqual (f ['strfield' ], 'x' )
401
+ self .assertEqual ([field .name () for field in f .fields ()], ['x' , 'strfield' ])
402
+
403
+ # Completely reload file
404
+ vl = QgsVectorLayer (datasource , 'test' , 'ogr' )
405
+ self .assertEqual (len (vl .fields ()), 2 )
406
+
372
407
373
408
if __name__ == '__main__' :
374
409
unittest .main ()
0 commit comments