Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Extract postgres specific from PyQgsVectorLayerUtils test
  • Loading branch information
troopa81 committed Mar 23, 2021
1 parent 70d8217 commit 710ec78
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 39 deletions.
5 changes: 3 additions & 2 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -323,6 +323,7 @@ ADD_PYTHON_TEST(PyQgsVectorLayerRenderer test_qgsvectorlayerrenderer.py)
ADD_PYTHON_TEST(PyQgsVectorLayerSelectedFeatureSource test_qgsvectorlayerselectedfeaturesource.py)
ADD_PYTHON_TEST(PyQgsVectorLayerShapefile test_qgsvectorlayershapefile.py)
ADD_PYTHON_TEST(PyQgsVectorLayerTemporalProperties test_qgsvectorlayertemporalproperties.py)
ADD_PYTHON_TEST(PyQgsVectorLayerUtils test_qgsvectorlayerutils.py)
ADD_PYTHON_TEST(PyQgsZonalStatistics test_qgszonalstatistics.py)
ADD_PYTHON_TEST(PyQgsVirtualLayerProvider test_provider_virtual.py)
ADD_PYTHON_TEST(PyQgsVirtualLayerTask test_qgsvirtuallayertask.py)
Expand Down Expand Up @@ -367,7 +368,7 @@ if (ENABLE_PGTEST)
ADD_PYTHON_TEST(PyQgsImportIntoPostGIS test_processing_importintopostgis.py)
ADD_PYTHON_TEST(PyQgsQueryResultModel test_qgsqueryresultmodel.py)
ADD_PYTHON_TEST(PyQgsVectorFileWriterPostgres test_qgsvectorfilewriter_postgres.py)
ADD_PYTHON_TEST(PyQgsVectorLayerUtils test_qgsvectorlayerutils.py)
ADD_PYTHON_TEST(PyQgsVectorLayerUtilsPostgres test_qgsvectorlayerutils_postgres.py)
ADD_PYTHON_TEST(PyQgsPostgresProvider test_provider_postgres.py)
ADD_PYTHON_TEST(PyQgsPostgresRasterProvider test_provider_postgresraster.py)
ADD_PYTHON_TEST(PyQgsPostgresDomain test_qgspostgresdomain.py)
Expand All @@ -392,7 +393,7 @@ if (ENABLE_PGTEST)
ADD_PYTHON_TEST(PyQgsServerAccessControlWMSGetPrintPG test_qgsserver_accesscontrol_wms_getprint_postgres.py)
endif()
SET_TESTS_PROPERTIES(PyQgsImportIntoPostGIS PyQgsQueryResultModel PyQgsVectorFileWriterPostgres PyQgsPostgresProvider
PyQgsVectorLayerUtils PyQgsPostgresRasterProvider PyQgsPostgresDomain PyQgsPostgresTransaction
PyQgsVectorLayerUtilsPostgres PyQgsPostgresRasterProvider PyQgsPostgresDomain PyQgsPostgresTransaction
PyQgsRelationEditWidget PyQgsRelationPostgres PyQgsVectorLayerTools PyQgsProjectStoragePostgres
PyQgsAuthManagerPKIPostgresTest PyQgsAuthManagerPasswordPostgresTest PyQgsAuthManagerOgrPostgresTest
PyQgsDbManagerPostgis PyQgsDatabaseSchemaModel PyQgsDatabaseTableModel PyQgsDatabaseSchemaComboBox PyQgsDatabaseTableComboBox
Expand Down
37 changes: 0 additions & 37 deletions tests/src/python/test_qgsvectorlayerutils.py
Expand Up @@ -472,43 +472,6 @@ def testCreateFeature(self):
f = QgsVectorLayerUtils.createFeature(layer, attributes={0: 'test_1', 1: 132})
self.assertEqual(f.attributes(), ['test_5', 132, NULL])

""" test creating a feature respecting unique values of postgres provider """
layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer&field=flddbl:double",
"addfeat", "memory")

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

