Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #36230 from elpaso/uri-encode-round-trip-test
Browse files Browse the repository at this point in the history
PG and PG raster en/decode URI fix and tests
  • Loading branch information
elpaso committed May 7, 2020
2 parents c2715d7 + ae60a2f commit e441eaa
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 33 deletions.
12 changes: 8 additions & 4 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -5452,7 +5452,8 @@ QVariantMap QgsPostgresProviderMetadata::decodeUri( const QString &uri )
if ( dsUri.wkbType() != QgsWkbTypes::Type::Unknown )
uriParts[ QStringLiteral( "type" ) ] = dsUri.wkbType();

uriParts[ QStringLiteral( "selectatid" ) ] = dsUri.selectAtIdDisabled();
if ( uri.contains( QStringLiteral( "selectatid=" ), Qt::CaseSensitivity::CaseInsensitive ) )
uriParts[ QStringLiteral( "selectatid" ) ] = ! dsUri.selectAtIdDisabled();

if ( ! dsUri.table().isEmpty() )
uriParts[ QStringLiteral( "table" ) ] = dsUri.table();
Expand All @@ -5463,8 +5464,11 @@ QVariantMap QgsPostgresProviderMetadata::decodeUri( const QString &uri )
if ( ! dsUri.srid().isEmpty() )
uriParts[ QStringLiteral( "srid" ) ] = dsUri.srid();

uriParts[ QStringLiteral( "estimatedmetadata" ) ] = dsUri.useEstimatedMetadata();
uriParts[ QStringLiteral( "sslmode" ) ] = dsUri.sslMode();
if ( uri.contains( QStringLiteral( "estimatedmetadata=" ), Qt::CaseSensitivity::CaseInsensitive ) )
uriParts[ QStringLiteral( "estimatedmetadata" ) ] = dsUri.useEstimatedMetadata();

if ( uri.contains( QStringLiteral( "sslmode=" ), Qt::CaseSensitivity::CaseInsensitive ) )
uriParts[ QStringLiteral( "sslmode" ) ] = dsUri.sslMode();

if ( ! dsUri.sql().isEmpty() )
uriParts[ QStringLiteral( "sql" ) ] = dsUri.sql();
Expand Down Expand Up @@ -5514,5 +5518,5 @@ QString QgsPostgresProviderMetadata::encodeUri( const QVariantMap &parts )
dsUri.setParam( QStringLiteral( "checkPrimaryKeyUnicity" ), parts.value( QStringLiteral( "checkPrimaryKeyUnicity" ) ).toString() );
if ( parts.contains( QStringLiteral( "geometrycolumn" ) ) )
dsUri.setGeometryColumn( parts.value( QStringLiteral( "geometrycolumn" ) ).toString() );
return dsUri.uri();
return dsUri.uri( false );
}
104 changes: 81 additions & 23 deletions src/providers/postgres/raster/qgspostgresrasterprovider.cpp
Expand Up @@ -542,28 +542,86 @@ QgsPostgresRasterProviderMetadata::QgsPostgresRasterProviderMetadata()
QVariantMap QgsPostgresRasterProviderMetadata::decodeUri( const QString &uri )
{
const QgsDataSourceUri dsUri { uri };
return
{
{ QStringLiteral( "dbname" ), dsUri.database() },
{ QStringLiteral( "host" ), dsUri.host() },
{ QStringLiteral( "port" ), dsUri.port() },
{ QStringLiteral( "service" ), dsUri.service() },
{ QStringLiteral( "username" ), dsUri.username() },
{ QStringLiteral( "password" ), dsUri.password() },
{ QStringLiteral( "authcfg" ), dsUri.authConfigId() },
{ QStringLiteral( "selectatid" ), dsUri.selectAtIdDisabled() },
{ QStringLiteral( "table" ), dsUri.table() },
{ QStringLiteral( "schema" ), dsUri.schema() },
{ QStringLiteral( "key" ), dsUri.keyColumn() },
{ QStringLiteral( "srid" ), dsUri.srid() },
{ QStringLiteral( "estimatedmetadata" ), dsUri.useEstimatedMetadata() },
{ QStringLiteral( "sslmode" ), dsUri.sslMode() },
{ QStringLiteral( "sql" ), dsUri.sql() },
{ QStringLiteral( "geometrycolumn" ), dsUri.geometryColumn() },
{ QStringLiteral( "temporalFieldIndex" ), dsUri.param( QStringLiteral( "temporalFieldIndex" ) ) },
{ QStringLiteral( "temporalDefaultTime" ), dsUri.param( QStringLiteral( "temporalDefaultTime" ) ) },
{ QStringLiteral( "enableTime" ), dsUri.param( QStringLiteral( "enableTime" ) ) },
};
QVariantMap decoded;

if ( ! dsUri.database().isEmpty() )
{
decoded[ QStringLiteral( "dbname" ) ] = dsUri.database();
}
if ( ! dsUri.host().isEmpty() )
{
decoded[ QStringLiteral( "host" ) ] = dsUri.host();
}
if ( ! dsUri.port().isEmpty() )
{
decoded[ QStringLiteral( "port" ) ] = dsUri.host();
}
if ( ! dsUri.service().isEmpty() )
{
decoded[ QStringLiteral( "service" ) ] = dsUri.service();
}
if ( ! dsUri.username().isEmpty() )
{
decoded[ QStringLiteral( "username" ) ] = dsUri.username();
}
if ( ! dsUri.password().isEmpty() )
{
decoded[ QStringLiteral( "password" ) ] = dsUri.password();
}
if ( ! dsUri.authConfigId().isEmpty() )
{
decoded[ QStringLiteral( "authcfg" ) ] = dsUri.authConfigId();
}
if ( ! dsUri.schema().isEmpty() )
{
decoded[ QStringLiteral( "schema" ) ] = dsUri.schema();
}
if ( ! dsUri.table().isEmpty() )
{
decoded[ QStringLiteral( "table" ) ] = dsUri.table();
}
if ( ! dsUri.keyColumn().isEmpty() )
{
decoded[ QStringLiteral( "key" ) ] = dsUri.keyColumn();
}
if ( ! dsUri.srid().isEmpty() )
{
decoded[ QStringLiteral( "srid" ) ] = dsUri.srid();
}
if ( uri.contains( QStringLiteral( "estimatedmetadata=" ), Qt::CaseSensitivity::CaseInsensitive ) )
{
decoded[ QStringLiteral( "estimatedmetadata" ) ] = dsUri.useEstimatedMetadata();
}
if ( uri.contains( QStringLiteral( "sslmode=" ), Qt::CaseSensitivity::CaseInsensitive ) )
{
decoded[ QStringLiteral( "sslmode" ) ] = dsUri.sslMode();
}
// Do not add sql if it's empty
if ( ! dsUri.sql().isEmpty() )
{
decoded[ QStringLiteral( "sql" ) ] = dsUri.sql();
}
if ( ! dsUri.geometryColumn().isEmpty() )
{
decoded[ QStringLiteral( "geometrycolumn" ) ] = dsUri.geometryColumn();
}

// Params
const static QStringList params {{
QStringLiteral( "temporalFieldIndex" ),
QStringLiteral( "temporalDefaultTime" ),
QStringLiteral( "enableTime" )
}};

for ( const QString &pname : qgis::as_const( params ) )
{
if ( dsUri.hasParam( pname ) )
{
decoded[ pname ] = dsUri.param( pname );
}
}

return decoded;
}


