Skip to content

Commit d1464a0

Browse files
committed
Update postgres project storage unit test
1 parent 4281140 commit d1464a0

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/providers/postgres/qgspostgresprojectstorage.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ bool QgsPostgresProjectStorage::readProject( const QString &uri, QIODevice *devi
9292

9393
if ( !_projectsTableExists( *conn, projectUri.schemaName ) )
9494
{
95-
// TODO: write to context
95+
context.pushMessage( "Table qgis_projects does not exist or it is not accessible.", Qgis::Critical );
9696
QgsPostgresConnPool::instance()->releaseConnection( conn );
9797
return false;
9898
}
@@ -267,7 +267,7 @@ QString QgsPostgresProjectStorage::encodeUri( const QgsPostgresProjectUri &postU
267267
urlQuery.addQueryItem( "service", postUri.connInfo.service() );
268268
if ( !postUri.connInfo.authConfigId().isEmpty() )
269269
urlQuery.addQueryItem( "authcfg", postUri.connInfo.authConfigId() );
270-
if ( !postUri.connInfo.sslMode() != QgsDataSourceUri::SslPrefer )
270+
if ( postUri.connInfo.sslMode() != QgsDataSourceUri::SslPrefer )
271271
urlQuery.addQueryItem( "sslmode", QgsDataSourceUri::encodeSslMode( postUri.connInfo.sslMode() ) );
272272

273273
urlQuery.addQueryItem( "dbname", postUri.connInfo.database() );

tests/src/python/test_project_storage_postgres.py

+36-4
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,48 @@
2525

2626
from qgis.core import (
2727
QgsApplication,
28+
QgsDataSourceUri,
2829
QgsVectorLayer,
2930
QgsProject,
3031
)
31-
from PyQt5.QtCore import QDateTime
32+
from PyQt5.QtCore import QDateTime, QUrl, QUrlQuery
3233
from qgis.testing import start_app, unittest
3334
from utilities import unitTestDataPath
3435

3536
QGISAPP = start_app()
3637
TEST_DATA_DIR = unitTestDataPath()
3738

3839

40+
def encode_uri(ds_uri, schema_name, project_name=None):
41+
u = QUrl()
42+
urlQuery = QUrlQuery()
43+
44+
u.setScheme("postgresql")
45+
u.setHost(ds_uri.host())
46+
if ds_uri.port() != '':
47+
u.setPort(int(ds_uri.port()))
48+
if ds_uri.username() != '':
49+
u.setUserName(ds_uri.username())
50+
if ds_uri.password() != '':
51+
u.setPassword(ds_uri.password())
52+
53+
if ds_uri.service() != '':
54+
urlQuery.addQueryItem("service", ds_uri.service())
55+
if ds_uri.authConfigId() != '':
56+
urlQuery.addQueryItem("authcfg", ds_uri.authConfigId())
57+
if ds_uri.sslMode() != QgsDataSourceUri.SslPrefer:
58+
urlQuery.addQueryItem("sslmode", QgsDataSourceUri.encodeSslMode(ds_uri.sslMode()))
59+
60+
urlQuery.addQueryItem("dbname", ds_uri.database())
61+
62+
urlQuery.addQueryItem("schema", schema_name)
63+
if project_name:
64+
urlQuery.addQueryItem("project", project_name)
65+
66+
u.setQuery(urlQuery)
67+
return str(u.toEncoded(), 'utf-8')
68+
69+
3970
class TestPyQgsProjectStoragePostgres(unittest.TestCase):
4071

4172
@classmethod
@@ -44,6 +75,8 @@ def setUpClass(cls):
4475
cls.dbconn = 'dbname=\'qgis_test\''
4576
if 'QGIS_PGTEST_DB' in os.environ:
4677
cls.dbconn = os.environ['QGIS_PGTEST_DB']
78+
cls.ds_uri = QgsDataSourceUri(cls.dbconn)
79+
4780
# Create test layers
4881
cls.vl = QgsVectorLayer(cls.dbconn + ' sslmode=disable key=\'pk\' srid=4326 type=POINT table="qgis_test"."someData" (geom) sql=', 'test', 'postgres')
4982
assert cls.vl.isValid()
@@ -66,9 +99,8 @@ def dropProjectsTable(self):
6699

67100
def testSaveLoadProject(self):
68101

69-
# TODO: respect QGIS_PGTEST_DB
70-
schema_uri = "postgresql:///?dbname=qgis_test&schema=qgis_test"
71-
project_uri = "postgresql:///?dbname=qgis_test&schema=qgis_test&project=abc"
102+
schema_uri = encode_uri(self.ds_uri, 'qgis_test')
103+
project_uri = encode_uri(self.ds_uri, 'qgis_test', 'abc')
72104

73105
self.dropProjectsTable() # make sure we have a clean start
74106

0 commit comments

Comments
 (0)