Add test for setting up connection string

Conflicts:
	python/plugins/processing/algs/gdal/ogr2ogrtopostgis.py
This commit is contained in:
Sandro Santilli 2016-10-15 11:14:29 +02:00 committed by Alexander Bruy
parent d63222e68e
commit 6731eaa9e5
2 changed files with 71 additions and 18 deletions

View File

@ -154,6 +154,28 @@ class Ogr2OgrToPostGis(GdalAlgorithm):
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 @@ class Ogr2OgrToPostGis(GdalAlgorithm):
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 @@ class Ogr2OgrToPostGis(GdalAlgorithm):
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)

View File

@ -26,6 +26,7 @@ __copyright__ = '(C) 2016, Matthias Kuhn'
__revision__ = ':%H$'
import AlgorithmsTestBase
from processing.algs.gdal.ogr2ogrtopostgis import Ogr2OgrToPostGis
import nose2
import shutil
@ -54,5 +55,52 @@ class TestGdalAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsTest):
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()