@@ -52,8 +52,8 @@ extern "C"
5252class TestQgsGrassFeature : public QgsFeature
5353{
5454 public:
55- TestQgsGrassFeature () : grassType( 0 ) {}
56- TestQgsGrassFeature ( int type ) : grassType( type ) {}
55+ TestQgsGrassFeature () : grassType( 0 ) { setValid ( true ); }
56+ TestQgsGrassFeature ( int type ) : grassType( type ) { setValid ( true ); }
5757
5858 int grassType;
5959};
@@ -78,11 +78,13 @@ class TestQgsGrassCommand
7878 RedoAll
7979 };
8080
81- TestQgsGrassCommand () : command( AddFeature ), fid( 0 ), geometry( 0 ) {}
82- TestQgsGrassCommand ( Command c ) : command( c ), fid( 0 ), geometry( 0 ) {}
81+ TestQgsGrassCommand () : command( AddFeature ), verify( true ), fid( 0 ), geometry( 0 ) {}
82+ TestQgsGrassCommand ( Command c ) : command( c ), verify( true ), fid( 0 ), geometry( 0 ) {}
8383
8484 QString toString () const ;
8585 Command command;
86+ // some commands (in case of multiple commands making single change) must not be verified
87+ bool verify;
8688
8789 QList<TestQgsGrassFeature> grassFeatures;
8890 QgsFeature expectedFeature; // simple feature for verification
@@ -115,7 +117,18 @@ QString TestQgsGrassCommand::toString() const
115117 else if ( command == AddFeature )
116118 {
117119 string += " AddFeature " ;
118- string += expectedFeature.constGeometry ()->exportToWkt ( 1 );
120+ Q_FOREACH ( const TestQgsGrassFeature & grassFeature, grassFeatures )
121+ {
122+ if ( grassFeature.constGeometry () )
123+ {
124+ string += " <br>grass: " + grassFeature.constGeometry ()->exportToWkt ( 1 );
125+ }
126+ }
127+
128+ if ( expectedFeature.constGeometry () )
129+ {
130+ string += " <br>expected: " + expectedFeature.constGeometry ()->exportToWkt ( 1 );
131+ }
119132 }
120133 else if ( command == DeleteFeature )
121134 {
@@ -889,6 +902,7 @@ QList< TestQgsGrassCommandGroup > TestQgsGrassProvider::createCommands()
889902 TestQgsGrassCommandGroup commandGroup;
890903 TestQgsGrassCommand command;
891904 TestQgsGrassFeature grassFeature;
905+ QgsLineStringV2 * line;
892906 QgsGeometry *geometry;
893907 QList<QgsPointV2> pointList;
894908
@@ -991,7 +1005,7 @@ QList< TestQgsGrassCommandGroup > TestQgsGrassProvider::createCommands()
9911005 command = TestQgsGrassCommand ( TestQgsGrassCommand::AddFeature );
9921006 grassFeature = TestQgsGrassFeature ( GV_LINE );
9931007 grassFeature.setFeatureId ( 1 );
994- QgsLineStringV2 * line = new QgsLineStringV2 ();
1008+ line = new QgsLineStringV2 ();
9951009 pointList.clear ();
9961010 pointList << QgsPointV2 ( QgsWKBTypes::Point, 0 , 0 , 0 );
9971011 pointList << QgsPointV2 ( QgsWKBTypes::Point, 20 , 10 , 0 );
@@ -1010,6 +1024,71 @@ QList< TestQgsGrassCommandGroup > TestQgsGrassProvider::createCommands()
10101024
10111025 commandGroups << commandGroup;
10121026
1027+ // ------------------------ Group 3 (issue #13739) -------------------------------
1028+ // TODO: resolve issue and enable test
1029+ #if 0
1030+ commandGroup = TestQgsGrassCommandGroup();
1031+
1032+ // Start editing
1033+ command = TestQgsGrassCommand( TestQgsGrassCommand::StartEditing );
1034+ command.values["grassLayerCode"] = "1_line";
1035+ command.values["expectedLayerType"] = "LineString";
1036+ commandGroup.commands << command;
1037+
1038+ // Add field
1039+ command = TestQgsGrassCommand( TestQgsGrassCommand::AddAttribute );
1040+ command.field = QgsField( "field_int", QVariant::Int, "integer" );
1041+ commandGroup.commands << command;
1042+
1043+ // Add grass boundary feature without attributes and expected line feature with attributes
1044+ command = TestQgsGrassCommand( TestQgsGrassCommand::AddFeature );
1045+ command.verify = false;
1046+ grassFeature = TestQgsGrassFeature( GV_BOUNDARY );
1047+ grassFeature.setFeatureId( 1 );
1048+ line = new QgsLineStringV2();
1049+ pointList.clear();
1050+ pointList << QgsPointV2( QgsWKBTypes::Point, 0, 0, 0 );
1051+ pointList << QgsPointV2( QgsWKBTypes::Point, 20, 10, 0 );
1052+ line->setPoints( pointList );
1053+ pointList.clear();
1054+ geometry = new QgsGeometry( line );
1055+ grassFeature.setGeometry( geometry );
1056+ command.grassFeatures << grassFeature;
1057+ commandGroup.commands << command;
1058+
1059+ command = TestQgsGrassCommand( TestQgsGrassCommand::AddFeature );
1060+ command.expectedFeature = grassFeature;
1061+ command.attributes["field_int"] = 123;
1062+ command.verify = false;
1063+ commandGroup.commands << command;
1064+
1065+ // Commit
1066+ command = TestQgsGrassCommand( TestQgsGrassCommand::CommitChanges );
1067+ command.verify = false;
1068+ commandGroup.commands << command;
1069+
1070+ // Restart editing
1071+ command = TestQgsGrassCommand( TestQgsGrassCommand::StartEditing );
1072+ command.values["grassLayerCode"] = "1_line";
1073+ command.values["expectedLayerType"] = "LineString";
1074+ command.verify = false;
1075+ commandGroup.commands << command;
1076+
1077+ // Change attribute
1078+ command = TestQgsGrassCommand( TestQgsGrassCommand::ChangeAttributeValue );
1079+ // hack: it will only change attribute in GRASS layer, the feature in expected layer
1080+ // with this fid does not exist and changeAttributeValue() on non existing fid does not fail
1081+ command.fid = 1000000000;
1082+ command.field.setName( "field_int" );
1083+ command.value = 123;
1084+ commandGroup.commands << command;
1085+
1086+ // Roll back
1087+ command = TestQgsGrassCommand( TestQgsGrassCommand::RollBack );
1088+ commandGroup.commands << command;
1089+
1090+ commandGroups << commandGroup;
1091+ #endif
10131092 return commandGroups;
10141093}
10151094
@@ -1136,12 +1215,16 @@ void TestQgsGrassProvider::edit()
11361215 grassLayer->commitChanges ();
11371216 expectedLayer->commitChanges ();
11381217 editCommands.clear ();
1218+ commandGroup.fids .clear ();
1219+ commandGroup.expectedFids .clear ();
11391220 }
11401221 else if ( command.command == TestQgsGrassCommand::RollBack )
11411222 {
11421223 grassLayer->rollBack ();
11431224 expectedLayer->rollBack ();
11441225 editCommands.clear ();
1226+ commandGroup.fids .clear ();
1227+ commandGroup.expectedFids .clear ();
11451228 }
11461229 else if ( command.command == TestQgsGrassCommand::AddFeature )
11471230 {
@@ -1167,20 +1250,25 @@ void TestQgsGrassProvider::edit()
11671250 }
11681251
11691252 QgsFeature expectedFeature = command.expectedFeature ;
1170- QgsFeatureId expectedFid = expectedFeature.id ();
1171- expectedFeature.setFields ( expectedLayer->fields () );
1172- expectedFeature.initAttributes ( expectedLayer->fields ().size () );
1173- if ( !setAttributes ( expectedFeature, command.attributes ) )
1253+ // some features (e.g. boundaries for future line features) are added only to grass layer
1254+ // in that case expectedFeature is invalid
1255+ if ( expectedFeature.isValid () )
11741256 {
1175- commandOk = false ;
1176- break ;
1177- }
1178- if ( !expectedLayer->addFeature ( expectedFeature ) )
1179- {
1180- reportRow ( " cannot add expectedFeature" );
1181- commandOk = false ;
1257+ QgsFeatureId expectedFid = expectedFeature.id ();
1258+ expectedFeature.setFields ( expectedLayer->fields () );
1259+ expectedFeature.initAttributes ( expectedLayer->fields ().size () );
1260+ if ( !setAttributes ( expectedFeature, command.attributes ) )
1261+ {
1262+ commandOk = false ;
1263+ break ;
1264+ }
1265+ if ( !expectedLayer->addFeature ( expectedFeature ) )
1266+ {
1267+ reportRow ( " cannot add expectedFeature" );
1268+ commandOk = false ;
1269+ }
1270+ commandGroup.expectedFids .insert ( expectedFid, expectedFeature.id () );
11821271 }
1183- commandGroup.expectedFids .insert ( expectedFid, expectedFeature.id () );
11841272 editCommands << command;
11851273 }
11861274 else if ( command.command == TestQgsGrassCommand::DeleteFeature )
@@ -1201,13 +1289,21 @@ void TestQgsGrassProvider::edit()
12011289 }
12021290 else if ( command.command == TestQgsGrassCommand::ChangeGeometry )
12031291 {
1204- QgsFeatureId fid = commandGroup.fids .value ( command.fid );
1292+ QgsFeatureId fid = command.fid ;
1293+ if ( commandGroup.fids .contains ( fid ) )
1294+ {
1295+ fid = commandGroup.fids .value ( fid );
1296+ }
12051297 if ( !grassLayer->changeGeometry ( fid, command.geometry ) )
12061298 {
12071299 reportRow ( " cannot change feature geometry" );
12081300 commandOk = false ;
12091301 }
1210- QgsFeatureId expectedFid = commandGroup.expectedFids .value ( command.fid );
1302+ QgsFeatureId expectedFid = command.fid ;
1303+ if ( commandGroup.expectedFids .contains ( expectedFid ) )
1304+ {
1305+ expectedFid = commandGroup.expectedFids .value ( expectedFid );
1306+ }
12111307 if ( !expectedLayer->changeGeometry ( expectedFid, command.geometry ) )
12121308 {
12131309 reportRow ( " cannot change expected feature geometry" );
@@ -1237,8 +1333,8 @@ void TestQgsGrassProvider::edit()
12371333 reportRow ( " cannot delete field from layer" );
12381334 commandOk = false ;
12391335 }
1240- QgsFeatureId expectedFid = commandGroup. expectedFids . value ( command.fid );
1241- if ( !expectedLayer->deleteAttribute ( expectedFid ) )
1336+ int expectedIndex = expectedLayer-> fields (). indexFromName ( command.field . name () );
1337+ if ( !expectedLayer->deleteAttribute ( expectedIndex ) )
12421338 {
12431339 reportRow ( " cannot delete field from expected layer" );
12441340 commandOk = false ;
@@ -1247,14 +1343,22 @@ void TestQgsGrassProvider::edit()
12471343 }
12481344 else if ( command.command == TestQgsGrassCommand::ChangeAttributeValue )
12491345 {
1250- QgsFeatureId fid = commandGroup.fids .value ( command.fid );
1346+ QgsFeatureId fid = command.fid ;
1347+ if ( commandGroup.fids .contains ( fid ) )
1348+ {
1349+ fid = commandGroup.fids .value ( fid );
1350+ }
12511351 int index = grassLayer->fields ().indexFromName ( command.field .name () );
12521352 if ( !grassLayer->changeAttributeValue ( fid, index, command.value ) )
12531353 {
12541354 reportRow ( " cannot change feature attribute" );
12551355 commandOk = false ;
12561356 }
1257- QgsFeatureId expectedFid = commandGroup.expectedFids .value ( command.fid );
1357+ QgsFeatureId expectedFid = command.fid ;
1358+ if ( commandGroup.expectedFids .contains ( expectedFid ) )
1359+ {
1360+ expectedFid = commandGroup.expectedFids .value ( expectedFid );
1361+ }
12581362 int expectedIndex = expectedLayer->fields ().indexFromName ( command.field .name () );
12591363 if ( !expectedLayer->changeAttributeValue ( expectedFid, expectedIndex, command.value ) )
12601364 {
@@ -1329,6 +1433,12 @@ void TestQgsGrassProvider::edit()
13291433 break ;
13301434 }
13311435
1436+ if ( !command.verify )
1437+ {
1438+ reportRow ( " partial command not verified" );
1439+ continue ;
1440+ }
1441+
13321442 if ( command.command != TestQgsGrassCommand::UndoAll && command.command != TestQgsGrassCommand::RedoAll )
13331443 {
13341444 if ( !compare ( expectedLayers, ok ) )
0 commit comments