Skip to content

Commit

Permalink
Test databaseConnection
Browse files Browse the repository at this point in the history
(cherry picked from commit 37e5f94)
  • Loading branch information
elpaso authored and nyalldawson committed Feb 19, 2021
1 parent 8e39816 commit ca4d4f1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 33 deletions.
11 changes: 0 additions & 11 deletions src/core/providers/ogr/qgsogrdataitems.cpp
Expand Up @@ -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()
Expand Down Expand Up @@ -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
{
Expand Down
15 changes: 0 additions & 15 deletions src/core/providers/ogr/qgsogrdataitems.h
Expand Up @@ -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
Expand Down
26 changes: 19 additions & 7 deletions tests/src/python/test_qgsdataitem.py
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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__':
Expand Down

0 comments on commit ca4d4f1

Please sign in to comment.