Skip to content

Commit

Permalink
Add tests for native polygonstolines algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Jan 20, 2019
1 parent 6c110c8 commit 23885e5
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions tests/src/analysis/testqgsprocessingalgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,21 @@
#include "qgsvectorlayer.h"
#include "qgscategorizedsymbolrenderer.h"
#include "qgssinglesymbolrenderer.h"
#include "qgsmultipolygon.h"

class TestQgsProcessingAlgs: public QObject
{
Q_OBJECT

private:

/**
* Helper function to get a feature based algorithm.
*/
std::unique_ptr<QgsProcessingFeatureBasedAlgorithm> featureBasedAlg( const QString &id );

QgsFeature runForFeature( const std::unique_ptr<QgsProcessingFeatureBasedAlgorithm> &alg, QgsFeature feature, const QString &layerType );

private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase(); // will be called after the last testfunction was executed.
Expand All @@ -48,6 +58,8 @@ class TestQgsProcessingAlgs: public QObject
void kmeansCluster();
void categorizeByStyle();
void extractBinary();
void polygonsToLines_data();
void polygonsToLines();

private:

Expand All @@ -57,6 +69,39 @@ class TestQgsProcessingAlgs: public QObject

};

std::unique_ptr<QgsProcessingFeatureBasedAlgorithm> TestQgsProcessingAlgs::featureBasedAlg( const QString &id )
{
return std::unique_ptr<QgsProcessingFeatureBasedAlgorithm>( static_cast<QgsProcessingFeatureBasedAlgorithm *>( QgsApplication::processingRegistry()->createAlgorithmById( id ) ) );
}

QgsFeature TestQgsProcessingAlgs::runForFeature( const std::unique_ptr< QgsProcessingFeatureBasedAlgorithm > &alg, QgsFeature feature, const QString &layerType )
{
std::unique_ptr< QgsProcessingContext > context = qgis::make_unique< QgsProcessingContext >();
QgsProject p;
context->setProject( &p );

QgsProcessingFeedback feedback;
context->setFeedback( &feedback );

QVariantMap parameters;

std::unique_ptr<QgsVectorLayer> inputLayer( qgis::make_unique<QgsVectorLayer>( layerType, QStringLiteral( "layer" ), QStringLiteral( "memory" ) ) );
inputLayer->dataProvider()->addFeature( feature );

parameters.insert( QStringLiteral( "INPUT" ), QVariant::fromValue<QgsMapLayer *>( inputLayer.get() ) );

parameters.insert( QStringLiteral( "OUTPUT" ), QStringLiteral( "memory:" ) );

bool ok = false;
auto res = alg->run( parameters, *context, &feedback, &ok );
QgsFeature result;

std::unique_ptr<QgsVectorLayer> outputLayer( qobject_cast< QgsVectorLayer * >( context->getMapLayer( res.value( QStringLiteral( "OUTPUT" ) ).toString() ) ) );
outputLayer->getFeatures().nextFeature( result );

return result;
}

void TestQgsProcessingAlgs::initTestCase()
{
QgsApplication::init();
Expand Down Expand Up @@ -668,5 +713,45 @@ void TestQgsProcessingAlgs::extractBinary()
}


void TestQgsProcessingAlgs::polygonsToLines_data()
{
QTest::addColumn<QgsGeometry>( "sourceGeometry" );
QTest::addColumn<QgsGeometry>( "expectedGeometry" );

QTest::newRow( "Simple Polygon" )
<< QgsGeometry::fromWkt( "Polygon((1 1, 2 2, 1 3, 1 1))" )
<< QgsGeometry::fromWkt( "MultiLineString ((1 1, 2 2, 1 3, 1 1))" );

QgsGeometry geomNoRing( qgis::make_unique<QgsMultiPolygon>() );

QTest::newRow( "Polygon without exterior ring" )
<< geomNoRing
<< QgsGeometry::fromWkt( "MultiLineString ()" );

QTest::newRow( "MultiPolygon" )
<< QgsGeometry::fromWkt( "MultiPolygon(((1 1, 2 2, 1 3, 1 1)), ((0 0, 0 10, 10 10, 10 0, 0 0), (3 3, 3 6, 6 6, 6 3, 3 3)))" )
<< QgsGeometry::fromWkt( "MultiLineString ((1 1, 2 2, 1 3, 1 1),(0 0, 0 10, 10 10, 10 0, 0 0),(3 3, 3 6, 6 6, 6 3, 3 3))" );

QTest::newRow( "Polygon with inner ring" )
<< QgsGeometry::fromWkt( "Polygon((0 0, 0 10, 10 10, 10 0, 0 0), (3 3, 3 6, 6 6, 6 3, 3 3))" )
<< QgsGeometry::fromWkt( "MultiLineString ((0 0, 0 10, 10 10, 10 0, 0 0),(3 3, 3 6, 6 6, 6 3, 3 3))" );
}


void TestQgsProcessingAlgs::polygonsToLines()
{
QFETCH( QgsGeometry, sourceGeometry );
QFETCH( QgsGeometry, expectedGeometry );

std::unique_ptr< QgsProcessingFeatureBasedAlgorithm > alg( featureBasedAlg( "native:polygonstolines" ) );

QgsFeature feature;
feature.setGeometry( sourceGeometry );

QgsFeature result = runForFeature( alg, feature, QStringLiteral( "Polygon" ) );

QVERIFY2( result.geometry().equals( expectedGeometry ), QStringLiteral( "Result: %1, Expected: %2" ).arg( result.geometry().asWkt(), expectedGeometry.asWkt() ).toUtf8().constData() );
}

QGSTEST_MAIN( TestQgsProcessingAlgs )
#include "testqgsprocessingalgs.moc"

0 comments on commit 23885e5

Please sign in to comment.