Skip to content
Permalink
Browse files
Add tests for QgsVectorLayerCache
  • Loading branch information
m-kuhn authored and jef-n committed Mar 27, 2013
1 parent e0c8aea commit 9154ed1ad606a9cfbb9e5edbdf18fee452ddaec0
Showing with 207 additions and 0 deletions.
  1. +1 −0 tests/src/core/CMakeLists.txt
  2. +206 −0 tests/src/core/testqgsvectorlayercache.cpp
@@ -110,3 +110,4 @@ ADD_QGIS_TEST(composerhtmltest testqgscomposerhtml.cpp )
ADD_QGIS_TEST(rectangletest testqgsrectangle.cpp)
ADD_QGIS_TEST(composerscalebartest testqgscomposerscalebar.cpp )
ADD_QGIS_TEST(ogcutilstest testqgsogcutils.cpp)
ADD_QGIS_TEST(vectorlayercachetest testqgsvectorlayercache.cpp )
@@ -0,0 +1,206 @@
/***************************************************************************
testqgsvectorlayercache.cpp
--------------------------------------
Date : 20.2.2013
Copyright : (C) 2013 Matthias Kuhn
Email : matthias dot kuhn at gmx 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>
#include <QObject>
#include <QTemporaryFile>

//qgis includes...
#include <qgsvectorlayercache.h>
#include <qgsvectordataprovider.h>
#include <qgsapplication.h>
#include <qgsvectorlayereditbuffer.h>

/** @ingroup UnitTests
* This is a unit test for the vector layer cache
*
* @see QgsVectorLayerCache
*/
class TestVectorLayerCache: public QObject
{
Q_OBJECT

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 onCommittedFeaturesAdded( QString, QgsFeatureList );

private:
QgsVectorLayerCache* mVectorLayerCache;
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";

QString myDataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
QString myTestDataDir = myDataDir + QDir::separator();

foreach( QString f, backupFiles )
{
QTemporaryFile* tmpFile = new QTemporaryFile();
tmpFile->open();
QString tmpFileName = tmpFile->fileName();
tmpFile->remove();

QString origFileName = myTestDataDir + f;

qDebug() << "Copy " << origFileName << " " << tmpFileName;

qDebug() << QFile::copy( origFileName, tmpFileName );
mTmpFiles.insert( origFileName, tmpFileName );
}

//
// load a vector layer
//
QString myPointsFileName = myTestDataDir + "points.shp";
QFileInfo myPointFileInfo( myPointsFileName );
mPointsLayer = new QgsVectorLayer( myPointFileInfo.filePath(),
myPointFileInfo.completeBaseName(), "ogr" );

mVectorLayerCache = new QgsVectorLayerCache( mPointsLayer, 10 );
}

//runs after all tests
void TestVectorLayerCache::cleanupTestCase()
{
// Clean added features
if ( mAddedFeatures.length() > 0 )
{
mPointsLayer->startEditing();
foreach( QgsFeature f, mAddedFeatures )
{
mPointsLayer->deleteFeature( f.id() );
}
mPointsLayer->commitChanges();
mAddedFeatures.clear();
}

delete mVectorLayerCache;
mVectorLayerCache = NULL;

delete mPointsLayer;
mPointsLayer = NULL;

// Clean tmp files
QMap<QString, QString>::const_iterator it;

for( it = mTmpFiles.constBegin(); it != mTmpFiles.constEnd(); ++it )
{
QString tmpFileName = it.value();
QString origFileName = it.key();

qDebug() << "Copy " << tmpFileName << " " << origFileName;
QFile( origFileName ).remove();
qDebug() << QFile::copy( tmpFileName, origFileName );
}
}

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( QgsFeatureRequest() );

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->pendingFields();
int idx = allFields.indexFromName( "newAttr" );

mPointsLayer->startEditing();
mPointsLayer->deleteAttribute( idx );
mPointsLayer->commitChanges();

QVERIFY( mVectorLayerCache->featureAtId( 15, f ) );
QVERIFY( false == 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 ) );
mPointsLayer->commitChanges();

QVERIFY( false == mVectorLayerCache->featureAtId( fid, f ) );
}

void TestVectorLayerCache::onCommittedFeaturesAdded( QString layerId, QgsFeatureList features )
{
Q_UNUSED ( layerId )
mAddedFeatures.append( features );
}

QTEST_MAIN( TestVectorLayerCache )
#include "moc_testqgsvectorlayercache.cxx"

0 comments on commit 9154ed1

Please sign in to comment.