Skip to content
Permalink
Browse files

Add test for setting up connection string

Conflicts:
	python/plugins/processing/algs/gdal/ogr2ogrtopostgis.py
  • Loading branch information
strk authored and alexbruy committed Oct 18, 2016
1 parent d63222e commit 6731eaa9e52da8047bcc42a972eec3c3c944c877
@@ -154,19 +154,35 @@ 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]
shapeEncoding = self.getParameterValue(self.SHAPE_ENCODING)
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)
@@ -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 6731eaa

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