@@ -54,6 +54,7 @@ class TestVectorLayerCache : public QObject
5454 void testFullCache ();
5555 void testFullCacheThroughRequest ();
5656 void testCanUseCacheForRequest ();
57+ void testCacheGeom ();
5758
5859 void onCommittedFeaturesAdded ( const QString&, const QgsFeatureList& );
5960
@@ -330,6 +331,51 @@ void TestVectorLayerCache::testCanUseCacheForRequest()
330331 QVERIFY ( cache.canUseCacheForRequest ( QgsFeatureRequest ().setFilterExpression ( " $x<5" ), it ) );
331332}
332333
334+ void TestVectorLayerCache::testCacheGeom ()
335+ {
336+ QgsVectorLayerCache cache ( mPointsLayer , 2 );
337+ // cache geometry
338+ cache.setCacheGeometry ( true );
339+
340+ // first get some feature ids from layer
341+ QgsFeature f;
342+ QgsFeatureIterator it = mPointsLayer ->getFeatures ();
343+ it.nextFeature ( f );
344+ QgsFeatureId id1 = f.id ();
345+ it.nextFeature ( f );
346+ QgsFeatureId id2 = f.id ();
347+
348+ QgsFeatureRequest req;
349+ req.setFlags ( QgsFeatureRequest::NoGeometry ); // should be ignored by cache
350+ req.setFilterFids ( QgsFeatureIds () << id1 << id2 );
351+
352+ it = cache.getFeatures ( req );
353+ while ( it.nextFeature ( f ) )
354+ {
355+ QVERIFY ( f.constGeometry () );
356+ }
357+
358+ // disabled geometry caching
359+ cache.setCacheGeometry ( false );
360+ // we should still have cached features... no need to lose these!
361+ QCOMPARE ( cache.cachedFeatureIds (), QgsFeatureIds () << id1 << id2 );
362+ it = cache.getFeatures ( req );
363+ while ( it.nextFeature ( f ) )
364+ {
365+ QVERIFY ( f.constGeometry () );
366+ }
367+
368+ // now upgrade cache from no geometry -> geometry, should be cleared since we
369+ // cannot be confident that features existing in the cache have geometry
370+ cache.setCacheGeometry ( true );
371+ QVERIFY ( cache.cachedFeatureIds ().isEmpty () );
372+ it = cache.getFeatures ( req );
373+ while ( it.nextFeature ( f ) )
374+ {
375+ QVERIFY ( f.constGeometry () );
376+ }
377+ }
378+
333379void TestVectorLayerCache::onCommittedFeaturesAdded ( const QString& layerId, const QgsFeatureList& features )
334380{
335381 Q_UNUSED ( layerId )
0 commit comments