Skip to content

Commit a3a595b

Browse files
committedDec 11, 2018
tests for gpkg synchronization
(cherry-picked from be9f6bb 73e3ead 8fda2b7)
1 parent e1c4419 commit a3a595b

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed
 

‎tests/src/core/testqgsofflineediting.cpp

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class TestQgsOfflineEditing : public QObject
4141
QStringList layerIds;
4242
long numberOfFeatures;
4343
int numberOfFields;
44+
QTemporaryDir tempDir;
4445

4546
private slots:
4647
void initTestCase();// will be called before the first testfunction is executed.
@@ -75,8 +76,12 @@ void TestQgsOfflineEditing::cleanupTestCase()
7576
void TestQgsOfflineEditing::init()
7677
{
7778
QString myFileName( TEST_DATA_DIR ); //defined in CmakeLists.txt
78-
myFileName = myFileName + "/points.shp";
79-
QFileInfo myMapFileInfo( myFileName );
79+
QString myTempDirName = tempDir.path();
80+
QFile::copy( myFileName + "/points.shp", myTempDirName + "/points.shp" );
81+
QFile::copy( myFileName + "/points.shx", myTempDirName + "/points.shx" );
82+
QFile::copy( myFileName + "/points.dbf", myTempDirName + "/points.dbf" );
83+
QString myTempFileName = myTempDirName + "/points.shp";
84+
QFileInfo myMapFileInfo( myTempFileName );
8085
mpLayer = new QgsVectorLayer( myMapFileInfo.filePath(),
8186
myMapFileInfo.completeBaseName(), QStringLiteral( "ogr" ) );
8287
QgsProject::instance()->addMapLayer( mpLayer );
@@ -124,6 +129,9 @@ void TestQgsOfflineEditing::createGeopackageAndSynchronizeBack()
124129
QCOMPARE( mpLayer->name(), QStringLiteral( "points" ) );
125130
QCOMPARE( mpLayer->featureCount(), numberOfFeatures );
126131
QCOMPARE( mpLayer->fields().size(), numberOfFields );
132+
QgsFeature firstFeatureBeforeAction;
133+
QgsFeatureIterator it = mpLayer->getFeatures();
134+
it.nextFeature( firstFeatureBeforeAction );
127135

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

146+
QgsFeature firstFeatureInAction;
147+
it = mpLayer->getFeatures();
148+
it.nextFeature( firstFeatureInAction );
149+
150+
//compare some values
151+
QCOMPARE( firstFeatureInAction.attribute( QStringLiteral( "Class" ) ).toString(), firstFeatureBeforeAction.attribute( QStringLiteral( "Class" ) ).toString() );
152+
QCOMPARE( firstFeatureInAction.attribute( QStringLiteral( "Heading" ) ).toString(), firstFeatureBeforeAction.attribute( QStringLiteral( "Heading" ) ).toString() );
153+
QCOMPARE( firstFeatureInAction.attribute( QStringLiteral( "Cabin Crew" ) ).toString(), firstFeatureBeforeAction.attribute( QStringLiteral( "Cabin Crew" ) ).toString() );
154+
155+
QgsFeature newFeature( mpLayer->dataProvider()->fields() );
156+
newFeature.setAttribute( QStringLiteral( "Class" ), QStringLiteral( "Superjet" ) );
157+
mpLayer->startEditing();
158+
mpLayer->addFeature( newFeature );
159+
mpLayer->commitChanges();
160+
QCOMPARE( mpLayer->featureCount(), numberOfFeatures + 1 );
161+
138162
//synchronize back
139163
mOfflineEditing->synchronize();
140164

141165
mpLayer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayers().first() );
142166
QCOMPARE( mpLayer->name(), QStringLiteral( "points" ) );
143-
QCOMPARE( mpLayer->featureCount(), numberOfFeatures );
167+
QCOMPARE( mpLayer->dataProvider()->featureCount(), numberOfFeatures + 1 );
144168
QCOMPARE( mpLayer->fields().size(), numberOfFields );
169+
//get last feature
170+
QgsFeature f = mpLayer->getFeature( mpLayer->dataProvider()->featureCount() - 1 );
171+
qDebug() << "FID:" << f.id() << "Class:" << f.attribute( "Class" ).toString();
172+
QCOMPARE( f.attribute( QStringLiteral( "Class" ) ).toString(), QStringLiteral( "Superjet" ) );
173+
174+
QgsFeature firstFeatureAfterAction;
175+
it = mpLayer->getFeatures();
176+
it.nextFeature( firstFeatureAfterAction );
177+
178+
QCOMPARE( firstFeatureAfterAction, firstFeatureBeforeAction );
179+
180+
//and delete the feature again
181+
QgsFeatureIds idsToClean;
182+
idsToClean << f.id();
183+
mpLayer->dataProvider()->deleteFeatures( idsToClean );
184+
QCOMPARE( mpLayer->dataProvider()->featureCount(), numberOfFeatures );
145185
}
146186

147187
QGSTEST_MAIN( TestQgsOfflineEditing )

0 commit comments

Comments
 (0)
Please sign in to comment.