Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add some unit tests for QgsProcessingRegistry
- Loading branch information
1 parent
dca697b
commit b71019d
Showing
2 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |