diff --git a/tests/src/core/testqgsofflineediting.cpp b/tests/src/core/testqgsofflineediting.cpp index c8d8e361fe48..8e76281e5fb0 100644 --- a/tests/src/core/testqgsofflineediting.cpp +++ b/tests/src/core/testqgsofflineediting.cpp @@ -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. @@ -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 ); @@ -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 @@ -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( 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 )