Skip to content
Permalink
Browse files

Make provider key lookup case insensitive

We use a confusing mix of provider key casing (e.g. 'ogr', 'WFS', 'wms'),
so make the lookups case insensitive to avoid frustration.

(There's 0% chance of two different providers having the same key
but different case!)
  • Loading branch information
nyalldawson committed Mar 16, 2020
1 parent 9276ea8 commit 124f9b166be66ba672dd24cf50a8e26966abec7f
Showing with 78 additions and 3 deletions.
  1. +11 −3 src/core/qgsproviderregistry.cpp
  2. +1 −0 tests/src/python/CMakeLists.txt
  3. +66 −0 tests/src/python/test_qgsproviderregistry.py
@@ -66,9 +66,10 @@ QgsProviderRegistry *QgsProviderRegistry::instance( const QString &pluginPath )
without accidentally adding a null meta data item to the metadata map.
*/
static
QgsProviderMetadata *findMetadata_( QgsProviderRegistry::Providers const &metaData,
QString const &providerKey )
QgsProviderMetadata *findMetadata_( const QgsProviderRegistry::Providers &metaData,
const QString &providerKey )
{
// first do case-sensitive match
QgsProviderRegistry::Providers::const_iterator i =
metaData.find( providerKey );

@@ -77,8 +78,15 @@ QgsProviderMetadata *findMetadata_( QgsProviderRegistry::Providers const &metaDa
return i->second;
}

// fallback to case-insensitive match
for ( auto it = metaData.begin(); it != metaData.end(); ++it )
{
if ( providerKey.compare( it->first, Qt::CaseInsensitive ) == 0 )
return it->second;
}

return nullptr;
} // findMetadata_
}

QgsProviderRegistry::QgsProviderRegistry( const QString &pluginPath )
{
@@ -188,6 +188,7 @@ ADD_PYTHON_TEST(PyQgsPropertyOverrideButton test_qgspropertyoverridebutton.py)
ADD_PYTHON_TEST(PyQgsProviderConnectionComboBox test_qgsproviderconnectioncombobox.py)
ADD_PYTHON_TEST(PyQgsProviderConnectionModel test_qgsproviderconnectionmodel.py)
ADD_PYTHON_TEST(PyQgsProviderConnectionGpkg test_qgsproviderconnection_ogr_gpkg.py)
ADD_PYTHON_TEST(PyQgsProviderRegistry test_qgsproviderregistry.py)
ADD_PYTHON_TEST(TestQgsRandomMarkerSymbolLayer test_qgsrandommarkersymbollayer.py)
ADD_PYTHON_TEST(PyQgsRange test_qgsrange.py)
ADD_PYTHON_TEST(PyQgsRangeWidgets test_qgsrangewidgets.py)
@@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsProviderRegistry.
.. note:: 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__ = 'Nyall Dawson'
__date__ = '16/03/2020'
__copyright__ = 'Copyright 2020, The QGIS Project'

import qgis # NOQA

from qgis.core import QgsProviderRegistry
from qgis.testing import start_app, unittest

# Convenience instances in case you may need them
# to find the srs.db
start_app()


class TestQgsProviderRegistry(unittest.TestCase):

def testProviderList(self):
"""
Test provider list
"""

providers = QgsProviderRegistry.instance().providerList()
self.assertIn('ogr', providers)
self.assertIn('gdal', providers)

def testProviderMetadata(self):
"""
Test retrieving provider metadata
"""

providers = QgsProviderRegistry.instance().providerList()
for p in providers:
self.assertTrue(QgsProviderRegistry.instance().providerMetadata(p))
# should be case-insensitive
self.assertTrue(QgsProviderRegistry.instance().providerMetadata(p.lower()))
self.assertTrue(QgsProviderRegistry.instance().providerMetadata(p.upper()))

self.assertIsNone(QgsProviderRegistry.instance().providerMetadata('asdasdasdasdasd'))

def testCreateProvider(self):
"""
Test creating provider instance
"""
providers = QgsProviderRegistry.instance().providerList()
for p in providers:
if p == 'geonode':
continue

self.assertTrue(QgsProviderRegistry.instance().createProvider(p, ''))
# should be case-insensitive
self.assertTrue(QgsProviderRegistry.instance().createProvider(p.lower(), ''))
self.assertTrue(QgsProviderRegistry.instance().createProvider(p.upper(), ''))

self.assertIsNone(QgsProviderRegistry.instance().createProvider('asdasdasdasdasd', ''))


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

0 comments on commit 124f9b1

Please sign in to comment.
You can’t perform that action at this time.