Skip to content

Commit

Permalink
Add unit test for package algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 23, 2017
1 parent a8eee62 commit fada3d9
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/CMakeLists.txt
Expand Up @@ -707,7 +707,6 @@ SET(QGIS_CORE_MOC_HDRS
processing/qgsprocessingfeedback.h
processing/qgsprocessingprovider.h
processing/qgsprocessingregistry.h
processing/models/qgsprocessingmodelalgorithm.h

providers/memory/qgsmemoryprovider.h

Expand Down Expand Up @@ -1025,6 +1024,7 @@ SET(QGIS_CORE_HDRS
processing/qgsprocessingoutputs.h
processing/qgsprocessingparameters.h
processing/qgsprocessingutils.h
processing/models/qgsprocessingmodelalgorithm.h
processing/models/qgsprocessingmodelchildalgorithm.h
processing/models/qgsprocessingmodelchildparametersource.h
processing/models/qgsprocessingmodelcomponent.h
Expand Down
1 change: 1 addition & 0 deletions tests/src/analysis/CMakeLists.txt
Expand Up @@ -71,6 +71,7 @@ SET(TESTS
testqgsgeometrysnapper.cpp
testqgsinterpolator.cpp
testqgsprocessing.cpp
testqgsprocessingalgs.cpp
testqgszonalstatistics.cpp
testqgsrastercalculator.cpp
testqgsalignraster.cpp
Expand Down
121 changes: 121 additions & 0 deletions tests/src/analysis/testqgsprocessingalgs.cpp
@@ -0,0 +1,121 @@
/***************************************************************************
testqgsprocessingalgs.cpp
---------------------
begin : November 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 "qgstest.h"
#include "qgsprocessingregistry.h"
#include "qgsprocessingprovider.h"
#include "qgsprocessingutils.h"
#include "qgsprocessingalgorithm.h"
#include "qgsprocessingcontext.h"
#include "qgsprocessingmodelalgorithm.h"
#include "qgsnativealgorithms.h"

class TestQgsProcessingAlgs: 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 packageAlg();

private:

QgsVectorLayer *mPointsLayer = nullptr;
QgsVectorLayer *mPolygonLayer = nullptr;

};

void TestQgsProcessingAlgs::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();

// Set up the QgsSettings environment
QCoreApplication::setOrganizationName( QStringLiteral( "QGIS" ) );
QCoreApplication::setOrganizationDomain( QStringLiteral( "qgis.org" ) );
QCoreApplication::setApplicationName( QStringLiteral( "QGIS-TEST" ) );

QgsApplication::processingRegistry()->addProvider( new QgsNativeAlgorithms( QgsApplication::processingRegistry() ) );

QString dataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt

QString pointsFileName = dataDir + "/points.shp";
QFileInfo pointFileInfo( pointsFileName );
mPointsLayer = new QgsVectorLayer( pointFileInfo.filePath(),
QStringLiteral( "points" ), QStringLiteral( "ogr" ) );
QVERIFY( mPointsLayer->isValid() );
// Register the layer with the registry
QgsProject::instance()->addMapLayers(
QList<QgsMapLayer *>() << mPointsLayer );

//
//create a poly layer that will be used in all tests...
//
QString polysFileName = dataDir + "/polys.shp";
QFileInfo polyFileInfo( polysFileName );
mPolygonLayer = new QgsVectorLayer( polyFileInfo.filePath(),
QStringLiteral( "polygons" ), QStringLiteral( "ogr" ) );
// Register the layer with the registry
QgsProject::instance()->addMapLayers(
QList<QgsMapLayer *>() << mPolygonLayer );
QVERIFY( mPolygonLayer->isValid() );
}

void TestQgsProcessingAlgs::cleanupTestCase()
{
QgsApplication::exitQgis();
}

void TestQgsProcessingAlgs::packageAlg()
{
const QgsProcessingAlgorithm *package( QgsApplication::processingRegistry()->algorithmById( QStringLiteral( "native:package" ) ) );
QVERIFY( package );

std::unique_ptr< QgsProcessingContext > context = qgis::make_unique< QgsProcessingContext >();
context->setProject( QgsProject::instance() );

QgsProcessingFeedback feedback;

QString outputGpkg = QDir::tempPath() + "/package_alg.gpkg";
if ( QFile::exists( outputGpkg ) )
QFile::remove( outputGpkg );

QVariantMap parameters;
parameters.insert( QStringLiteral( "LAYERS" ), QStringList() << mPointsLayer->id() << mPolygonLayer->id() );
parameters.insert( QStringLiteral( "OUTPUT" ), outputGpkg );
bool ok = false;
QVariantMap results = package->run( parameters, *context, &feedback, &ok );
QVERIFY( ok );
context.reset();

QVERIFY( !results.value( QStringLiteral( "OUTPUT" ) ).toString().isEmpty() );
std::unique_ptr< QgsVectorLayer > pointLayer = qgis::make_unique< QgsVectorLayer >( outputGpkg + "|layername=points", "points", "ogr" );
QVERIFY( pointLayer->isValid() );
QCOMPARE( pointLayer->wkbType(), mPointsLayer->wkbType() );
QCOMPARE( pointLayer->featureCount(), mPointsLayer->featureCount() );
std::unique_ptr< QgsVectorLayer > polygonLayer = qgis::make_unique< QgsVectorLayer >( outputGpkg + "|layername=polygons", "polygons", "ogr" );
QVERIFY( polygonLayer->isValid() );
QCOMPARE( polygonLayer->wkbType(), mPolygonLayer->wkbType() );
QCOMPARE( polygonLayer->featureCount(), mPolygonLayer->featureCount() );
}


QGSTEST_MAIN( TestQgsProcessingAlgs )
#include "testqgsprocessingalgs.moc"

0 comments on commit fada3d9

Please sign in to comment.