Skip to content
Permalink
Browse files

Add test

  • Loading branch information
pblottiere committed Nov 13, 2017
1 parent 3bbe71d commit caf0cf757671aeec37b3e0fb9b2ed5c7754e2e07
Showing with 40 additions and 0 deletions.
  1. +40 −0 tests/src/python/test_qgsvectorfilewriter.py
@@ -93,6 +93,46 @@ def testWrite(self):

writeShape(self.mMemoryLayer, 'writetest.shp')

def testWriteWithBoolField(self):

# init connection string
dbconn = 'dbname=\'qgis_test\''
if 'QGIS_PGTEST_DB' in os.environ:
dbconn = os.environ['QGIS_PGTEST_DB']

# create a vector layer
vl = QgsVectorLayer('{} table="qgis_test"."boolean_table" sql='.format(dbconn), "testbool", "postgres")
self.assertTrue(vl.isValid())

# check that 1 of its fields is a bool
fields = vl.fields()
self.assertEqual(fields.at(fields.indexFromName('fld1')).type(), QVariant.Bool)

# write a gpkg package with a bool field
crs = QgsCoordinateReferenceSystem()
crs.createFromId(4326, QgsCoordinateReferenceSystem.EpsgCrsId)
filename = os.path.join(str(QDir.tempPath()), 'with_bool_field')
rc, errmsg = QgsVectorFileWriter.writeAsVectorFormat(vl,
filename,
'utf-8',
crs,
'GPKG')

self.assertEqual(rc, QgsVectorFileWriter.NoError)

# open the resulting geopackage
vl = QgsVectorLayer(filename + '.gpkg', '', 'ogr')
self.assertTrue(vl.isValid())
fields = vl.fields()

# test type of converted field
idx = fields.indexFromName('fld1')
self.assertEqual(fields.at(idx).type(), QVariant.Int)

# test values
self.assertEqual(vl.getFeature(1).attributes()[idx], 1)
self.assertEqual(vl.getFeature(2).attributes()[idx], 0)

def testDateTimeWriteShapefile(self):
"""Check writing date and time fields to an ESRI shapefile."""
ml = QgsVectorLayer(

0 comments on commit caf0cf7

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