Expand Down Expand Up @@ -608,7 +666,7 @@ QString QgsPostgresRasterProviderMetadata::encodeUri( const QVariantMap &parts )
dsUri.setParam( QStringLiteral( "temporalDefaultTime" ), parts.value( QStringLiteral( "temporalDefaultTime" ) ).toString() );
if ( parts.contains( QStringLiteral( "enableTime" ) ) )
dsUri.setParam( QStringLiteral( "enableTime" ), parts.value( QStringLiteral( "enableTime" ) ).toString() );
return dsUri.uri();
return dsUri.uri( false );
}

QgsPostgresRasterProvider *QgsPostgresRasterProviderMetadata::createProvider( const QString &uri, const QgsDataProvider::ProviderOptions &options )
Expand Down
49 changes: 43 additions & 6 deletions tests/src/python/test_provider_postgres.py
Expand Up @@ -46,6 +46,7 @@
QgsGeometry,
QgsProviderRegistry,
QgsVectorDataProvider,
QgsDataSourceUri,
)
from qgis.gui import QgsGui, QgsAttributeForm
from qgis.PyQt.QtCore import QDate, QTime, QDateTime, QVariant, QDir, QObject, QByteArray
Expand Down Expand Up @@ -1687,7 +1688,6 @@ def testEncodeDecodeUri(self):
'host': 'localhost',
'port': '5432',
'schema': 'public',
'selectatid': False,
'srid': '3067',
'sslmode': 1,
'table': 'basic_map_tiled',
Expand All @@ -1701,7 +1701,6 @@ def testEncodeDecodeUri(self):
'key': 'id',
'port': '5432',
'schema': 'public',
'selectatid': False,
'srid': '3763',
'sslmode': 1,
'table': 'copas1',
Expand All @@ -1715,24 +1714,62 @@ def testEncodeDecodeUri(self):
'key': 'id',
'port': '5432',
'schema': 'public',
'selectatid': False,
'srid': '3763',
'sslmode': 1,
'table': 'copas1',
'type': 6,
'username': 'myuser'}), "dbname='qgis_tests' user='myuser' srid=3763 estimatedmetadata='true' host='localhost' key='id' port='5432' selectatid='false' sslmode='disable' type='MultiPolygon' table=\"public\".\"copas1\" (geom)")
'username': 'myuser'}), "dbname='qgis_tests' user='myuser' srid=3763 estimatedmetadata='true' host='localhost' key='id' port='5432' sslmode='disable' type='MultiPolygon' table=\"public\".\"copas1\" (geom)")

