-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some tests for QgsVectorLayerFeaturePool
- Loading branch information
Showing
2 changed files
with
172 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
171 changes: 171 additions & 0 deletions
171
tests/src/geometry_checker/testqgsvectorlayerfeaturepool.cpp
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,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" |