Skip to content

Commit d914a19

Browse files
authoredJun 7, 2017
Merge pull request #4685 from strk/dbmanager-test
Add test for DBManager's PostGIS connector and plugin
2 parents 9397c4f + 11ace44 commit d914a19

File tree

7 files changed

+245
-13
lines changed

7 files changed

+245
-13
lines changed
 

‎python/plugins/db_manager/db_plugins/connector.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def _execute(self, cursor, sql):
7878
if cursor is None:
7979
cursor = self._get_cursor()
8080
try:
81-
cursor.execute(unicode(sql))
81+
cursor.execute(str(sql))
8282

8383
except self.connection_error_types() as e:
8484
raise ConnectionError(e)
@@ -98,7 +98,7 @@ def _execute_and_commit(self, sql):
9898
def _get_cursor(self, name=None):
9999
try:
100100
if name is not None:
101-
name = unicode(name).encode('ascii', 'replace').replace('?', "_")
101+
name = str(name).encode('ascii', 'replace').replace('?', "_")
102102
self._last_cursor_named_id = 0 if not hasattr(self,
103103
'_last_cursor_named_id') else self._last_cursor_named_id + 1
104104
return self.connection.cursor("%s_%d" % (name, self._last_cursor_named_id))
@@ -181,37 +181,39 @@ def _get_cursor_columns(self, c):
181181

182182
@classmethod
183183
def quoteId(self, identifier):
184-
if hasattr(identifier, '__iter__'):
184+
if hasattr(identifier, '__iter__') and not isinstance(identifier, str):
185185
ids = list()
186186
for i in identifier:
187187
if i is None or i == "":
188188
continue
189189
ids.append(self.quoteId(i))
190190
return u'.'.join(ids)
191191

192-
identifier = unicode(
193-
identifier) if identifier is not None else unicode() # make sure it's python unicode string
192+
identifier = str(
193+
identifier) if identifier is not None else str() # make sure it's python unicode string
194194
return u'"%s"' % identifier.replace('"', '""')
195195

196196
@classmethod
197197
def quoteString(self, txt):
198198
""" make the string safe - replace ' with '' """
199-
if hasattr(txt, '__iter__'):
199+
if hasattr(txt, '__iter__') and not isinstance(txt, str):
200200
txts = list()
201201
for i in txt:
202202
if i is None:
203203
continue
204204
txts.append(self.quoteString(i))
205205
return u'.'.join(txts)
206206

207-
txt = unicode(txt) if txt is not None else unicode() # make sure it's python unicode string
207+
txt = str(txt) if txt is not None else str() # make sure it's python unicode string
208208
return u"'%s'" % txt.replace("'", "''")
209209

210210
@classmethod
211211
def getSchemaTableName(self, table):
212-
if not hasattr(table, '__iter__'):
212+
if not hasattr(table, '__iter__') and not isinstance(table, str):
213213
return (None, table)
214-
elif len(table) < 2:
214+
if isinstance(table, str):
215+
table = table.split('.')
216+
if len(table) < 2:
215217
return (None, table[0])
216218
else:
217219
return (table[0], table[1])

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

Lines changed: 7 additions & 0 deletions
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)

‎python/plugins/db_manager/db_plugins/postgis/connector.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
from qgis.PyQt.QtCore import QRegExp
2626
from qgis.core import QgsCredentials, QgsDataSourceURI
27+
from functools import cmp_to_key
2728

2829
from ..connector import DBConnector
2930
from ..plugin import ConnectionError, DbError, Table
@@ -60,7 +61,7 @@ def __init__(self, uri):
6061

6162
expandedConnInfo = self._connectionInfo()
6263
try:
63-
self.connection = psycopg2.connect(expandedConnInfo.encode('utf-8'))
64+
self.connection = psycopg2.connect(expandedConnInfo)
6465
except self.connection_error_types() as e:
6566
err = unicode(e)
6667
uri = self.uri()
@@ -79,7 +80,7 @@ def __init__(self, uri):
7980

8081
newExpandedConnInfo = uri.connectionInfo(True)
8182
try:
82-
self.connection = psycopg2.connect(newExpandedConnInfo.encode('utf-8'))
83+
self.connection = psycopg2.connect(newExpandedConnInfo)
8384
QgsCredentials.instance().put(conninfo, username, password)
8485
except self.connection_error_types() as e:
8586
if i == 2:
@@ -135,7 +136,7 @@ def __init__(self, uri):
135136
self._checkRasterColumnsTable()
136137

137138
def _connectionInfo(self):
138-
return unicode(self.uri().connectionInfo(True))
139+
return str(self.uri().connectionInfo(True))
139140

140141
def _checkSpatial(self):
141142
""" check whether postgis_version is present in catalog """
@@ -331,7 +332,7 @@ def getTables(self, schema=None, add_sys_tables=False):
331332
items.append(item)
332333
self._close_cursor(c)
333334

334-
return sorted(items, cmp=lambda x, y: cmp((x[2], x[1]), (y[2], y[1])))
335+
return sorted(items, key=cmp_to_key(lambda x, y: (x[1] > y[1]) - (x[1] < y[1])))
335336

336337
def getVectorTables(self, schema=None):
337338
""" get list of table with a geometry column
Lines changed: 76 additions & 0 deletions
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()
Lines changed: 130 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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
Lines changed: 15 additions & 0 deletions
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)
Please sign in to comment.