@@ -41,6 +41,7 @@ class TestQgsOfflineEditing : public QObject
41
41
QStringList layerIds;
42
42
long numberOfFeatures;
43
43
int numberOfFields;
44
+ QTemporaryDir tempDir;
44
45
45
46
private slots:
46
47
void initTestCase ();// will be called before the first testfunction is executed.
@@ -75,8 +76,12 @@ void TestQgsOfflineEditing::cleanupTestCase()
75
76
void TestQgsOfflineEditing::init ()
76
77
{
77
78
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 );
80
85
mpLayer = new QgsVectorLayer ( myMapFileInfo.filePath (),
81
86
myMapFileInfo.completeBaseName (), QStringLiteral ( " ogr" ) );
82
87
QgsProject::instance ()->addMapLayer ( mpLayer );
@@ -124,6 +129,9 @@ void TestQgsOfflineEditing::createGeopackageAndSynchronizeBack()
124
129
QCOMPARE ( mpLayer->name (), QStringLiteral ( " points" ) );
125
130
QCOMPARE ( mpLayer->featureCount (), numberOfFeatures );
126
131
QCOMPARE ( mpLayer->fields ().size (), numberOfFields );
132
+ QgsFeature firstFeatureBeforeAction;
133
+ QgsFeatureIterator it = mpLayer->getFeatures ();
134
+ it.nextFeature ( firstFeatureBeforeAction );
127
135
128
136
connect ( mOfflineEditing , &QgsOfflineEditing::warning, this , []( const QString & title, const QString & message ) { qDebug () << title << message; } );
129
137
// convert
@@ -135,13 +143,45 @@ void TestQgsOfflineEditing::createGeopackageAndSynchronizeBack()
135
143
// comparing with the number +1 because GPKG created an fid
136
144
QCOMPARE ( mpLayer->fields ().size (), numberOfFields + 1 );
137
145
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
+
138
162
// synchronize back
139
163
mOfflineEditing ->synchronize ();
140
164
141
165
mpLayer = qobject_cast<QgsVectorLayer *>( QgsProject::instance ()->mapLayers ().first () );
142
166
QCOMPARE ( mpLayer->name (), QStringLiteral ( " points" ) );
143
- QCOMPARE ( mpLayer->featureCount (), numberOfFeatures );
167
+ QCOMPARE ( mpLayer->dataProvider ()-> featureCount (), numberOfFeatures + 1 );
144
168
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 );
145
185
}
146
186
147
187
QGSTEST_MAIN ( TestQgsOfflineEditing )
0 commit comments