/
testqgsvectorlayercache.cpp
386 lines (321 loc) · 13.2 KB
/
testqgsvectorlayercache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/***************************************************************************
testqgsvectorlayercache.cpp
--------------------------------------
Date : 20.2.2013
Copyright : (C) 2013 Matthias Kuhn
Email : matthias at opengis dot ch
***************************************************************************
* *
* 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 <QtTest/QtTest>
#include <QObject>
#include <QTemporaryFile>
//qgis includes...
#include <qgsvectorlayercache.h>
#include <qgsvectordataprovider.h>
#include <qgsapplication.h>
#include <qgsvectorlayereditbuffer.h>
#include <qgscacheindexfeatureid.h>
#include <QDebug>
/** @ingroup UnitTests
* This is a unit test for the vector layer cache
*
* @see QgsVectorLayerCache
*/
class TestVectorLayerCache : public QObject
{
Q_OBJECT
public:
TestVectorLayerCache()
: mVectorLayerCache( 0 )
, mFeatureIdIndex( 0 )
, mPointsLayer( 0 )
{}
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 testCacheOverflow(); // Test cache will work if too many features to cache them all are present
void testCacheAttrActions(); // Test attribute add/ attribute delete
void testFeatureActions(); // Test adding/removing features works
void testSubsetRequest();
void testFullCache();
void testFullCacheThroughRequest();
void testCanUseCacheForRequest();
void testCacheGeom();
void onCommittedFeaturesAdded( const QString&, const QgsFeatureList& );
private:
QgsVectorLayerCache* mVectorLayerCache;
QgsCacheIndexFeatureId* mFeatureIdIndex;
QgsVectorLayer* mPointsLayer;
QgsFeatureList mAddedFeatures;
QMap<QString, QString> mTmpFiles;
};
// runs before all tests
void TestVectorLayerCache::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();
QgsApplication::showSettings();
// Backup test shape file and attributes
QStringList backupFiles;
backupFiles << "points.shp" << "points.shx" << "points.dbf" << "points.prj";
QString myDataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
QString myTestDataDir = myDataDir + '/';
Q_FOREACH ( const QString& f, backupFiles )
{
QString origFileName = myTestDataDir + f;
QFileInfo origFileInfo( origFileName );
QString tmpFileName = QDir::tempPath() + '/' + origFileInfo.baseName() + '_' + QString::number( qApp->applicationPid() ) + '.' + origFileInfo.completeSuffix();
qDebug() << "Copy " << origFileName << " " << tmpFileName;
qDebug() << QFile::copy( origFileName, tmpFileName );
mTmpFiles.insert( origFileName, tmpFileName );
}
//
// load a vector layer
//
QString myPointsFileName = mTmpFiles.value( myTestDataDir + "points.shp" );
QFileInfo myPointFileInfo( myPointsFileName );
mPointsLayer = new QgsVectorLayer( myPointFileInfo.filePath(),
myPointFileInfo.completeBaseName(), "ogr" );
}
void TestVectorLayerCache::init()
{
mVectorLayerCache = new QgsVectorLayerCache( mPointsLayer, 10 );
mFeatureIdIndex = new QgsCacheIndexFeatureId( mVectorLayerCache );
mVectorLayerCache->addCacheIndex( mFeatureIdIndex );
}
void TestVectorLayerCache::cleanup()
{
delete mVectorLayerCache;
}
//runs after all tests
void TestVectorLayerCache::cleanupTestCase()
{
delete mPointsLayer;
// Clean tmp files
QMap<QString, QString>::const_iterator it;
for ( it = mTmpFiles.constBegin(); it != mTmpFiles.constEnd(); ++it )
{
QString tmpFileName = it.value();
qDebug() << "Remove " << tmpFileName;
QFile::remove( tmpFileName );
}
// also clean up newly created .qix file
QFile::remove( QString( TEST_DATA_DIR ) + "/points.qix" );
QgsApplication::exitQgis();
}
void TestVectorLayerCache::testCacheOverflow()
{
QgsFeature f;
// Verify we get all features, even if there are too many to fit into the cache
QgsFeatureIterator it = mVectorLayerCache->getFeatures();
int i = 0;
while ( it.nextFeature( f ) )
{
i++;
}
it.close();
QVERIFY( i == 17 );
}
void TestVectorLayerCache::testCacheAttrActions()
{
QgsFeature f;
// Add an attribute, make sure it is returned also if a cached feature is requested
mPointsLayer->startEditing();
QVariant::Type attrType = QVariant::Int;
mPointsLayer->addAttribute( QgsField( "newAttr", attrType, "Int", 5, 0 ) );
mPointsLayer->commitChanges();
QVERIFY( mVectorLayerCache->featureAtId( 15, f ) );
QVERIFY( f.attribute( "newAttr" ).isValid() );
QgsFields allFields = mPointsLayer->fields();
int idx = allFields.indexFromName( "newAttr" );
mPointsLayer->startEditing();
mPointsLayer->deleteAttribute( idx );
mPointsLayer->commitChanges();
QVERIFY( mVectorLayerCache->featureAtId( 15, f ) );
QVERIFY( !f.attribute( "newAttr" ).isValid() );
}
void TestVectorLayerCache::testFeatureActions()
{
QgsFeature f;
// Get a random feature to clone
mPointsLayer->getFeatures( QgsFeatureRequest().setFilterFid( 1 ) ).nextFeature( f );
// Add feature...
mPointsLayer->startEditing();
QVERIFY( mPointsLayer->addFeature( f ) );
connect( mPointsLayer, SIGNAL( committedFeaturesAdded( QString, QgsFeatureList ) ), SLOT( onCommittedFeaturesAdded( QString, QgsFeatureList ) ) );
mPointsLayer->commitChanges();
disconnect( mPointsLayer, SIGNAL( committedFeaturesAdded( QString, QgsFeatureList ) ), this, SLOT( onCommittedFeaturesAdded( QString, QgsFeatureList ) ) );
QgsFeatureId fid = mAddedFeatures.last().id();
QVERIFY( mVectorLayerCache->featureAtId( fid, f ) );
// Delete feature...
mPointsLayer->startEditing();
QVERIFY( mPointsLayer->deleteFeature( fid ) );
QVERIFY( !mVectorLayerCache->featureAtId( fid, f ) );
mPointsLayer->rollBack();
}
void TestVectorLayerCache::testSubsetRequest()
{
QgsFeature f;
QgsFields fields = mPointsLayer->fields();
QStringList requiredFields;
requiredFields << "Class" << "Cabin Crew";
mVectorLayerCache->featureAtId( 16, f );
QVariant a = f.attribute( 3 );
QgsFeatureIterator itSubset = mVectorLayerCache->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( requiredFields, fields ) );
while ( itSubset.nextFeature( f ) ) {}
itSubset.close();
mVectorLayerCache->featureAtId( 16, f );
QVERIFY( a == f.attribute( 3 ) );
}
void TestVectorLayerCache::testFullCache()
{
// cache is too small to fit all features
QgsVectorLayerCache cache( mPointsLayer, 2 );
QVERIFY( !cache.hasFullCache() );
QVERIFY( cache.cacheSize() < mPointsLayer->featureCount() );
// but we set it to full cache
cache.setFullCache( true );
// so now it should have sufficient size for all features
QVERIFY( cache.cacheSize() >= mPointsLayer->featureCount() );
QVERIFY( cache.hasFullCache() );
// double check that everything is indeed in the cache
QgsFeatureIterator it = mPointsLayer->getFeatures();
QgsFeature f;
while ( it.nextFeature( f ) )
{
QVERIFY( cache.isFidCached( f.id() ) );
}
}
void TestVectorLayerCache::testFullCacheThroughRequest()
{
// make sure cache is sufficient size for all features
QgsVectorLayerCache cache( mPointsLayer, mPointsLayer->featureCount() * 2 );
QVERIFY( !cache.hasFullCache() );
// now request all features from cache
QgsFeatureIterator it = cache.getFeatures( QgsFeatureRequest() );
QgsFeature f;
while ( it.nextFeature( f ) )
{
// suck in all features
}
// cache should now contain all features
it = mPointsLayer->getFeatures();
while ( it.nextFeature( f ) )
{
QVERIFY( cache.isFidCached( f.id() ) );
}
// so it should be a full cache!
QVERIFY( cache.hasFullCache() );
}
void TestVectorLayerCache::testCanUseCacheForRequest()
{
//first get some feature ids from layer
QgsFeature f;
QgsFeatureIterator it = mPointsLayer->getFeatures();
it.nextFeature( f );
QgsFeatureId id1 = f.id();
it.nextFeature( f );
QgsFeatureId id2 = f.id();
QgsVectorLayerCache cache( mPointsLayer, 10 );
// initially nothing in cache, so can't use it to fulfill the request
QVERIFY( !cache.canUseCacheForRequest( QgsFeatureRequest().setFilterFid( id1 ), it ) );
QVERIFY( !cache.canUseCacheForRequest( QgsFeatureRequest().setFilterFid( id2 ), it ) );
QVERIFY( !cache.canUseCacheForRequest( QgsFeatureRequest().setFilterFids( QgsFeatureIds() << id1 << id2 ), it ) );
QVERIFY( !cache.canUseCacheForRequest( QgsFeatureRequest().setFilterRect( QgsRectangle( 1, 2, 3, 4 ) ), it ) );
QVERIFY( !cache.canUseCacheForRequest( QgsFeatureRequest().setFilterExpression( "$x<5" ), it ) );
// get just the first feature into the cache
it = cache.getFeatures( QgsFeatureRequest().setFilterFid( id1 ) );
while ( it.nextFeature( f ) ) { }
QCOMPARE( cache.cachedFeatureIds(), QgsFeatureIds() << id1 );
QVERIFY( cache.canUseCacheForRequest( QgsFeatureRequest().setFilterFid( id1 ), it ) );
//verify that the returned iterator was correct
QVERIFY( it.nextFeature( f ) );
QCOMPARE( f.id(), id1 );
QVERIFY( !it.nextFeature( f ) );
QVERIFY( !cache.canUseCacheForRequest( QgsFeatureRequest().setFilterFid( id2 ), it ) );
QVERIFY( !cache.canUseCacheForRequest( QgsFeatureRequest().setFilterFids( QgsFeatureIds() << id1 << id2 ), it ) );
QVERIFY( !cache.canUseCacheForRequest( QgsFeatureRequest().setFilterRect( QgsRectangle( 1, 2, 3, 4 ) ), it ) );
QVERIFY( !cache.canUseCacheForRequest( QgsFeatureRequest().setFilterExpression( "$x<5" ), it ) );
// get feature 2 into cache
it = cache.getFeatures( QgsFeatureRequest().setFilterFid( id2 ) );
while ( it.nextFeature( f ) ) { }
QCOMPARE( cache.cachedFeatureIds(), QgsFeatureIds() << id1 << id2 );
QVERIFY( cache.canUseCacheForRequest( QgsFeatureRequest().setFilterFid( id1 ), it ) );
QVERIFY( it.nextFeature( f ) );
QCOMPARE( f.id(), id1 );
QVERIFY( !it.nextFeature( f ) );
QVERIFY( cache.canUseCacheForRequest( QgsFeatureRequest().setFilterFid( id2 ), it ) );
QVERIFY( it.nextFeature( f ) );
QCOMPARE( f.id(), id2 );
QVERIFY( !it.nextFeature( f ) );
QVERIFY( cache.canUseCacheForRequest( QgsFeatureRequest().setFilterFids( QgsFeatureIds() << id1 << id2 ), it ) );
QVERIFY( it.nextFeature( f ) );
QgsFeatureIds result;
result << f.id();
QVERIFY( it.nextFeature( f ) );
result << f.id();
QCOMPARE( result, QgsFeatureIds() << id1 << id2 );
QVERIFY( !cache.canUseCacheForRequest( QgsFeatureRequest().setFilterRect( QgsRectangle( 1, 2, 3, 4 ) ), it ) );
QVERIFY( !cache.canUseCacheForRequest( QgsFeatureRequest().setFilterExpression( "$x<5" ), it ) );
// can only use rect/expression requests if cache has everything
cache.setFullCache( true );
QVERIFY( cache.canUseCacheForRequest( QgsFeatureRequest().setFilterFid( id1 ), it ) );
QVERIFY( cache.canUseCacheForRequest( QgsFeatureRequest().setFilterFid( id2 ), it ) );
QVERIFY( cache.canUseCacheForRequest( QgsFeatureRequest().setFilterFids( QgsFeatureIds() << id1 << id2 ), it ) );
QVERIFY( cache.canUseCacheForRequest( QgsFeatureRequest().setFilterRect( QgsRectangle( 1, 2, 3, 4 ) ), it ) );
QVERIFY( cache.canUseCacheForRequest( QgsFeatureRequest().setFilterExpression( "$x<5" ), it ) );
}
void TestVectorLayerCache::testCacheGeom()
{
QgsVectorLayerCache cache( mPointsLayer, 2 );
// cache geometry
cache.setCacheGeometry( true );
//first get some feature ids from layer
QgsFeature f;
QgsFeatureIterator it = mPointsLayer->getFeatures();
it.nextFeature( f );
QgsFeatureId id1 = f.id();
it.nextFeature( f );
QgsFeatureId id2 = f.id();
QgsFeatureRequest req;
req.setFlags( QgsFeatureRequest::NoGeometry ); // should be ignored by cache
req.setFilterFids( QgsFeatureIds() << id1 << id2 );
it = cache.getFeatures( req );
while ( it.nextFeature( f ) )
{
QVERIFY( f.constGeometry() );
}
// disabled geometry caching
cache.setCacheGeometry( false );
// we should still have cached features... no need to lose these!
QCOMPARE( cache.cachedFeatureIds(), QgsFeatureIds() << id1 << id2 );
it = cache.getFeatures( req );
while ( it.nextFeature( f ) )
{
QVERIFY( f.constGeometry() );
}
// now upgrade cache from no geometry -> geometry, should be cleared since we
// cannot be confident that features existing in the cache have geometry
cache.setCacheGeometry( true );
QVERIFY( cache.cachedFeatureIds().isEmpty() );
it = cache.getFeatures( req );
while ( it.nextFeature( f ) )
{
QVERIFY( f.constGeometry() );
}
}
void TestVectorLayerCache::onCommittedFeaturesAdded( const QString& layerId, const QgsFeatureList& features )
{
Q_UNUSED( layerId )
mAddedFeatures.append( features );
}
QTEST_MAIN( TestVectorLayerCache )
#include "testqgsvectorlayercache.moc"