Skip to content
Permalink
Browse files

Extract postgres specific from vectorfilewriter test

  • Loading branch information
troopa81 committed Mar 23, 2021
1 parent 05e56fc commit 70d821752c3df09724510492e432194f2c930507
@@ -312,6 +312,7 @@ ADD_PYTHON_TEST(PyQgsUnitTypes test_qgsunittypes.py)
ADD_PYTHON_TEST(PyQgsValidityChecks test_qgsvaliditychecks.py)
ADD_PYTHON_TEST(PyQgsValidityResultsWidget test_qgsvalidityresultswidget.py)
ADD_PYTHON_TEST(PyQgsVectorFieldMarkerSymbolLayer test_qgsvectorfieldmarkersymbollayer.py)
ADD_PYTHON_TEST(PyQgsVectorFileWriter test_qgsvectorfilewriter.py)
ADD_PYTHON_TEST(PyQgsVectorFileWriterTask test_qgsvectorfilewritertask.py)
ADD_PYTHON_TEST(PyQgsVectorLayer test_qgsvectorlayer.py)
ADD_PYTHON_TEST(PyQgsVectorLayerFeatureCounter test_qgsvectorlayerfeaturecounter.py)
@@ -365,7 +366,7 @@ endif()
if (ENABLE_PGTEST)
ADD_PYTHON_TEST(PyQgsImportIntoPostGIS test_processing_importintopostgis.py)
ADD_PYTHON_TEST(PyQgsQueryResultModel test_qgsqueryresultmodel.py)
ADD_PYTHON_TEST(PyQgsVectorFileWriter test_qgsvectorfilewriter.py)
ADD_PYTHON_TEST(PyQgsVectorFileWriterPostgres test_qgsvectorfilewriter_postgres.py)
ADD_PYTHON_TEST(PyQgsVectorLayerUtils test_qgsvectorlayerutils.py)
ADD_PYTHON_TEST(PyQgsPostgresProvider test_provider_postgres.py)
ADD_PYTHON_TEST(PyQgsPostgresRasterProvider test_provider_postgresraster.py)
@@ -390,7 +391,7 @@ if (ENABLE_PGTEST)
ADD_PYTHON_TEST(PyQgsServerWMSGetFeatureInfo test_qgsserver_wms_getfeatureinfo.py)
ADD_PYTHON_TEST(PyQgsServerAccessControlWMSGetPrintPG test_qgsserver_accesscontrol_wms_getprint_postgres.py)
endif()
SET_TESTS_PROPERTIES(PyQgsImportIntoPostGIS PyQgsQueryResultModel PyQgsVectorFileWriter PyQgsPostgresProvider
SET_TESTS_PROPERTIES(PyQgsImportIntoPostGIS PyQgsQueryResultModel PyQgsVectorFileWriterPostgres PyQgsPostgresProvider
PyQgsVectorLayerUtils PyQgsPostgresRasterProvider PyQgsPostgresDomain PyQgsPostgresTransaction
PyQgsRelationEditWidget PyQgsRelationPostgres PyQgsVectorLayerTools PyQgsProjectStoragePostgres
PyQgsAuthManagerPKIPostgresTest PyQgsAuthManagerPasswordPostgresTest PyQgsAuthManagerOgrPostgresTest
@@ -172,48 +172,6 @@ def testWriteWithLongLongField(self):
del vl
os.unlink(filename + '.gpkg')

def testWriteWithBoolField(self):

# init connection string
dbconn = 'service=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('EPSG:4326')
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.Bool)

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

del vl
os.unlink(filename + '.gpkg')

def testDateTimeWriteShapefile(self):
"""Check writing date and time fields to an ESRI shapefile."""
ml = QgsVectorLayer(
@@ -0,0 +1,76 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsVectorFileWriter with a PostGres database.
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
from builtins import next
from builtins import str

__author__ = 'Julien Cabieces'
__date__ = '22/03/2021'
__copyright__ = 'Copyright 2021, The QGIS Project'

import qgis # NOQA

from qgis.core import (QgsVectorLayer,
QgsCoordinateReferenceSystem,
QgsVectorFileWriter)
from qgis.PyQt.QtCore import QVariant, QDir
import os
from qgis.testing import start_app, unittest
from utilities import unitTestDataPath

TEST_DATA_DIR = unitTestDataPath()
start_app()


class TestQgsVectorFileWriterPG(unittest.TestCase):

def testWriteWithBoolField(self):

# init connection string
dbconn = 'service=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('EPSG:4326')
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.Bool)

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

del vl
os.unlink(filename + '.gpkg')


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

0 comments on commit 70d8217

Please sign in to comment.