Skip to content

Commit 8da2910

Browse files
committed
Add some tests for QgsVectorLayerFeaturePool
1 parent 3d31d72 commit 8da2910

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

tests/src/geometry_checker/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,4 @@ ENDMACRO (ADD_QGIS_TEST)
5858
# Tests:
5959

6060
ADD_QGIS_TEST(testqgsgeometrychecks.cpp)
61+
ADD_QGIS_TEST(testqgsvectorlayerfeaturepool.cpp)
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/***************************************************************************
2+
testqgsgeometrychecks.cpp
3+
--------------------------------------
4+
Date : September 2017
5+
Copyright : (C) 2017 Sandro Mani
6+
Email : manisandro at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include "qgstest.h"
17+
#include "qgsvectorlayerfeaturepool.h"
18+
#include "qgsvectorlayer.h"
19+
20+
21+
class TestQgsVectorLayerFeaturePool : public QObject
22+
{
23+
Q_OBJECT
24+
25+
private slots:
26+
void initTestCase();
27+
void cleanupTestCase();
28+
void getFeatures();
29+
void addFeature();
30+
void deleteFeature();
31+
void changeGeometry();
32+
33+
private:
34+
std::unique_ptr<QgsVectorLayer> createPopulatedLayer();
35+
};
36+
37+
void TestQgsVectorLayerFeaturePool::initTestCase()
38+
{
39+
QgsApplication::initQgis();
40+
}
41+
42+
void TestQgsVectorLayerFeaturePool::cleanupTestCase()
43+
{
44+
QgsApplication::exitQgis();
45+
}
46+
47+
void TestQgsVectorLayerFeaturePool::getFeatures()
48+
{
49+
std::unique_ptr<QgsVectorLayer> vl = createPopulatedLayer();
50+
QgsVectorLayerFeaturePool pool( vl.get() );
51+
52+
QgsFeatureIds ids1 = pool.getFeatures( QgsFeatureRequest().setFilterRect( QgsRectangle( 0, 0, 10, 10 ) ) );
53+
54+
// One feature is within the requested area
55+
QCOMPARE( ids1.size(), 1 );
56+
57+
QgsFeatureIds ids2 = pool.getIntersects( QgsRectangle( 0, 0, 10, 10 ) );
58+
// Also one within the spatial index
59+
QCOMPARE( ids2.size(), 1 );
60+
}
61+
62+
void TestQgsVectorLayerFeaturePool::addFeature()
63+
{
64+
std::unique_ptr<QgsVectorLayer> vl = createPopulatedLayer();
65+
QgsVectorLayerFeaturePool pool( vl.get() );
66+
67+
QgsFeatureIds ids1 = pool.getFeatures( QgsFeatureRequest().setFilterRect( QgsRectangle( 0, 0, 10, 10 ) ) );
68+
69+
// One feature is within the requested area
70+
QCOMPARE( ids1.size(), 1 );
71+
72+
QgsFeatureIds ids2 = pool.getIntersects( QgsRectangle( 0, 0, 10, 10 ) );
73+
// Also one within the spatial index
74+
QCOMPARE( ids2.size(), 1 );
75+
76+
QgsFeature feature;
77+
feature.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "Polygon((1 1, 9 1, 9 9, 1 9, 1 1))" ) ) );
78+
79+
// Add another feature...
80+
vl->startEditing();
81+
82+
pool.addFeature( feature );
83+
84+
QgsFeatureIds ids3 = pool.getIntersects( QgsRectangle( 0, 0, 10, 10 ) );
85+
86+
// Two features within the requested area
87+
QCOMPARE( ids3.size(), 2 );
88+
}
89+
90+
void TestQgsVectorLayerFeaturePool::deleteFeature()
91+
{
92+
std::unique_ptr<QgsVectorLayer> vl = createPopulatedLayer();
93+
94+
QgsVectorLayerFeaturePool pool( vl.get() );
95+
96+
QgsFeatureIds ids1 = pool.getFeatures( QgsFeatureRequest().setFilterRect( QgsRectangle( 0, 0, 10, 10 ) ) );
97+
98+
// One feature is within the requested area
99+
QCOMPARE( ids1.size(), 1 );
100+
101+
QgsFeatureIds ids2 = pool.getIntersects( QgsRectangle( 0, 0, 10, 10 ) );
102+
// Also one within the spatial index
103+
QCOMPARE( ids2.size(), 1 );
104+
105+
vl->startEditing();
106+
// Delete a feature outside the AOI (0, 0, 10, 10)
107+
pool.deleteFeature( 2 );
108+
109+
QgsFeatureIds ids3 = pool.getIntersects( QgsRectangle( 0, 0, 10, 10 ) );
110+
111+
// Still 1 feature
112+
QCOMPARE( ids3.size(), 1 );
113+
114+
// Delete a feature inside the AOI (0, 0, 10, 10)
115+
pool.deleteFeature( 1 );
116+
117+
QgsFeatureIds ids4 = pool.getIntersects( QgsRectangle( 0, 0, 10, 10 ) );
118+
119+
// No more features here
120+
QCOMPARE( ids4.size(), 0 );
121+
}
122+
123+
void TestQgsVectorLayerFeaturePool::changeGeometry()
124+
{
125+
std::unique_ptr<QgsVectorLayer> vl = createPopulatedLayer();
126+
127+
QgsVectorLayerFeaturePool pool( vl.get() );
128+
129+
// One feature is within the requested area
130+
QgsFeatureIds ids1 = pool.getFeatures( QgsFeatureRequest().setFilterRect( QgsRectangle( 0, 0, 10, 10 ) ) );
131+
QCOMPARE( ids1.size(), 1 );
132+
133+
// Also one when using the spatial index
134+
QgsFeatureIds ids2 = pool.getIntersects( QgsRectangle( 0, 0, 10, 10 ) );
135+
QCOMPARE( ids2.size(), 1 );
136+
137+
vl->startEditing();
138+
QgsFeature feat;
139+
pool.getFeature( 1, feat );
140+
141+
// Update a feature to be outside the AOI
142+
feat.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "Polygon((100 100, 110 100, 110 110, 100 110, 100 100))" ) ) );
143+
vl->updateFeature( feat );
144+
145+
// No features in the AOI
146+
QgsFeatureIds ids3 = pool.getIntersects( QgsRectangle( 0, 0, 10, 10 ) );
147+
QCOMPARE( ids3.size(), 0 );
148+
149+
// Update a feature to be inside the AOI
150+
feat.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "Polygon((0 0, 10 0, 10 10, 0 10, 0 0))" ) ) );
151+
vl->updateFeature( feat );
152+
153+
// One there again
154+
QgsFeatureIds ids4 = pool.getIntersects( QgsRectangle( 0, 0, 10, 10 ) );
155+
QCOMPARE( ids4.size(), 1 );
156+
}
157+
158+
std::unique_ptr<QgsVectorLayer> TestQgsVectorLayerFeaturePool::createPopulatedLayer()
159+
{
160+
std::unique_ptr<QgsVectorLayer> vl = qgis::make_unique<QgsVectorLayer>( QStringLiteral( "Polygon" ), QStringLiteral( "Polygons" ), QStringLiteral( "memory" ) );
161+
162+
QgsFeature feature;
163+
feature.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "Polygon((0 0, 10 0, 10 10, 0 10, 0 0))" ) ) );
164+
vl->dataProvider()->addFeature( feature );
165+
feature.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "Polygon((100 100, 110 100, 110 110, 100 110, 100 100))" ) ) );
166+
vl->dataProvider()->addFeature( feature );
167+
168+
return vl;
169+
}
170+
QGSTEST_MAIN( TestQgsVectorLayerFeaturePool )
171+
#include "testqgsvectorlayerfeaturepool.moc"

0 commit comments

Comments
 (0)