self.assertEqual(md.encodeUri({'dbname': 'qgis_tests',
'estimatedmetadata': True,
'geometrycolumn': 'rast',
'host': 'localhost',
'port': '5432',
'schema': 'public',
'selectatid': False,
'srid': '3067',
'sslmode': 1,
'table': 'basic_map_tiled',
'username': 'myuser'}), "dbname='qgis_tests' user='myuser' srid=3067 estimatedmetadata='true' host='localhost' port='5432' selectatid='false' sslmode='disable' table=\"public\".\"basic_map_tiled\" (rast)")
'username': 'myuser'}), "dbname='qgis_tests' user='myuser' srid=3067 estimatedmetadata='true' host='localhost' port='5432' sslmode='disable' table=\"public\".\"basic_map_tiled\" (rast)")

def _round_trip(uri):
decoded = md.decodeUri(uri)
self.assertEqual(decoded, md.decodeUri(md.encodeUri(decoded)))

uri = self.dbconn + \
' sslmode=disable key=\'gid\' srid=3035 table="public"."my_pg_vector" sql='
decoded = md.decodeUri(uri)
self.assertEqual(decoded, {
'key': 'gid',
'schema': 'public',
'service': 'qgis_test',
'srid': '3035',
'sslmode': QgsDataSourceUri.SslDisable,
'table': 'my_pg_vector',
})

_round_trip(uri)

uri = self.dbconn + \
' sslmode=prefer key=\'gid\' srid=3035 temporalFieldIndex=2 ' + \
'authcfg=afebeff username=\'my username\' password=\'my secret password=\' ' + \
'table="public"."my_pg_vector" (the_geom) sql="a_field" != 1223223'

_round_trip(uri)

decoded = md.decodeUri(uri)
self.assertEqual(decoded, {
'authcfg': 'afebeff',
'geometrycolumn': 'the_geom',
'key': 'gid',
'password': 'my secret password=',
'schema': 'public',
'service': 'qgis_test',
'sql': '"a_field" != 1223223',
'srid': '3035',
'sslmode': QgsDataSourceUri.SslPrefer,
'table': 'my_pg_vector',
'username': 'my username',
})


class TestPyQgsPostgresProviderCompoundKey(unittest.TestCase, ProviderTestCase):
Expand Down
49 changes: 49 additions & 0 deletions tests/src/python/test_provider_postgresraster.py
Expand Up @@ -34,6 +34,7 @@
QgsRaster,
QgsProviderRegistry,
QgsRasterBandStats,
QgsDataSourceUri,
)
from qgis.testing import start_app, unittest
from utilities import unitTestDataPath, compareWkt
Expand Down Expand Up @@ -365,6 +366,54 @@ def _test_block(rl, expected_block, expected_single):

_test_block(rl, [136, 142, 161, 169], 169)

def testMetadataEncodeDecode(self):
"""Round trip tests on URIs"""

def _round_trip(uri):
decoded = md.decodeUri(uri)
self.assertEqual(decoded, md.decodeUri(md.encodeUri(decoded)))

uri = self.dbconn + \
' sslmode=disable key=\'rid\' srid=3035 table="public"."raster_tiled_3035" sql='
md = QgsProviderRegistry.instance().providerMetadata('postgresraster')
decoded = md.decodeUri(uri)
self.assertEqual(decoded, {
'key': 'rid',
'schema': 'public',
'service': 'qgis_test',
'srid': '3035',
'sslmode': QgsDataSourceUri.SslDisable,
'table': 'raster_tiled_3035',
})

_round_trip(uri)

uri = self.dbconn + \
' sslmode=prefer key=\'rid\' srid=3035 temporalFieldIndex=2 temporalDefaultTime=2020-03-02 ' + \
'authcfg=afebeff username=\'my username\' password=\'my secret password=\' ' + \
'enableTime=true table="public"."raster_tiled_3035" (rast) sql="a_field" != 1223223'

_round_trip(uri)

decoded = md.decodeUri(uri)
self.assertEqual(decoded, {
'authcfg': 'afebeff',
'enableTime': 'true',
'geometrycolumn': 'rast',
'key': 'rid',
'password': 'my secret password=',
'schema': 'public',
'service': 'qgis_test',
'sql': '"a_field" != 1223223',
'srid': '3035',
'sslmode': QgsDataSourceUri.SslPrefer,
'table': 'raster_tiled_3035',
'temporalDefaultTime':
'2020-03-02',
'temporalFieldIndex': '2',
'username': 'my username',
})


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

0 comments on commit e441eaa

Please sign in to comment.