Skip to content

Commit

Permalink
GdalUtils test added
Browse files Browse the repository at this point in the history
  • Loading branch information
uclaros authored and wonder-sk committed Jul 18, 2024
1 parent 8e2fd94 commit b137540
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ ADD_PYTHON_TEST(PyQgsSelectiveMasking test_selective_masking.py)
ADD_PYTHON_TEST(PyQgsAttributeEditorAction test_qgsattributeeditoraction.py)
ADD_PYTHON_TEST(PyQgsVectorTile test_qgsvectortile.py)
ADD_PYTHON_TEST(PyQgsVtpk test_qgsvtpk.py)
ADD_PYTHON_TEST(PyQgsProcessingAlgsGdalGdalUtils test_processing_algs_gdal_gdalutils.py)

if (NOT WIN32)
ADD_PYTHON_TEST(PyQgsLogger test_qgslogger.py)
Expand Down
82 changes: 82 additions & 0 deletions tests/src/python/test_processing_algs_gdal_gdalutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""QGIS Unit tests for GdalUtils class
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Stefanos Natsis'
__date__ = '03/07/2024'
__copyright__ = 'Copyright 2024, The QGIS Project'


import unittest
from qgis.testing import start_app, QgisTestCase
from qgis.core import QgsApplication, QgsRasterLayer, QgsDataSourceUri, QgsAuthMethodConfig
from processing.algs.gdal.GdalUtils import (
GdalUtils,
GdalConnectionDetails
)

start_app()


class TestProcessingAlgsGdalGdalUtils(QgisTestCase):

def test_gdal_connection_details_from_layer_postgresraster(self):
"""
Test GdalUtils.gdal_connection_details_from_layer
"""

rl = QgsRasterLayer(
"dbname='mydb' host=localhost port=5432 user='asdf' password='42'"
" sslmode=disable table=some_table schema=some_schema column=rast sql=pk = 2",
'pg_layer', 'postgresraster')

self.assertEqual(rl.providerType(), 'postgresraster')

connection_details = GdalUtils.gdal_connection_details_from_layer(rl)
s = connection_details.connection_string

self.assertTrue(s.lower().startswith('pg:'))
self.assertTrue("schema='some_schema'" in s)
self.assertTrue("password='42'" in s)
self.assertTrue("column='rast'" in s)
self.assertTrue("mode=1" in s)
self.assertTrue("where='pk = 2'" in s)
self.assertEqual(connection_details.format, '"PostGISRaster"')

# test different uri:
# - authcfg is expanded
# - column is parsed
# - where is skipped
authm = QgsApplication.authManager()
self.assertTrue(authm.setMasterPassword('masterpassword', True))
config = QgsAuthMethodConfig()
config.setName('Basic')
config.setMethod('Basic')
config.setConfig('username', 'asdf')
config.setConfig('password', '42')
self.assertTrue(authm.storeAuthenticationConfig(config, True))

rl = QgsRasterLayer(
f"dbname='mydb' host=localhost port=5432 authcfg={config.id()}"
f" sslmode=disable table=\"some_schema\".\"some_table\" (rast)",
'pg_layer', 'postgresraster')

self.assertEqual(rl.providerType(), 'postgresraster')

connection_details = GdalUtils.gdal_connection_details_from_layer(rl)
s = connection_details.connection_string

self.assertTrue(s.lower().startswith('pg:'))
self.assertTrue("schema='some_schema'" in s)
self.assertTrue("user='asdf'" in s)
self.assertTrue("password='42'" in s)
self.assertTrue("column='rast'" in s)
self.assertTrue("mode=1" in s)
self.assertFalse("where=" in s)


if __name__ == '__main__':
unittest.main()

0 comments on commit b137540

Please sign in to comment.