Skip to content

Commit 3efee43

Browse files
committed
Add test for DBManager's PostGIS connector and plugin
Only enabled when ENABLE_PGTEST is set Includes test for dbname-less URI and service-only URI (ref #10600 ref #16625 ref #16626) The test connects to the `qgis_test` database, or what the QGIS_PGTEST_DB variable requests (as per tests/README.md) A raster table is added to the test postgis setup script, for use by this new test.
1 parent ecb4c5a commit 3efee43

File tree

5 files changed

+229
-0
lines changed

5 files changed

+229
-0
lines changed

python/plugins/db_manager/db_plugins/postgis/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@ PLUGIN_INSTALL(db_manager db_plugins/postgis/icons ${ICON_FILES})
88

99
ADD_SUBDIRECTORY(plugins)
1010

11+
IF(ENABLE_TESTS)
12+
INCLUDE(UsePythonTest)
13+
IF (ENABLE_PGTEST)
14+
ADD_PYTHON_TEST(dbmanager-postgis-connector connector_test.py)
15+
ADD_PYTHON_TEST(dbmanager-postgis-plugin plugin_test.py)
16+
ENDIF (ENABLE_PGTEST)
17+
ENDIF(ENABLE_TESTS)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
connector_test.py
6+
---------------------
7+
Date : May 2017
8+
Copyright : (C) 2017, Sandro Santilli
9+
Email : strk at kbt dot io
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Sandro Santilli'
21+
__date__ = 'May 2017'
22+
__copyright__ = '(C) 2017, Sandro Santilli'
23+
# This will get replaced with a git SHA1 when you do a git archive
24+
__revision__ = '$Format:%H$'
25+
26+
import os
27+
import qgis
28+
from qgis.testing import start_app, unittest
29+
from qgis.core import QgsDataSourceURI
30+
from qgis.utils import iface
31+
32+
start_app()
33+
34+
from db_manager.db_plugins.postgis.connector import PostGisDBConnector
35+
36+
37+
class TestDBManagerPostgisConnector(unittest.TestCase):
38+
39+
#def setUpClass():
40+
41+
def _getUser(self, connector):
42+
r = connector._execute(None, "SELECT USER")
43+
val = connector._fetchone(r)[0]
44+
connector._close_cursor(r)
45+
return val
46+
47+
def _getDatabase(self, connector):
48+
r = connector._execute(None, "SELECT current_database()")
49+
val = connector._fetchone(r)[0]
50+
connector._close_cursor(r)
51+
return val
52+
53+
# See https://issues.qgis.org/issues/16625
54+
# and https://issues.qgis.org/issues/10600
55+
def test_dbnameLessURI(self):
56+
c = PostGisDBConnector(QgsDataSourceURI())
57+
self.assertIsInstance(c, PostGisDBConnector)
58+
uri = c.uri()
59+
60+
# No username was passed, so we expect it to be taken
61+
# from PGUSER or USER environment variables
62+
expected_user = os.environ.get('PGUSER') or os.environ.get('USER')
63+
actual_user = self._getUser(c)
64+
self.assertEqual(actual_user, expected_user)
65+
66+
# No database was passed, so we expect it to be taken
67+
# from PGDATABASE or expected user
68+
expected_db = os.environ.get('PGDATABASE') or expected_user
69+
actual_db = self._getDatabase(c)
70+
self.assertEqual(actual_db, expected_db)
71+
72+
# TODO: add service-only test (requires a ~/.pg_service.conf file)
73+
74+
75+
if __name__ == '__main__':
76+
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
plugin_test.py
6+
---------------------
7+
Date : May 2017
8+
Copyright : (C) 2017, Sandro Santilli
9+
Email : strk at kbt dot io
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Sandro Santilli'
21+
__date__ = 'May 2017'
22+
__copyright__ = '(C) 2017, Sandro Santilli'
23+
# This will get replaced with a git SHA1 when you do a git archive
24+
__revision__ = '$Format:%H$'
25+
26+
import os
27+
import re
28+
import qgis
29+
from qgis.testing import start_app, unittest
30+
from qgis.core import QgsDataSourceURI
31+
from qgis.utils import iface
32+
from qgis.PyQt.QtCore import QObject
33+
34+
start_app()
35+
36+
from db_manager.db_plugins.postgis.plugin import PostGisDBPlugin, PGRasterTable
37+
from db_manager.db_plugins.postgis.plugin import PGDatabase
38+
from db_manager.db_plugins.plugin import Table
39+
from db_manager.db_plugins.postgis.connector import PostGisDBConnector
40+
41+
42+
class TestDBManagerPostgisPlugin(unittest.TestCase):
43+
44+
@classmethod
45+
def setUpClass(self):
46+
self.old_pgdatabase_env = os.environ.get('PGDATABASE')
47+
self.testdb = os.environ.get('QGIS_PGTEST_DB') or 'qgis_test'
48+
os.environ['PGDATABASE'] = self.testdb
49+
50+
# Create temporary service file
51+
self.old_pgservicefile_env = os.environ.get('PGSERVICEFILE')
52+
self.tmpservicefile = '/tmp/qgis-test-{}-pg_service.conf'.format(os.getpid())
53+
os.environ['PGSERVICEFILE'] = self.tmpservicefile
54+
55+
f = open(self.tmpservicefile, "w")
56+
f.write("[dbmanager]\ndbname={}\n".format(self.testdb))
57+
# TODO: add more things if PGSERVICEFILE was already set ?
58+
f.close()
59+
60+
@classmethod
61+
def tearDownClass(self):
62+
# Restore previous env variables if needed
63+
if self.old_pgdatabase_env:
64+
os.environ['PGDATABASE'] = self.old_pgdatabase_env
65+
if self.old_pgservicefile_env:
66+
os.environ['PGSERVICEFILE'] = self.old_pgservicefile_env
67+
# Remove temporary service file
68+
os.unlink(self.tmpservicefile)
69+
70+
# See https://issues.qgis.org/issues/16625
71+
72+
def test_rasterTableGdalURI(self):
73+
74+
def check_rasterTableGdalURI(expected_dbname):
75+
tables = database.tables()
76+
raster_tables_count = 0
77+
for tab in tables:
78+
if tab.type == Table.RasterType:
79+
raster_tables_count += 1
80+
gdalUri = tab.gdalUri()
81+
m = re.search(' dbname=([^ ]*) ', gdalUri)
82+
self.assertTrue(m)
83+
actual_dbname = m.group(1)
84+
self.assertEqual(actual_dbname, expected_dbname)
85+
#print(tab.type)
86+
#print(tab.quotedName())
87+
#print(tab)
88+
89+
# We need to make sure a database is created with at
90+
# least one raster table !
91+
self.assertEqual(raster_tables_count, 1)
92+
93+
obj = QObject() # needs to be kept alive
94+
95+
# Test for empty URI
96+
# See https://issues.qgis.org/issues/16625
97+
# and https://issues.qgis.org/issues/10600
98+
99+
expected_dbname = self.testdb
100+
os.environ['PGDATABASE'] = expected_dbname
101+
102+
database = PGDatabase(obj, QgsDataSourceURI())
103+
self.assertIsInstance(database, PGDatabase)
104+
105+
uri = database.uri()
106+
self.assertEqual(uri.host(), '')
107+
self.assertEqual(uri.username(), '')
108+
self.assertEqual(uri.database(), expected_dbname)
109+
self.assertEqual(uri.service(), '')
110+
111+
check_rasterTableGdalURI(expected_dbname)
112+
113+
# Test for service-only URI
114+
# See https://issues.qgis.org/issues/16626
115+
116+
os.environ['PGDATABASE'] = 'fake'
117+
database = PGDatabase(obj, QgsDataSourceURI('service=dbmanager'))
118+
self.assertIsInstance(database, PGDatabase)
119+
120+
uri = database.uri()
121+
self.assertEqual(uri.host(), '')
122+
self.assertEqual(uri.username(), '')
123+
self.assertEqual(uri.database(), '')
124+
self.assertEqual(uri.service(), 'dbmanager')
125+
126+
check_rasterTableGdalURI(expected_dbname)
127+
128+
129+
if __name__ == '__main__':
130+
unittest.main()

tests/testdata/provider/testdata_pg.sh

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ SCRIPTS="
44
tests/testdata/provider/testdata_pg.sql
55
tests/testdata/provider/testdata_pg_reltests.sql
66
tests/testdata/provider/testdata_pg_vectorjoin.sql
7+
tests/testdata/provider/testdata_pg_raster.sql
78
"
89

910
createdb qgis_test || exit 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- Table: qgis_test.raster1
2+
3+
CREATE TABLE qgis_test."Raster1"
4+
(
5+
pk serial NOT NULL,
6+
name character varying(255),
7+
"Rast" raster
8+
);
9+
10+
INSERT INTO qgis_test."Raster1" (name, "Rast") SELECT
11+
'simple one',
12+
ST_AddBand(
13+
ST_MakeEmptyRaster(16, 32, 7, -5, 0.2, -0.7, 0, 0, 0),
14+
1, '8BUI', 0.0, NULL
15+
);

0 commit comments

Comments
 (0)