Skip to content

Commit

Permalink
tests for gpkg synchronization
Browse files Browse the repository at this point in the history
(cherry-picked from be9f6bb 73e3ead 8fda2b7)
  • Loading branch information
signedav committed Dec 11, 2018
1 parent e1c4419 commit a3a595b
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions tests/src/core/testqgsofflineediting.cpp
Expand Up @@ -41,6 +41,7 @@ class TestQgsOfflineEditing : public QObject
QStringList layerIds;
long numberOfFeatures;
int numberOfFields;
QTemporaryDir tempDir;

private slots:
void initTestCase();// will be called before the first testfunction is executed.
Expand Down Expand Up @@ -75,8 +76,12 @@ void TestQgsOfflineEditing::cleanupTestCase()
void TestQgsOfflineEditing::init()
{
QString myFileName( TEST_DATA_DIR ); //defined in CmakeLists.txt
myFileName = myFileName + "/points.shp";
QFileInfo myMapFileInfo( myFileName );
QString myTempDirName = tempDir.path();
QFile::copy( myFileName + "/points.shp", myTempDirName + "/points.shp" );
QFile::copy( myFileName + "/points.shx", myTempDirName + "/points.shx" );
QFile::copy( myFileName + "/points.dbf", myTempDirName + "/points.dbf" );
QString myTempFileName = myTempDirName + "/points.shp";
QFileInfo myMapFileInfo( myTempFileName );
mpLayer = new QgsVectorLayer( myMapFileInfo.filePath(),
myMapFileInfo.completeBaseName(), QStringLiteral( "ogr" ) );
QgsProject::instance()->addMapLayer( mpLayer );
Expand Down Expand Up @@ -124,6 +129,9 @@ void TestQgsOfflineEditing::createGeopackageAndSynchronizeBack()
QCOMPARE( mpLayer->name(), QStringLiteral( "points" ) );
QCOMPARE( mpLayer->featureCount(), numberOfFeatures );
QCOMPARE( mpLayer->fields().size(), numberOfFields );
QgsFeature firstFeatureBeforeAction;
QgsFeatureIterator it = mpLayer->getFeatures();
it.nextFeature( firstFeatureBeforeAction );

connect( mOfflineEditing, &QgsOfflineEditing::warning, this, []( const QString & title, const QString & message ) { qDebug() << title << message; } );
//convert
Expand All @@ -135,13 +143,45 @@ void TestQgsOfflineEditing::createGeopackageAndSynchronizeBack()
//comparing with the number +1 because GPKG created an fid
QCOMPARE( mpLayer->fields().size(), numberOfFields + 1 );

QgsFeature firstFeatureInAction;
it = mpLayer->getFeatures();
it.nextFeature( firstFeatureInAction );

//compare some values
QCOMPARE( firstFeatureInAction.attribute( QStringLiteral( "Class" ) ).toString(), firstFeatureBeforeAction.attribute( QStringLiteral( "Class" ) ).toString() );
QCOMPARE( firstFeatureInAction.attribute( QStringLiteral( "Heading" ) ).toString(), firstFeatureBeforeAction.attribute( QStringLiteral( "Heading" ) ).toString() );
QCOMPARE( firstFeatureInAction.attribute( QStringLiteral( "Cabin Crew" ) ).toString(), firstFeatureBeforeAction.attribute( QStringLiteral( "Cabin Crew" ) ).toString() );

QgsFeature newFeature( mpLayer->dataProvider()->fields() );
newFeature.setAttribute( QStringLiteral( "Class" ), QStringLiteral( "Superjet" ) );
mpLayer->startEditing();
mpLayer->addFeature( newFeature );
mpLayer->commitChanges();
QCOMPARE( mpLayer->featureCount(), numberOfFeatures + 1 );

//synchronize back
mOfflineEditing->synchronize();

mpLayer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayers().first() );
QCOMPARE( mpLayer->name(), QStringLiteral( "points" ) );
QCOMPARE( mpLayer->featureCount(), numberOfFeatures );
QCOMPARE( mpLayer->dataProvider()->featureCount(), numberOfFeatures + 1 );
QCOMPARE( mpLayer->fields().size(), numberOfFields );
//get last feature
QgsFeature f = mpLayer->getFeature( mpLayer->dataProvider()->featureCount() - 1 );
qDebug() << "FID:" << f.id() << "Class:" << f.attribute( "Class" ).toString();
QCOMPARE( f.attribute( QStringLiteral( "Class" ) ).toString(), QStringLiteral( "Superjet" ) );

QgsFeature firstFeatureAfterAction;
it = mpLayer->getFeatures();
it.nextFeature( firstFeatureAfterAction );

QCOMPARE( firstFeatureAfterAction, firstFeatureBeforeAction );

//and delete the feature again
QgsFeatureIds idsToClean;
idsToClean << f.id();
mpLayer->dataProvider()->deleteFeatures( idsToClean );
QCOMPARE( mpLayer->dataProvider()->featureCount(), numberOfFeatures );
}

QGSTEST_MAIN( TestQgsOfflineEditing )
Expand Down

0 comments on commit a3a595b

Please sign in to comment.