Skip to content

Commit

Permalink
Tests for case sensistive pg schema
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed May 22, 2019
1 parent ad24b87 commit a4187a7
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -159,6 +159,7 @@ ADD_PYTHON_TEST(PyQgsProcessingRecentAlgorithmLog test_qgsprocessingrecentalgori
ADD_PYTHON_TEST(PyQgsProcessingInPlace test_qgsprocessinginplace.py)
ADD_PYTHON_TEST(PyQgsProcessingAlgRunner test_qgsprocessingalgrunner.py)
ADD_PYTHON_TEST(PyQgsProcessingAlgDecorator test_processing_alg_decorator.py)
ADD_PYTHON_TEST(PyQgsImportIntoPostGIS test_processing_importintopostgis.py)
ADD_PYTHON_TEST(PyQgsProjectionSelectionWidgets test_qgsprojectionselectionwidgets.py)
ADD_PYTHON_TEST(PyQgsProjectMetadata test_qgsprojectmetadata.py)
ADD_PYTHON_TEST(PyQgsPropertyOverrideButton test_qgspropertyoverridebutton.py)
Expand Down
106 changes: 106 additions & 0 deletions tests/src/python/test_processing_importintopostgis.py
@@ -0,0 +1,106 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for Processing Export to Postgis algorithm.
.. 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__ = 'Alessandro Pasotti'
__date__ = '2018-09'
__copyright__ = 'Copyright 2018, The QGIS Project'

import re

from processing.core.Processing import Processing
from processing.gui.AlgorithmExecutor import execute
from qgis.analysis import QgsNativeAlgorithms
from qgis.core import (QgsApplication, QgsVectorLayer,
QgsGeometry, QgsProcessingContext,
QgsProcessingFeedback, QgsSettings,
)
from qgis.PyQt.QtCore import QCoreApplication
from qgis.testing import start_app, unittest
from utilities import unitTestDataPath

start_app()


class ConsoleFeedBack(QgsProcessingFeedback):

_errors = []

def reportError(self, error, fatalError=False):
print(error)
self._errors.append(error)


class TestExportToPostGis(unittest.TestCase):

@classmethod
def setUpClass(cls):
"""Run before all tests"""
QCoreApplication.setOrganizationName("QGIS_Test")
QCoreApplication.setOrganizationDomain(
"QGIS_TestPyQgsExportToPostgis.com")
QCoreApplication.setApplicationName("QGIS_TestPyQgsExportToPostgis")
QgsSettings().clear()
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
cls.registry = QgsApplication.instance().processingRegistry()

# Create DB connection in the settings
settings = QgsSettings()
settings.beginGroup('/PostgreSQL/connections/qgis_test')
settings.setValue('service', 'qgis_test')
settings.setValue('database', 'qgis_test')

def test_import(self):
"""Test algorithm with CamelCaseSchema"""

alg = self.registry.createAlgorithmById("qgis:importintopostgis")
self.assertIsNotNone(alg)

table_name = 'out_TestPyQgsExportToPostgis'

parameters = {
'CREATEINDEX': True,
'DATABASE': 'qgis_test',
'DROP_STRING_LENGTH': False,
'ENCODING': 'UTF-8',
'FORCE_SINGLEPART': False,
'GEOMETRY_COLUMN': 'geom',
'INPUT': unitTestDataPath() + '/points.shp',
'LOWERCASE_NAMES': True,
'OVERWRITE': True,
'PRIMARY_KEY': None,
'SCHEMA': 'CamelCaseSchema',
'TABLENAME': table_name
}

feedback = ConsoleFeedBack()
context = QgsProcessingContext()
# Note: the following returns true also in case of errors ...
self.assertTrue(execute(alg, parameters, context, feedback))
# ... so we check the log
self.assertEqual(feedback._errors, [])

# Check that data have been imported correctly
exported = QgsVectorLayer(unitTestDataPath() + '/points.shp', 'exported')
self.assertTrue(exported.isValid())
imported = QgsVectorLayer("service='qgis_test' dbname=\'qgis_test\' table=\"CamelCaseSchema\".\"%s\" (geom)" % table_name, 'imported', 'postgres')
self.assertTrue(imported.isValid())
imported_fields = [f.name() for f in imported.fields()]
for f in exported.fields():
self.assertTrue(f.name().lower() in imported_fields)

# Check data
imported_f = next(imported.getFeatures("class = 'Jet' AND heading = 85"))
self.assertTrue(imported_f.isValid())
exported_f = next(exported.getFeatures("class = 'Jet' AND heading = 85"))
self.assertTrue(exported_f.isValid())
self.assertEqual(exported_f.geometry().asWkt(), imported_f.geometry().asWkt())


if __name__ == '__main__':
unittest.main()
10 changes: 10 additions & 0 deletions tests/testdata/provider/testdata_pg.sql
Expand Up @@ -21,12 +21,22 @@ SET client_min_messages = warning;

CREATE EXTENSION IF NOT EXISTS postgis;

--- Create qgis_test schema
DROP SCHEMA IF EXISTS qgis_test CASCADE;
CREATE SCHEMA qgis_test;
GRANT ALL ON SCHEMA qgis_test TO public;
ALTER DEFAULT PRIVILEGES IN SCHEMA qgis_test GRANT ALL ON TABLES TO public;
ALTER DEFAULT PRIVILEGES IN SCHEMA qgis_test GRANT ALL ON SEQUENCES TO public;


--- Create "CamelCaseSchema" schema
DROP SCHEMA IF EXISTS "CamelCaseSchema" CASCADE;
CREATE SCHEMA "CamelCaseSchema";
GRANT ALL ON SCHEMA "CamelCaseSchema" TO public;
ALTER DEFAULT PRIVILEGES IN SCHEMA "CamelCaseSchema" GRANT ALL ON TABLES TO public;
ALTER DEFAULT PRIVILEGES IN SCHEMA "CamelCaseSchema" GRANT ALL ON SEQUENCES TO public;


SET default_tablespace = '';

SET default_with_oids = false;
Expand Down

0 comments on commit a4187a7

Please sign in to comment.