Skip to content
Permalink
Browse files

Test databaseConnection

(cherry picked from commit 37e5f94)
  • Loading branch information
elpaso authored and nyalldawson committed Feb 19, 2021
1 parent 8e39816 commit ca4d4f1d472c836348be1c1f822696744274102a
@@ -505,16 +505,6 @@ QgsAbstractDatabaseProviderConnection *QgsOgrDataCollectionItem::databaseConnect
return conn;
}

void QgsOgrDataCollectionItem::setDriverName( const QString &driverName )
{
mDriverName = driverName;
}

QString QgsOgrDataCollectionItem::driverName() const
{
return mDriverName;
}

// ---------------------------------------------------------------------------

QString QgsOgrDataItemProvider::name()
@@ -774,7 +764,6 @@ QgsDataItem *QgsOgrDataItemProvider::createDataItem( const QString &pathIn, QgsD
else if ( numLayers > 1 || sOgrSupportedDbDriverNames.contains( driverName ) )
{
item = new QgsOgrDataCollectionItem( parentItem, name, path );
qobject_cast<QgsOgrDataCollectionItem *>( item )->setDriverName( driverName );
}
else
{
@@ -135,21 +135,6 @@ class CORE_EXPORT QgsOgrDataCollectionItem final: public QgsDataCollectionItem

QgsAbstractDatabaseProviderConnection *databaseConnection() const override;

/**
* Sets GDAL driver name.
* \since QGIS 3.18
*/
void setDriverName( const QString &driverName );

/**
* Returns GDAL driver name.
* \since QGIS 3.18
*/
QString driverName() const;

private:

QString mDriverName;
};

//! Provider for OGR root data item
@@ -10,9 +10,11 @@
__date__ = '14/11/2020 late in the night'
__copyright__ = 'Copyright 2020, The QGIS Project'

from qgis.PyQt.QtCore import QEventLoop
from qgis.core import QgsDataCollectionItem, QgsLayerItem

import os
from qgis.PyQt.QtCore import QEventLoop
from qgis.core import QgsDataCollectionItem, QgsLayerItem, QgsDirectoryItem
from utilities import unitTestDataPath
from qgis.testing import start_app, unittest

app = start_app()
@@ -63,9 +65,10 @@ def testPythonCreateChildrenCalledFromCplusplus(self):
# Python object PyQgsLayerItem should still be alive
self.assertFalse(tabSetDestroyedFlag[0])

self.assertEqual(len(item.children()), 2)
self.assertEqual(item.children()[0].name(), "name")
self.assertEqual(item.children()[1].name(), "name2")
children = item.children()
self.assertEqual(len(children), 2)
self.assertEqual(children[0].name(), "name")
self.assertEqual(children[1].name(), "name2")

# Delete the object and make sure all deferred deletions are processed
item.destroyed.connect(loop.quit)
@@ -78,8 +81,17 @@ def testPythonCreateChildrenCalledFromCplusplus(self):

def test_databaseConnection(self):

from IPython import embed
embed(using=False)
dataitem = QgsDataCollectionItem(None, 'name', '/invalid_path', 'ogr')
self.assertIsNone(dataitem.databaseConnection())
dataitem = QgsDirectoryItem(None, 'name', os.path.join(unitTestDataPath(), 'provider'))
children = dataitem.createChildren()
# Check spatialite and gpkg
spatialite_item = [i for i in children if i.path().endswith('spatialite.db')][0]
geopackage_item = [i for i in children if i.path().endswith('geopackage.gpkg')][0]
textfile_item = [i for i in children if i.path().endswith('.sql')][0]
self.assertIsNotNone(spatialite_item.databaseConnection())
self.assertIsNotNone(geopackage_item.databaseConnection())
self.assertIsNone(textfile_item.databaseConnection())


if __name__ == '__main__':

0 comments on commit ca4d4f1

Please sign in to comment.