mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-15 00:04:00 -04:00
Add test for DBManager's PostGIS connector and plugin
Only enabled when ENABLE_PGTEST is set Includes test for dbname-less URI (#10600 and #16625 and #16626) The test relies on default libpq accessed database containing one raster table, but makes no effort to create such database yet.
This commit is contained in:
parent
72f95e6f1a
commit
3738798991
@ -8,3 +8,10 @@ PLUGIN_INSTALL(db_manager db_plugins/postgis/icons ${ICON_FILES})
|
||||
|
||||
ADD_SUBDIRECTORY(plugins)
|
||||
|
||||
IF(ENABLE_TESTS)
|
||||
INCLUDE(UsePythonTest)
|
||||
IF (ENABLE_PGTEST)
|
||||
ADD_PYTHON_TEST(dbmanager-postgis-connector connector_test.py)
|
||||
ADD_PYTHON_TEST(dbmanager-postgis-plugin plugin_test.py)
|
||||
ENDIF (ENABLE_PGTEST)
|
||||
ENDIF(ENABLE_TESTS)
|
||||
|
@ -0,0 +1,76 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
***************************************************************************
|
||||
connector_test.py
|
||||
---------------------
|
||||
Date : May 2017
|
||||
Copyright : (C) 2017, Sandro Santilli
|
||||
Email : strk at kbt dot io
|
||||
***************************************************************************
|
||||
* *
|
||||
* 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__ = 'Sandro Santilli'
|
||||
__date__ = 'May 2017'
|
||||
__copyright__ = '(C) 2017, Sandro Santilli'
|
||||
# This will get replaced with a git SHA1 when you do a git archive
|
||||
__revision__ = '$Format:%H$'
|
||||
|
||||
import os
|
||||
import qgis
|
||||
from qgis.testing import start_app, unittest
|
||||
from qgis.core import QgsDataSourceUri
|
||||
from qgis.utils import iface
|
||||
|
||||
start_app()
|
||||
|
||||
from db_manager.db_plugins.postgis.connector import PostGisDBConnector
|
||||
|
||||
|
||||
class TestDBManagerPostgisConnector(unittest.TestCase):
|
||||
|
||||
#def setUpClass():
|
||||
|
||||
def _getUser(self, connector):
|
||||
r = connector._execute(None, "SELECT USER")
|
||||
val = connector._fetchone(r)[0]
|
||||
connector._close_cursor(r)
|
||||
return val
|
||||
|
||||
def _getDatabase(self, connector):
|
||||
r = connector._execute(None, "SELECT current_database()")
|
||||
val = connector._fetchone(r)[0]
|
||||
connector._close_cursor(r)
|
||||
return val
|
||||
|
||||
# See https://issues.qgis.org/issues/16625
|
||||
# and https://issues.qgis.org/issues/10600
|
||||
def test_dbnameLessURI(self):
|
||||
c = PostGisDBConnector(QgsDataSourceUri())
|
||||
self.assertIsInstance(c, PostGisDBConnector)
|
||||
uri = c.uri()
|
||||
|
||||
# No username was passed, so we expect it to be taken
|
||||
# from PGUSER or USER environment variables
|
||||
expected_user = os.environ.get('PGUSER') or os.environ.get('USER')
|
||||
actual_user = self._getUser(c)
|
||||
self.assertEqual(actual_user, expected_user)
|
||||
|
||||
# No database was passed, so we expect it to be taken
|
||||
# from PGDATABASE or expected user
|
||||
expected_db = os.environ.get('PGDATABASE') or expected_user
|
||||
actual_db = self._getDatabase(c)
|
||||
self.assertEqual(actual_db, expected_db)
|
||||
|
||||
# TODO: add service-only test (requires a ~/.pg_service.conf file)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
83
python/plugins/db_manager/db_plugins/postgis/plugin_test.py
Normal file
83
python/plugins/db_manager/db_plugins/postgis/plugin_test.py
Normal file
@ -0,0 +1,83 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
***************************************************************************
|
||||
plugin_test.py
|
||||
---------------------
|
||||
Date : May 2017
|
||||
Copyright : (C) 2017, Sandro Santilli
|
||||
Email : strk at kbt dot io
|
||||
***************************************************************************
|
||||
* *
|
||||
* 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__ = 'Sandro Santilli'
|
||||
__date__ = 'May 2017'
|
||||
__copyright__ = '(C) 2017, Sandro Santilli'
|
||||
# This will get replaced with a git SHA1 when you do a git archive
|
||||
__revision__ = '$Format:%H$'
|
||||
|
||||
import os
|
||||
import re
|
||||
import qgis
|
||||
from qgis.testing import start_app, unittest
|
||||
from qgis.core import QgsDataSourceUri
|
||||
from qgis.utils import iface
|
||||
from qgis.PyQt.QtCore import QObject
|
||||
|
||||
start_app()
|
||||
|
||||
from db_manager.db_plugins.postgis.plugin import PostGisDBPlugin, PGRasterTable
|
||||
from db_manager.db_plugins.postgis.plugin import PGDatabase
|
||||
from db_manager.db_plugins.plugin import Table
|
||||
from db_manager.db_plugins.postgis.connector import PostGisDBConnector
|
||||
|
||||
|
||||
class TestDBManagerPostgisPlugin(unittest.TestCase):
|
||||
|
||||
# See https://issues.qgis.org/issues/16625
|
||||
# and https://issues.qgis.org/issues/10600
|
||||
|
||||
def test_rasterTable(self):
|
||||
|
||||
#testdb = os.environ.get('QGIS_PGTEST_DB') or 'qgis_test'
|
||||
#os.environ['PGDATABASE'] = testdb
|
||||
|
||||
obj = QObject() # needs to be kept alive
|
||||
database = PGDatabase(obj, QgsDataSourceUri())
|
||||
self.assertIsInstance(database, PGDatabase)
|
||||
|
||||
uri = database.uri()
|
||||
self.assertEqual(uri.host(), '')
|
||||
self.assertEqual(uri.username(), '')
|
||||
expected_user = os.environ.get('PGUSER') or os.environ.get('USER')
|
||||
expected_dbname = os.environ.get('PGDATABASE') or expected_user
|
||||
self.assertEqual(uri.database(), expected_dbname)
|
||||
|
||||
tables = database.tables()
|
||||
raster_tables_count = 0
|
||||
for tab in tables:
|
||||
if tab.type == Table.RasterType:
|
||||
raster_tables_count += 1
|
||||
gdalUri = tab.gdalUri()
|
||||
m = re.search(' dbname=([^ ]*) ', gdalUri)
|
||||
self.assertTrue(m)
|
||||
actual_dbname = m.group(1)
|
||||
self.assertEqual(actual_dbname, expected_dbname)
|
||||
#print(tab.type)
|
||||
#print(tab.quotedName())
|
||||
#print(tab)
|
||||
|
||||
# We need to make sure a database is created with at
|
||||
# least one raster table !
|
||||
self.assertEqual(raster_tables_count, 1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
x
Reference in New Issue
Block a user