Skip to content
Permalink
Browse files

[processing] Allow using libpq defaults for host,port,user,pass and d…

…bname

Fixes #15706
Includes tests
  • Loading branch information
strk committed Oct 18, 2016
1 parent aa413b6 commit 640e10b582ba03e095cc059ba2ff0d87e98d7497
@@ -150,18 +150,34 @@ def defineCharacteristics(self):
self.addParameter(ParameterString(self.OPTIONS,
self.tr('Additional creation options'), '', optional=True))

def getConsoleCommands(self):
inLayer = self.getParameterValue(self.INPUT_LAYER)
ogrLayer = ogrConnectionString(inLayer)[1:-1]
ssrs = unicode(self.getParameterValue(self.S_SRS))
tsrs = unicode(self.getParameterValue(self.T_SRS))
asrs = unicode(self.getParameterValue(self.A_SRS))
def getConnectionString(self):
host = unicode(self.getParameterValue(self.HOST))
port = unicode(self.getParameterValue(self.PORT))
user = unicode(self.getParameterValue(self.USER))
dbname = unicode(self.getParameterValue(self.DBNAME))
password = unicode(self.getParameterValue(self.PASSWORD))
schema = unicode(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]
ssrs = unicode(self.getParameterValue(self.S_SRS))
tsrs = unicode(self.getParameterValue(self.T_SRS))
asrs = unicode(self.getParameterValue(self.A_SRS))
table = unicode(self.getParameterValue(self.TABLE))
pk = unicode(self.getParameterValue(self.PK))
pkstring = "-lco FID=" + pk
@@ -194,17 +210,9 @@ def getConsoleCommands(self):
arguments.append('--config PG_USE_COPY YES')
arguments.append('-f')
arguments.append('PostgreSQL')
arguments.append('PG:"host=' + host)
arguments.append('port=' + port)
if len(dbname) > 0:
arguments.append('dbname=' + dbname)
if len(password) > 0:
arguments.append('password=' + password)
if len(schema) > 0:
arguments.append('active_schema=' + schema)
else:
arguments.append('active_schema=public')
arguments.append('user=' + user + '"')
arguments.append('PG:"')
arguments.append(self.getConnectionString())
arguments.append('"')
arguments.append(dimstring)
arguments.append(ogrLayer)
arguments.append(ogrLayerName(inLayer))
@@ -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()

0 comments on commit 640e10b

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