@@ -92,6 +92,8 @@ class TestQgsGrassCommand
92
92
QVariant value;
93
93
94
94
QMap<QString, QVariant> values;
95
+ // map of attributes by name
96
+ QMap<QString, QVariant> attributes;
95
97
};
96
98
97
99
QString TestQgsGrassCommand::toString () const
@@ -206,6 +208,7 @@ class TestQgsGrassProvider: public QObject
206
208
bool compare ( QString uri, QgsVectorLayer *expectedLayer, bool & ok );
207
209
QList< TestQgsGrassCommandGroup > createCommands ();
208
210
QList<QgsFeature> getFeatures ( QgsVectorLayer *layer );
211
+ bool setAttributes ( QgsFeature & feature, const QMap<QString, QVariant> &attributes );
209
212
QString mGisdbase ;
210
213
QString mLocation ;
211
214
QString mReport ;
@@ -887,6 +890,7 @@ QList< TestQgsGrassCommandGroup > TestQgsGrassProvider::createCommands()
887
890
TestQgsGrassCommand command;
888
891
TestQgsGrassFeature grassFeature;
889
892
QgsGeometry *geometry;
893
+ QList<QgsPointV2> pointList;
890
894
891
895
// Start editing
892
896
command = TestQgsGrassCommand ( TestQgsGrassCommand::StartEditing );
@@ -969,9 +973,65 @@ QList< TestQgsGrassCommandGroup > TestQgsGrassProvider::createCommands()
969
973
970
974
commandGroups << commandGroup;
971
975
976
+ // ------------------------ Group 2 (issue #13726) -------------------------------
977
+ commandGroup = TestQgsGrassCommandGroup ();
978
+
979
+ // Start editing
980
+ command = TestQgsGrassCommand ( TestQgsGrassCommand::StartEditing );
981
+ command.values [" grassLayerCode" ] = " 1_line" ;
982
+ command.values [" expectedLayerType" ] = " LineString" ;
983
+ commandGroup.commands << command;
984
+
985
+ // Add field
986
+ command = TestQgsGrassCommand ( TestQgsGrassCommand::AddAttribute );
987
+ command.field = QgsField ( " field_int" , QVariant::Int, " integer" );
988
+ commandGroup.commands << command;
989
+
990
+ // Add line feature with attributes
991
+ command = TestQgsGrassCommand ( TestQgsGrassCommand::AddFeature );
992
+ grassFeature = TestQgsGrassFeature ( GV_LINE );
993
+ grassFeature.setFeatureId ( 1 );
994
+ QgsLineStringV2 * line = new QgsLineStringV2 ();
995
+ pointList.clear ();
996
+ pointList << QgsPointV2 ( QgsWKBTypes::Point, 0 , 0 , 0 );
997
+ pointList << QgsPointV2 ( QgsWKBTypes::Point, 20 , 10 , 0 );
998
+ line->setPoints ( pointList );
999
+ pointList.clear ();
1000
+ geometry = new QgsGeometry ( line );
1001
+ grassFeature.setGeometry ( geometry );
1002
+ command.grassFeatures << grassFeature;
1003
+ command.expectedFeature = grassFeature;
1004
+ command.attributes [" field_int" ] = 456 ;
1005
+ commandGroup.commands << command;
1006
+
1007
+ // Commit
1008
+ command = TestQgsGrassCommand ( TestQgsGrassCommand::CommitChanges );
1009
+ commandGroup.commands << command;
1010
+
1011
+ commandGroups << commandGroup;
1012
+
972
1013
return commandGroups;
973
1014
}
974
1015
1016
+ bool TestQgsGrassProvider::setAttributes ( QgsFeature & feature, const QMap<QString, QVariant> &attributes )
1017
+ {
1018
+ bool attributesSet = true ;
1019
+ Q_FOREACH ( const QString fieldName, attributes.keys () )
1020
+ {
1021
+ int index = feature.fields ()->indexFromName ( fieldName );
1022
+ if ( index < 0 )
1023
+ {
1024
+ attributesSet = false ;
1025
+ reportRow ( " cannot find index of attribute " + fieldName );
1026
+ }
1027
+ else
1028
+ {
1029
+ feature.setAttribute ( index, attributes.value ( fieldName ) );
1030
+ }
1031
+ }
1032
+ return attributesSet;
1033
+ }
1034
+
975
1035
void TestQgsGrassProvider::edit ()
976
1036
{
977
1037
reportHeader ( " TestQgsGrassProvider::edit" );
@@ -1089,7 +1149,14 @@ void TestQgsGrassProvider::edit()
1089
1149
{
1090
1150
QgsFeatureId fid = grassFeature.id ();
1091
1151
grassProvider->setNewFeatureType ( grassFeature.grassType );
1152
+ grassFeature.setFields ( grassLayer->fields () );
1092
1153
grassFeature.initAttributes ( grassLayer->fields ().size () ); // attributes must match layer fields
1154
+ if ( !setAttributes ( grassFeature, command.attributes ) )
1155
+ {
1156
+ commandOk = false ;
1157
+ break ;
1158
+ }
1159
+
1093
1160
if ( !grassLayer->addFeature ( grassFeature ) )
1094
1161
{
1095
1162
reportRow ( " cannot add feature" );
@@ -1101,8 +1168,13 @@ void TestQgsGrassProvider::edit()
1101
1168
1102
1169
QgsFeature expectedFeature = command.expectedFeature ;
1103
1170
QgsFeatureId expectedFid = expectedFeature.id ();
1171
+ expectedFeature.setFields ( expectedLayer->fields () );
1104
1172
expectedFeature.initAttributes ( expectedLayer->fields ().size () );
1105
- // expectedFeature.setGeometry( new QgsGeometry( new QgsPointV2( QgsWKBTypes::Point, 10, 20, 0 ) ) ); // debug
1173
+ if ( !setAttributes ( expectedFeature, command.attributes ) )
1174
+ {
1175
+ commandOk = false ;
1176
+ break ;
1177
+ }
1106
1178
if ( !expectedLayer->addFeature ( expectedFeature ) )
1107
1179
{
1108
1180
reportRow ( " cannot add expectedFeature" );
0 commit comments