def test_getConnectionString(self): obj = OgrToPostGis() obj.initAlgorithm({}) parameters = {} context = QgsProcessingContext() # NOTE: defaults are debatable, see # https://github.com/qgis/QGIS/pull/3607#issuecomment-253971020 self.assertEqual(obj.getConnectionString(parameters, context), "host=localhost port=5432 active_schema=public") parameters['HOST'] = 'remote' self.assertEqual(obj.getConnectionString(parameters, context), "host=remote port=5432 active_schema=public") parameters['HOST'] = '' self.assertEqual(obj.getConnectionString(parameters, context), "port=5432 active_schema=public") parameters['PORT'] = '5555' self.assertEqual(obj.getConnectionString(parameters, context), "port=5555 active_schema=public") parameters['PORT'] = '' self.assertEqual(obj.getConnectionString(parameters, context), "active_schema=public") parameters['USER'] = '******' self.assertEqual(obj.getConnectionString(parameters, context), "active_schema=public user=usr") parameters['PASSWORD'] = '******' self.assertEqual(obj.getConnectionString(parameters, context), "password=pwd active_schema=public user=usr")
def testConnectionString(self): alg = OgrToPostGis() alg.initAlgorithm() parameters = {} feedback = QgsProcessingFeedback() context = QgsProcessingContext() # NOTE: defaults are debatable, see # https://github.com/qgis/QGIS/pull/3607#issuecomment-253971020 self.assertEqual(alg.getConnectionString(parameters, context), "host=localhost port=5432 active_schema=public") parameters['HOST'] = 'remote' self.assertEqual(alg.getConnectionString(parameters, context), "host=remote port=5432 active_schema=public") parameters['HOST'] = '' self.assertEqual(alg.getConnectionString(parameters, context), "port=5432 active_schema=public") parameters['PORT'] = '5555' self.assertEqual(alg.getConnectionString(parameters, context), "port=5555 active_schema=public") parameters['PORT'] = '' self.assertEqual(alg.getConnectionString(parameters, context), "active_schema=public") parameters['USER'] = '******' self.assertEqual(alg.getConnectionString(parameters, context), "active_schema=public user=usr") parameters['PASSWORD'] = '******' self.assertEqual(alg.getConnectionString(parameters, context), "password=pwd active_schema=public user=usr")
def testOgr2PostGis(self): context = QgsProcessingContext() feedback = QgsProcessingFeedback() source = os.path.join(testDataPath, 'polys.gml') source_with_space = os.path.join(testDataPath, 'filename with spaces.gml') alg = OgrToPostGis() alg.initAlgorithm() self.assertEqual( alg.getConsoleCommands({'INPUT': source}, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({'INPUT': source_with_space}, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 "' + source_with_space + '" filename_with_spaces ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.filename_with_spaces -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'HOST': 'google.com' }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=google.com port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'PORT': 3333 }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=3333 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'USER': '******' }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public user=kevin_bacon" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'DBNAME': 'secret_stuff' }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 dbname=secret_stuff active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'PASSWORD': '******' }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 password=passw0rd active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'SCHEMA': 'desktop' }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=desktop" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln desktop.polys2 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'TABLE': 'out_table' }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.out_table -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'PK': '' }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -nln public.polys2 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'PK': 'new_fid' }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=new_fid -nln public.polys2 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands( { 'INPUT': source, 'PK': '', 'PRIMARY_KEY': 'objectid' }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=objectid -nln public.polys2 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands( { 'INPUT': source, 'PK': 'new_id', 'PRIMARY_KEY': 'objectid' }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=new_id -nln public.polys2 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'GEOCOLUMN': 'my_geom' }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=my_geom -lco FID=id -nln public.polys2 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'DIM': 1 }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=3 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'SIMPLIFY': 5 }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -simplify 5 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'SEGMENTIZE': 4 }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -segmentize 4 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands( { 'INPUT': source, 'SPAT': QgsRectangle(1, 2, 3, 4) }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -spat 1.0 2.0 3.0 4.0 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'FIELDS': ['f1', 'f2'] }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 -select "f1,f2" ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'WHERE': '0=1' }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -where "0=1" -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'GT': 2 }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -gt 2 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'OVERWRITE': False }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'APPEND': True }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-append -overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'ADDFIELDS': True }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-addfields -overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'LAUNDER': True }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-lco LAUNDER=NO -overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'INDEX': True }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-lco SPATIAL_INDEX=OFF -overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'SKIPFAILURES': True }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -skipfailures -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'PROMOTETOMULTI': False }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'PRECISION': False }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -nlt PROMOTE_TO_MULTI -lco PRECISION=NO' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'OPTIONS': 'blah' }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -nlt PROMOTE_TO_MULTI blah' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'SHAPE_ENCODING': 'blah' }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES --config SHAPE_ENCODING blah -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'GTYPE': 4 }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -nlt LINESTRING -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'A_SRS': 'EPSG:3111' }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -a_srs EPSG:3111 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands( { 'INPUT': source, 'A_SRS': QgsCoordinateReferenceSystem('EPSG:3111') }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -a_srs EPSG:3111 -nlt PROMOTE_TO_MULTI' ]) custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'A_SRS': custom_crs }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -a_srs EPSG:20936 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'T_SRS': 'EPSG:3111' }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -t_srs EPSG:3111 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands( { 'INPUT': source, 'T_SRS': QgsCoordinateReferenceSystem('EPSG:3111') }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -t_srs EPSG:3111 -nlt PROMOTE_TO_MULTI' ]) custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'T_SRS': custom_crs }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -t_srs EPSG:20936 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'S_SRS': 'EPSG:3111' }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -s_srs EPSG:3111 -nlt PROMOTE_TO_MULTI' ]) self.assertEqual( alg.getConsoleCommands( { 'INPUT': source, 'S_SRS': QgsCoordinateReferenceSystem('EPSG:3111') }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -s_srs EPSG:3111 -nlt PROMOTE_TO_MULTI' ]) custom_crs = 'proj4: +proj=utm +zone=36 +south +a=6378249.145 +b=6356514.966398753 +towgs84=-143,-90,-294,0,0,0,0 +units=m +no_defs' self.assertEqual( alg.getConsoleCommands({ 'INPUT': source, 'S_SRS': custom_crs }, context, feedback), [ 'ogr2ogr', '-progress --config PG_USE_COPY YES -f PostgreSQL "PG:host=localhost port=5432 active_schema=public" ' '-lco DIM=2 ' + source + ' polys2 ' '-overwrite -lco GEOMETRY_NAME=geom -lco FID=id -nln public.polys2 -s_srs EPSG:20936 -nlt PROMOTE_TO_MULTI' ])