Skip to content
Permalink
Browse files

Add test for QgsVectorLayerExporter with RegeneratePrimaryKey

  • Loading branch information
m-kuhn committed Oct 31, 2018
1 parent 71590bb commit 432bec17fc6f69be55ba4edd5db76d0231eced87
Showing with 47 additions and 0 deletions.
  1. +47 −0 tests/src/python/test_provider_ogr_gpkg.py
@@ -24,6 +24,7 @@
from qgis.core import (QgsFeature,
QgsCoordinateReferenceSystem,
QgsFeatureRequest,
QgsFeatureSink,
QgsFields,
QgsField,
QgsFieldConstraints,
@@ -1113,6 +1114,52 @@ def testApproxFeatureCountAndExtent(self):
self.assertTrue(QgsGeometry.compare(provider_extent.asPolygon()[0], reference.asPolygon()[0], 0.00001),
provider_extent.asPolygon()[0])

def testRegenerateFid(self):
""" Test regenerating feature ids """

fields = QgsFields()
fields.append(QgsField('fid', QVariant.Int))
fields.append(QgsField('f1', QVariant.Int))
tmpfile = os.path.join(self.basetestpath, 'testRegenerateFid.gpkg')
options = {}
options['update'] = True
options['driverName'] = 'GPKG'
options['layerName'] = 'table1'
exporter = QgsVectorLayerExporter(tmpfile, "ogr", fields, QgsWkbTypes.Polygon, QgsCoordinateReferenceSystem(3111), False, options, QgsFeatureSink.RegeneratePrimaryKey)
self.assertFalse(exporter.errorCode(),
'unexpected export error {}: {}'.format(exporter.errorCode(), exporter.errorMessage()))

feat = QgsFeature(fields)

feat['fid'] = 0
feat['f1'] = 10
exporter.addFeature(feat)

feat['fid'] = 0
feat['f1'] = 20
exporter.addFeature(feat)

feat['fid'] = 1
feat['f1'] = 30
exporter.addFeature(feat)

feat['fid'] = 1
feat['f1'] = 40
exporter.addFeature(feat)

del exporter
# make sure layers exist
lyr = QgsVectorLayer('{}|layername=table1'.format(tmpfile), "lyr1", "ogr")
self.assertTrue(lyr.isValid())
self.assertEqual(lyr.crs().authid(), 'EPSG:3111')
self.assertEqual(lyr.wkbType(), QgsWkbTypes.Polygon)

values = set([f['f1'] for f in lyr.getFeatures()])
self.assertEqual(values, set([10, 20, 30, 40]))

fids = set([f['fid'] for f in lyr.getFeatures()])
self.assertEqual(len(fids), 4)

def testTransaction(self):

tmpfile = os.path.join(self.basetestpath, 'testTransaction.gpkg')

0 comments on commit 432bec1

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