Skip to content

Commit

Permalink
Add some tests for QgsVectorLayerFeaturePool
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Oct 15, 2018
1 parent 3d31d72 commit 8da2910
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/src/geometry_checker/CMakeLists.txt
Expand Up @@ -58,3 +58,4 @@ ENDMACRO (ADD_QGIS_TEST)
# Tests:

ADD_QGIS_TEST(testqgsgeometrychecks.cpp)
ADD_QGIS_TEST(testqgsvectorlayerfeaturepool.cpp)
171 changes: 171 additions & 0 deletions tests/src/geometry_checker/testqgsvectorlayerfeaturepool.cpp
@@ -0,0 +1,171 @@
/***************************************************************************
testqgsgeometrychecks.cpp
--------------------------------------
Date : September 2017
Copyright : (C) 2017 Sandro Mani
Email : manisandro 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 "qgsvectorlayerfeaturepool.h"
#include "qgsvectorlayer.h"


class TestQgsVectorLayerFeaturePool : public QObject
{
Q_OBJECT

private slots:
void initTestCase();
void cleanupTestCase();
void getFeatures();
void addFeature();
void deleteFeature();
void changeGeometry();

private:
std::unique_ptr<QgsVectorLayer> createPopulatedLayer();
};

void TestQgsVectorLayerFeaturePool::initTestCase()
{
QgsApplication::initQgis();
}

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

void TestQgsVectorLayerFeaturePool::getFeatures()
{
std::unique_ptr<QgsVectorLayer> vl = createPopulatedLayer();
QgsVectorLayerFeaturePool pool( vl.get() );

QgsFeatureIds ids1 = pool.getFeatures( QgsFeatureRequest().setFilterRect( QgsRectangle( 0, 0, 10, 10 ) ) );

// One feature is within the requested area
QCOMPARE( ids1.size(), 1 );

QgsFeatureIds ids2 = pool.getIntersects( QgsRectangle( 0, 0, 10, 10 ) );
// Also one within the spatial index
QCOMPARE( ids2.size(), 1 );
}

void TestQgsVectorLayerFeaturePool::addFeature()
{
std::unique_ptr<QgsVectorLayer> vl = createPopulatedLayer();
QgsVectorLayerFeaturePool pool( vl.get() );

QgsFeatureIds ids1 = pool.getFeatures( QgsFeatureRequest().setFilterRect( QgsRectangle( 0, 0, 10, 10 ) ) );

// One feature is within the requested area
QCOMPARE( ids1.size(), 1 );

QgsFeatureIds ids2 = pool.getIntersects( QgsRectangle( 0, 0, 10, 10 ) );
// Also one within the spatial index
QCOMPARE( ids2.size(), 1 );

QgsFeature feature;
feature.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "Polygon((1 1, 9 1, 9 9, 1 9, 1 1))" ) ) );

// Add another feature...
vl->startEditing();

pool.addFeature( feature );

QgsFeatureIds ids3 = pool.getIntersects( QgsRectangle( 0, 0, 10, 10 ) );

// Two features within the requested area
QCOMPARE( ids3.size(), 2 );
}

void TestQgsVectorLayerFeaturePool::deleteFeature()
{
std::unique_ptr<QgsVectorLayer> vl = createPopulatedLayer();

QgsVectorLayerFeaturePool pool( vl.get() );

QgsFeatureIds ids1 = pool.getFeatures( QgsFeatureRequest().setFilterRect( QgsRectangle( 0, 0, 10, 10 ) ) );

// One feature is within the requested area
QCOMPARE( ids1.size(), 1 );

QgsFeatureIds ids2 = pool.getIntersects( QgsRectangle( 0, 0, 10, 10 ) );
// Also one within the spatial index
QCOMPARE( ids2.size(), 1 );

vl->startEditing();
// Delete a feature outside the AOI (0, 0, 10, 10)
pool.deleteFeature( 2 );

QgsFeatureIds ids3 = pool.getIntersects( QgsRectangle( 0, 0, 10, 10 ) );

// Still 1 feature
QCOMPARE( ids3.size(), 1 );

// Delete a feature inside the AOI (0, 0, 10, 10)
pool.deleteFeature( 1 );

QgsFeatureIds ids4 = pool.getIntersects( QgsRectangle( 0, 0, 10, 10 ) );

// No more features here
QCOMPARE( ids4.size(), 0 );
}

void TestQgsVectorLayerFeaturePool::changeGeometry()
{
std::unique_ptr<QgsVectorLayer> vl = createPopulatedLayer();

QgsVectorLayerFeaturePool pool( vl.get() );

// One feature is within the requested area
QgsFeatureIds ids1 = pool.getFeatures( QgsFeatureRequest().setFilterRect( QgsRectangle( 0, 0, 10, 10 ) ) );
QCOMPARE( ids1.size(), 1 );

// Also one when using the spatial index
QgsFeatureIds ids2 = pool.getIntersects( QgsRectangle( 0, 0, 10, 10 ) );
QCOMPARE( ids2.size(), 1 );

vl->startEditing();
QgsFeature feat;
pool.getFeature( 1, feat );

// Update a feature to be outside the AOI
feat.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "Polygon((100 100, 110 100, 110 110, 100 110, 100 100))" ) ) );
vl->updateFeature( feat );

// No features in the AOI
QgsFeatureIds ids3 = pool.getIntersects( QgsRectangle( 0, 0, 10, 10 ) );
QCOMPARE( ids3.size(), 0 );

// Update a feature to be inside the AOI
feat.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "Polygon((0 0, 10 0, 10 10, 0 10, 0 0))" ) ) );
vl->updateFeature( feat );

// One there again
QgsFeatureIds ids4 = pool.getIntersects( QgsRectangle( 0, 0, 10, 10 ) );
QCOMPARE( ids4.size(), 1 );
}

std::unique_ptr<QgsVectorLayer> TestQgsVectorLayerFeaturePool::createPopulatedLayer()
{
std::unique_ptr<QgsVectorLayer> vl = qgis::make_unique<QgsVectorLayer>( QStringLiteral( "Polygon" ), QStringLiteral( "Polygons" ), QStringLiteral( "memory" ) );

QgsFeature feature;
feature.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "Polygon((0 0, 10 0, 10 10, 0 10, 0 0))" ) ) );
vl->dataProvider()->addFeature( feature );
feature.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "Polygon((100 100, 110 100, 110 110, 100 110, 100 100))" ) ) );
vl->dataProvider()->addFeature( feature );

return vl;
}
QGSTEST_MAIN( TestQgsVectorLayerFeaturePool )
#include "testqgsvectorlayerfeaturepool.moc"

0 comments on commit 8da2910

Please sign in to comment.