From 6731eaa9e52da8047bcc42a972eec3c3c944c877 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Sat, 15 Oct 2016 11:14:29 +0200 Subject: [PATCH] Add test for setting up connection string Conflicts: python/plugins/processing/algs/gdal/ogr2ogrtopostgis.py --- .../processing/algs/gdal/ogr2ogrtopostgis.py | 41 +++++++++------- .../processing/tests/GdalAlgorithmsTest.py | 48 +++++++++++++++++++ 2 files changed, 71 insertions(+), 18 deletions(-) diff --git a/python/plugins/processing/algs/gdal/ogr2ogrtopostgis.py b/python/plugins/processing/algs/gdal/ogr2ogrtopostgis.py index d8fbbe7b2eda..273eb33f0a2d 100644 --- a/python/plugins/processing/algs/gdal/ogr2ogrtopostgis.py +++ b/python/plugins/processing/algs/gdal/ogr2ogrtopostgis.py @@ -154,6 +154,28 @@ def defineCharacteristics(self): self.addParameter(ParameterString(self.OPTIONS, self.tr('Additional creation options'), '', optional=True)) + def getConnectionString(self): + host = str(self.getParameterValue(self.HOST)) + port = str(self.getParameterValue(self.PORT)) + user = str(self.getParameterValue(self.USER)) + dbname = str(self.getParameterValue(self.DBNAME)) + password = str(self.getParameterValue(self.PASSWORD)) + schema = str(self.getParameterValue(self.SCHEMA)) + arguments = [] + if host: + arguments.append('host=' + host) + if port: + arguments.append('port=' + port) + if dbname: + arguments.append('dbname=' + dbname) + if password: + arguments.append('password=' + password) + if schema: + arguments.append('active_schema=' + schema) + if user: + arguments.append('user=' + user) + return GdalUtils.escapeAndJoin(arguments) + def getConsoleCommands(self): inLayer = self.getParameterValue(self.INPUT_LAYER) ogrLayer = ogrConnectionString(inLayer)[1:-1] @@ -161,12 +183,6 @@ def getConsoleCommands(self): ssrs = str(self.getParameterValue(self.S_SRS)) tsrs = str(self.getParameterValue(self.T_SRS)) asrs = str(self.getParameterValue(self.A_SRS)) - host = str(self.getParameterValue(self.HOST)) - port = str(self.getParameterValue(self.PORT)) - user = str(self.getParameterValue(self.USER)) - dbname = str(self.getParameterValue(self.DBNAME)) - password = str(self.getParameterValue(self.PASSWORD)) - schema = str(self.getParameterValue(self.SCHEMA)) table = str(self.getParameterValue(self.TABLE)) pk = str(self.getParameterValue(self.PK)) pkstring = "-lco FID=" + pk @@ -204,18 +220,7 @@ def getConsoleCommands(self): arguments.append('-f') arguments.append('PostgreSQL') arguments.append('PG:"') - if host: - arguments.append(' host=' + host) - if port: - arguments.append('port=' + port) - if dbname: - arguments.append('dbname=' + dbname) - if password: - arguments.append('password=' + password) - if schema: - arguments.append('active_schema=' + schema) - if user: - arguments.append('user=' + user) + arguments.append(self.getConnectionString()) arguments.append('"') arguments.append(dimstring) arguments.append(ogrLayer) diff --git a/python/plugins/processing/tests/GdalAlgorithmsTest.py b/python/plugins/processing/tests/GdalAlgorithmsTest.py index 3766cdd3d36d..57f6f13c276c 100644 --- a/python/plugins/processing/tests/GdalAlgorithmsTest.py +++ b/python/plugins/processing/tests/GdalAlgorithmsTest.py @@ -26,6 +26,7 @@ __revision__ = ':%H$' import AlgorithmsTestBase +from processing.algs.gdal.ogr2ogrtopostgis import Ogr2OgrToPostGis import nose2 import shutil @@ -54,5 +55,52 @@ def test_definition_file(self): return 'gdal_algorithm_tests.yaml' +class TestGdalOgr2OgrToPostgis(unittest.TestCase): + + @classmethod + def setUpClass(cls): + #start_app() + pass + + @classmethod + def tearDownClass(cls): + pass + + # See http://hub.qgis.org/issues/15706 + def test_getConnectionString(self): + + obj = Ogr2OgrToPostGis() + + cs = obj.getConnectionString() + # NOTE: defaults are debatable, see + # https://github.com/qgis/QGIS/pull/3607#issuecomment-253971020 + self.assertEquals(obj.getConnectionString(), + "host=localhost port=5432 active_schema=public") + + obj.setParameterValue('HOST', 'remote') + self.assertEquals(obj.getConnectionString(), + "host=remote port=5432 active_schema=public") + + obj.setParameterValue('HOST', '') + self.assertEquals(obj.getConnectionString(), + "port=5432 active_schema=public") + + obj.setParameterValue('PORT', '5555') + self.assertEquals(obj.getConnectionString(), + "port=5555 active_schema=public") + + obj.setParameterValue('PORT', '') + self.assertEquals(obj.getConnectionString(), + "active_schema=public") + + obj.setParameterValue('USER', 'usr') + self.assertEquals(obj.getConnectionString(), + "active_schema=public user=usr") + + obj.setParameterValue('PASSWORD', 'pwd') + self.assertEquals(obj.getConnectionString(), + "password=pwd active_schema=public user=usr") + + if __name__ == '__main__': nose2.main()