Skip to content

Commit

Permalink
Bring back GDAL/OGR provider tests (and fix them)
Browse files Browse the repository at this point in the history
The tests got disabled by mistake in 62d615c
They have started to rot in the meanwhile, so this PR also fixes that:
- renaming of data items is now GUI thing - moved to a new OGR GUI provider test
- a test function was added in the test and never run, so it was not working at all
  • Loading branch information
wonder-sk committed May 17, 2020
1 parent db50e77 commit 6254927
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 39 deletions.
2 changes: 2 additions & 0 deletions tests/src/core/CMakeLists.txt
Expand Up @@ -121,6 +121,7 @@ SET(TESTS
testqgsfields.cpp
testqgsfield.cpp
testqgsfilledmarker.cpp
testqgsgdalprovider.cpp
testqgsgdalutils.cpp
testqgsvectorfilewriter.cpp
testqgsfontmarker.cpp
Expand Down Expand Up @@ -188,6 +189,7 @@ SET(TESTS
testqgsnetworkcontentfetcher.cpp
testqgsnewsfeedparser.cpp
testqgsogcutils.cpp
testqgsogrprovider.cpp
testqgsogrutils.cpp
testqgspagesizeregistry.cpp
testqgspainteffectregistry.cpp
Expand Down
43 changes: 4 additions & 39 deletions tests/src/core/testqgsogrprovider.cpp
Expand Up @@ -24,8 +24,6 @@
#include <qgsproviderregistry.h>
#include <qgsvectorlayer.h>
#include <qgsnetworkaccessmanager.h>
#include <qgsgeopackagedataitems.h>
#include <qgsdataitem.h>

#include <QObject>

Expand All @@ -50,8 +48,6 @@ class TestQgsOgrProvider : public QObject
void decodeUri();
void encodeUri();
void testThread();
//! Test GPKG data items rename
void testGpkgDataItemRename();

private:
QString mTestDataDir;
Expand Down Expand Up @@ -140,20 +136,20 @@ void TestQgsOgrProvider::encodeUri()
{
QVariantMap parts;
parts.insert( QStringLiteral( "path" ), QStringLiteral( "/home/user/test.gpkg" ) );
QCOMPARE( QgsProviderRegistry::instance()->encodeUri( QStringLiteral( "gdal" ), parts ), QStringLiteral( "/home/user/test.gpkg" ) );
QCOMPARE( QgsProviderRegistry::instance()->encodeUri( QStringLiteral( "ogr" ), parts ), QStringLiteral( "/home/user/test.gpkg" ) );

// layerName only
parts.insert( QStringLiteral( "layerName" ), QStringLiteral( "test" ) );
QCOMPARE( QgsProviderRegistry::instance()->encodeUri( QStringLiteral( "gdal" ), parts ), QStringLiteral( "/home/user/test.gpkg|layername=test" ) );
QCOMPARE( QgsProviderRegistry::instance()->encodeUri( QStringLiteral( "ogr" ), parts ), QStringLiteral( "/home/user/test.gpkg|layername=test" ) );
parts.remove( QStringLiteral( "layerName" ) );

// layerId only
parts.insert( QStringLiteral( "layerId" ), QStringLiteral( "testid" ) );
QCOMPARE( QgsProviderRegistry::instance()->encodeUri( QStringLiteral( "gdal" ), parts ), QStringLiteral( "/home/user/test.gpkg|layerid=testid" ) );
QCOMPARE( QgsProviderRegistry::instance()->encodeUri( QStringLiteral( "ogr" ), parts ), QStringLiteral( "/home/user/test.gpkg|layerid=testid" ) );

// Both layerName and layerId: layerName takes precedence
parts.insert( QStringLiteral( "layerName" ), QStringLiteral( "test" ) );
QCOMPARE( QgsProviderRegistry::instance()->encodeUri( QStringLiteral( "gdal" ), parts ), QStringLiteral( "/home/user/test.gpkg|layername=test|layername=test" ) );
QCOMPARE( QgsProviderRegistry::instance()->encodeUri( QStringLiteral( "ogr" ), parts ), QStringLiteral( "/home/user/test.gpkg|layername=test" ) );

}

Expand Down Expand Up @@ -244,37 +240,6 @@ void TestQgsOgrProvider::testThread()

}

void TestQgsOgrProvider::testGpkgDataItemRename()
{
QTemporaryFile f( QStringLiteral( "qgis-XXXXXX.gpkg" ) );
f.open();
f.close();
QString fileName { f.fileName( ) };
f.remove();
QVERIFY( QFile::copy( QStringLiteral( "%1/provider/bug_21227-rename-styles.gpkg" ).arg( mTestDataDir ), fileName ) );
QgsGeoPackageVectorLayerItem item( nullptr,
QStringLiteral( "Layer 1" ),
QStringLiteral( "gpkg:/%1|layername=layer 1" )
.arg( fileName ),
QStringLiteral( "%1|layername=layer 1" ).arg( fileName ),
QgsLayerItem::LayerType::TableLayer );
item.rename( "layer 3" );
// Check that the style is still available
QgsVectorLayer metadataLayer( QStringLiteral( "/%1|layername=layer_styles" ).arg( fileName ) );
QVERIFY( metadataLayer.isValid() );
QgsFeature feature;
QgsFeatureIterator it = metadataLayer.getFeatures( QgsFeatureRequest( QgsExpression( QStringLiteral( "\"f_table_name\" = 'layer 3'" ) ) ) );
QVERIFY( it.nextFeature( feature ) );
QVERIFY( feature.isValid() );
QCOMPARE( feature.attribute( QStringLiteral( "styleName" ) ).toString(), QString( "style for layer 1" ) );
it = metadataLayer.getFeatures( QgsFeatureRequest( QgsExpression( QStringLiteral( "\"f_table_name\" = 'layer 1' " ) ) ) );
QVERIFY( !it.nextFeature( feature ) );
it = metadataLayer.getFeatures( QgsFeatureRequest( QgsExpression( QStringLiteral( "\"f_table_name\" = 'layer 2' " ) ) ) );
QVERIFY( it.nextFeature( feature ) );
QVERIFY( feature.isValid() );
QCOMPARE( feature.attribute( QStringLiteral( "styleName" ) ).toString(), QString( "style for layer 2" ) );
}


QGSTEST_MAIN( TestQgsOgrProvider )
#include "testqgsogrprovider.moc"
2 changes: 2 additions & 0 deletions tests/src/gui/CMakeLists.txt
Expand Up @@ -27,6 +27,7 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/src/core/metadata
${CMAKE_SOURCE_DIR}/src/core/numericformats
${CMAKE_SOURCE_DIR}/src/core/processing
${CMAKE_SOURCE_DIR}/src/core/providers/ogr
${CMAKE_SOURCE_DIR}/src/core/raster
${CMAKE_SOURCE_DIR}/src/core/symbology
${CMAKE_SOURCE_DIR}/src/core/effects
Expand Down Expand Up @@ -164,3 +165,4 @@ ADD_QGIS_TEST(featurelistcombobox testqgsfeaturelistcombobox.cpp)
ADD_QGIS_TEST(texteditwrapper testqgstexteditwrapper.cpp)
ADD_QGIS_TEST(tableeditorwidget testqgstableeditor.cpp)
ADD_QGIS_TEST(newdatabasetablewidget testqgsnewdatabasetablewidget.cpp)
ADD_QGIS_TEST(ogrproviderguitest testqgsogrprovidergui.cpp)
133 changes: 133 additions & 0 deletions tests/src/gui/testqgsogrprovidergui.cpp
@@ -0,0 +1,133 @@
/***************************************************************************
testqgsogrprovidergui.cpp - TestQgsOgrProviderGui
---------------------
begin : 10.11.2017
copyright : (C) 2017 by Alessandro Pasotti
email : apasotti at boundlessgeo dot com
***************************************************************************
* *
* 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. *
* *
***************************************************************************/

#include "qgstest.h"

#include "qgsdataitemguiprovider.h"
#include "qgsdataitemguiproviderregistry.h"
#include "qgsgeopackagedataitems.h"
#include "qgsgui.h"
#include "qgsvectorlayer.h"

/**
* \ingroup UnitTests
* This is a unit test for the ogr provider GUI
*/
class TestQgsOgrProviderGui : public QObject
{
Q_OBJECT

private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.
void init() {}// will be called before each testfunction is executed.
void cleanup() {}// will be called after every testfunction.

void providersRegistered();
//! Test GPKG data items rename
void testGpkgDataItemRename();

private:
QString mTestDataDir;
};


//runs before all tests
void TestQgsOgrProviderGui::initTestCase()
{
// init QGIS's paths - true means that all path will be inited from prefix
QgsApplication::init();
QgsApplication::initQgis();

mTestDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt
}

void TestQgsOgrProviderGui::providersRegistered()
{
const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
bool hasOgrProvider = false;
bool hasGpkgProvider = false;
for ( QgsDataItemGuiProvider *provider : providers )
{
if ( provider->name() == QStringLiteral( "ogr_items" ) )
hasOgrProvider = true;
if ( provider->name() == QStringLiteral( "geopackage_items" ) )
hasGpkgProvider = true;
}
QVERIFY( hasOgrProvider );
QVERIFY( hasGpkgProvider );
}

//runs after all tests
void TestQgsOgrProviderGui::cleanupTestCase()
{
QgsApplication::exitQgis();
}

void TestQgsOgrProviderGui::testGpkgDataItemRename()
{
QTemporaryFile f( QStringLiteral( "qgis-XXXXXX.gpkg" ) );
f.open();
f.close();
QString fileName { f.fileName( ) };
f.remove();
QVERIFY( QFile::copy( QStringLiteral( "%1/provider/bug_21227-rename-styles.gpkg" ).arg( mTestDataDir ), fileName ) );

// create geopackage item and populate it with layers
QgsGeoPackageCollectionItem gpkgItem( nullptr, QStringLiteral( "test gpkg" ), QStringLiteral( "gpkg:/%1" ).arg( fileName ) );
gpkgItem.populate( true );
const QVector<QgsDataItem *> items = gpkgItem.children();
QgsDataItem *itemLayer1 = nullptr;
for ( QgsDataItem *item : items )
{
if ( item->name() == QStringLiteral( "layer 1" ) )
itemLayer1 = item;
}
QVERIFY( itemLayer1 );

// try to rename
const QList<QgsDataItemGuiProvider *> providers = QgsGui::dataItemGuiProviderRegistry()->providers();
bool success = false;
for ( QgsDataItemGuiProvider *provider : providers )
{
if ( provider->rename( itemLayer1, QStringLiteral( "layer 3" ), QgsDataItemGuiContext() ) )
{
success = true;
// also check it was the correct provider
QCOMPARE( provider->name(), QStringLiteral( "geopackage_items" ) );
}
}
QVERIFY( success );

// Check that the style is still available
QgsVectorLayer metadataLayer( QStringLiteral( "/%1|layername=layer_styles" ).arg( fileName ) );
QVERIFY( metadataLayer.isValid() );
QgsFeature feature;
QgsFeatureIterator it = metadataLayer.getFeatures( QgsFeatureRequest( QgsExpression( QStringLiteral( "\"f_table_name\" = 'layer 3'" ) ) ) );
QVERIFY( it.nextFeature( feature ) );
QVERIFY( feature.isValid() );
QCOMPARE( feature.attribute( QStringLiteral( "styleName" ) ).toString(), QString( "style for layer 1" ) );
it = metadataLayer.getFeatures( QgsFeatureRequest( QgsExpression( QStringLiteral( "\"f_table_name\" = 'layer 1' " ) ) ) );
QVERIFY( !it.nextFeature( feature ) );
it = metadataLayer.getFeatures( QgsFeatureRequest( QgsExpression( QStringLiteral( "\"f_table_name\" = 'layer 2' " ) ) ) );
QVERIFY( it.nextFeature( feature ) );
QVERIFY( feature.isValid() );
QCOMPARE( feature.attribute( QStringLiteral( "styleName" ) ).toString(), QString( "style for layer 2" ) );
}


QGSTEST_MAIN( TestQgsOgrProviderGui )
#include "testqgsogrprovidergui.moc"

0 comments on commit 6254927

Please sign in to comment.