# create a vector layer
pg_layer = QgsVectorLayer('{} table="qgis_test"."authors" sql='.format(dbconn), "authors", "postgres")
self.assertTrue(pg_layer.isValid())
# check the default clause
default_clause = 'nextval(\'qgis_test.authors_pk_seq\'::regclass)'
self.assertEqual(pg_layer.dataProvider().defaultValueClause(0), default_clause)

# though default_clause is after the first create not unique (until save), it should fill up all the features with it
pg_layer.startEditing()
f = QgsVectorLayerUtils.createFeature(pg_layer)
self.assertEqual(f.attributes(), [default_clause, NULL])
self.assertTrue(pg_layer.addFeatures([f]))
self.assertTrue(QgsVectorLayerUtils.valueExists(pg_layer, 0, default_clause))
f = QgsVectorLayerUtils.createFeature(pg_layer)
self.assertEqual(f.attributes(), [default_clause, NULL])
self.assertTrue(pg_layer.addFeatures([f]))
f = QgsVectorLayerUtils.createFeature(pg_layer)
self.assertEqual(f.attributes(), [default_clause, NULL])
self.assertTrue(pg_layer.addFeatures([f]))
# if a unique value is passed, use it
f = QgsVectorLayerUtils.createFeature(pg_layer, attributes={0: 40, 1: NULL})
self.assertEqual(f.attributes(), [40, NULL])
# and if a default value is configured use it as well
pg_layer.setDefaultValueDefinition(0, QgsDefaultValue('11*4'))
f = QgsVectorLayerUtils.createFeature(pg_layer)
self.assertEqual(f.attributes(), [44, NULL])
pg_layer.rollBack()

def testDuplicateFeature(self):
""" test duplicating a feature """

Expand Down
68 changes: 68 additions & 0 deletions tests/src/python/test_qgsvectorlayerutils_postgres.py
@@ -0,0 +1,68 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsVectorLayerUtils in 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.
"""
__author__ = 'Julien Cabieces'
__date__ = '22/03/2021'
__copyright__ = 'Copyright 2021, The QGIS Project'

import os
import qgis # NOQA

from qgis.core import (QgsVectorLayer,
QgsVectorLayerUtils,
QgsDefaultValue,
NULL
)
from qgis.testing import start_app, unittest

start_app()


class TestQgsVectorLayerUtilsPostgres(unittest.TestCase):

def testCreateFeature(self):
""" test creating a feature respecting unique values of postgres provider """
layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer&field=flddbl:double",
"addfeat", "memory")

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

# create a vector layer
pg_layer = QgsVectorLayer('{} table="qgis_test"."authors" sql='.format(dbconn), "authors", "postgres")
self.assertTrue(pg_layer.isValid())
# check the default clause
default_clause = 'nextval(\'qgis_test.authors_pk_seq\'::regclass)'
self.assertEqual(pg_layer.dataProvider().defaultValueClause(0), default_clause)

# though default_clause is after the first create not unique (until save), it should fill up all the features with it
pg_layer.startEditing()
f = QgsVectorLayerUtils.createFeature(pg_layer)
self.assertEqual(f.attributes(), [default_clause, NULL])
self.assertTrue(pg_layer.addFeatures([f]))
self.assertTrue(QgsVectorLayerUtils.valueExists(pg_layer, 0, default_clause))
f = QgsVectorLayerUtils.createFeature(pg_layer)
self.assertEqual(f.attributes(), [default_clause, NULL])
self.assertTrue(pg_layer.addFeatures([f]))
f = QgsVectorLayerUtils.createFeature(pg_layer)
self.assertEqual(f.attributes(), [default_clause, NULL])
self.assertTrue(pg_layer.addFeatures([f]))
# if a unique value is passed, use it
f = QgsVectorLayerUtils.createFeature(pg_layer, attributes={0: 40, 1: NULL})
self.assertEqual(f.attributes(), [40, NULL])
# and if a default value is configured use it as well
pg_layer.setDefaultValueDefinition(0, QgsDefaultValue('11*4'))
f = QgsVectorLayerUtils.createFeature(pg_layer)
self.assertEqual(f.attributes(), [44, NULL])
pg_layer.rollBack()


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

0 comments on commit 710ec78

Please sign in to comment.