Add some unit tests for QgsProcessingRegistry

This commit is contained in:
Nyall Dawson 2017-01-09 11:41:10 +10:00
parent dca697b427
commit b71019dfda
2 changed files with 150 additions and 0 deletions

View File

@ -13,6 +13,7 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/src/core/geometry
${CMAKE_SOURCE_DIR}/src/core/effects
${CMAKE_SOURCE_DIR}/src/core/layertree
${CMAKE_SOURCE_DIR}/src/core/processing
${CMAKE_SOURCE_DIR}/src/core/raster
${CMAKE_SOURCE_DIR}/src/core/symbology-ng
${CMAKE_SOURCE_DIR}/src/test
@ -167,6 +168,7 @@ ADD_QGIS_TEST(pallabelingtest testqgspallabeling.cpp)
ADD_QGIS_TEST(pointlocatortest testqgspointlocator.cpp )
ADD_QGIS_TEST(pointpatternfillsymboltest testqgspointpatternfillsymbol.cpp )
ADD_QGIS_TEST(pointtest testqgspoint.cpp)
ADD_QGIS_TEST(processingtest testqgsprocessing.cpp)
ADD_QGIS_TEST(projecttest testqgsproject.cpp)
ADD_QGIS_TEST(qgistest testqgis.cpp)
ADD_QGIS_TEST(rasterfilewritertest testqgsrasterfilewriter.cpp)

View File

@ -0,0 +1,148 @@
/***************************************************************************
testqgsprocessing.cpp
---------------------
begin : January 2017
copyright : (C) 2017 by Nyall Dawson
email : nyall dot dawson at gmail 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 "qgsprocessingregistry.h"
#include "qgsprocessingprovider.h"
#include <QObject>
#include <QSharedPointer>
#include "qgstest.h"
//dummy provider for testing
class DummyProvider : public QgsProcessingProvider
{
public:
DummyProvider( const QString& id ) : mId( id ) {}
virtual QString id() const override { return mId; }
virtual QString name() const override { return "dummy"; }
QString mId;
};
class TestQgsProcessing: 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 instance();
void addProvider();
void providerById();
void removeProvider();
private:
};
void TestQgsProcessing::instance()
{
// test that application has a registry instance
QVERIFY( QgsApplication::processingRegistry() );
}
void TestQgsProcessing::addProvider()
{
QgsProcessingRegistry r;
QSignalSpy spyProviderAdded( &r, &QgsProcessingRegistry::providerAdded );
QVERIFY( r.providers().isEmpty() );
QVERIFY( !r.addProvider( nullptr ) );
// add a provider
DummyProvider* p = new DummyProvider( "p1" );
QVERIFY( r.addProvider( p ) );
QCOMPARE( r.providers(), QList< QgsProcessingProvider* >() << p );
QCOMPARE( spyProviderAdded.count(), 1 );
QCOMPARE( spyProviderAdded.last().at( 0 ).toString(), QString( "p1" ) );
//try adding another provider
DummyProvider* p2 = new DummyProvider( "p2" );
QVERIFY( r.addProvider( p2 ) );
QCOMPARE( r.providers().toSet(), QSet< QgsProcessingProvider* >() << p << p2 );
QCOMPARE( spyProviderAdded.count(), 2 );
QCOMPARE( spyProviderAdded.last().at( 0 ).toString(), QString( "p2" ) );
//try adding a provider with duplicate id
DummyProvider* p3 = new DummyProvider( "p2" );
QVERIFY( !r.addProvider( p3 ) );
QCOMPARE( r.providers().toSet(), QSet< QgsProcessingProvider* >() << p << p2 );
QCOMPARE( spyProviderAdded.count(), 2 );
}
void TestQgsProcessing::providerById()
{
QgsProcessingRegistry r;
// no providers
QVERIFY( !r.providerById( "p1" ) );
// add a provider
DummyProvider* p = new DummyProvider( "p1" );
QVERIFY( r.addProvider( p ) );
QCOMPARE( r.providerById( "p1" ), p );
QVERIFY( !r.providerById( "p2" ) );
//try adding another provider
DummyProvider* p2 = new DummyProvider( "p2" );
QVERIFY( r.addProvider( p2 ) );
QCOMPARE( r.providerById( "p1" ), p );
QCOMPARE( r.providerById( "p2" ), p2 );
QVERIFY( !r.providerById( "p3" ) );
}
void TestQgsProcessing::removeProvider()
{
QgsProcessingRegistry r;
QSignalSpy spyProviderRemoved( &r, &QgsProcessingRegistry::providerRemoved );
QVERIFY( !r.removeProvider( nullptr ) );
QVERIFY( !r.removeProvider( "p1" ) );
// provider not in registry
DummyProvider* p = new DummyProvider( "p1" );
QVERIFY( !r.removeProvider( p ) );
QCOMPARE( spyProviderRemoved.count(), 0 );
// add some providers
QVERIFY( r.addProvider( p ) );
DummyProvider* p2 = new DummyProvider( "p2" );
QVERIFY( r.addProvider( p2 ) );
// remove one by pointer
QVERIFY( r.removeProvider( p ) );
QCOMPARE( spyProviderRemoved.count(), 1 );
QCOMPARE( spyProviderRemoved.last().at( 0 ).toString(), QString( "p1" ) );
QCOMPARE( r.providers(), QList< QgsProcessingProvider* >() << p2 );
// should fail, already removed
QVERIFY( !r.removeProvider( "p1" ) );
// remove one by id
QVERIFY( r.removeProvider( "p2" ) );
QCOMPARE( spyProviderRemoved.count(), 2 );
QCOMPARE( spyProviderRemoved.last().at( 0 ).toString(), QString( "p2" ) );
QVERIFY( r.providers().isEmpty() );
}
QGSTEST_MAIN( TestQgsProcessing )
#include "testqgsprocessing.moc"