Skip to content

Commit 5f9611e

Browse files
committed
[GRASS] add test for #13726 (new line attributes lost)
1 parent 968e55c commit 5f9611e

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed

tests/src/providers/grass/testqgsgrassprovider.cpp

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ class TestQgsGrassCommand
9292
QVariant value;
9393

9494
QMap<QString, QVariant> values;
95+
// map of attributes by name
96+
QMap<QString, QVariant> attributes;
9597
};
9698

9799
QString TestQgsGrassCommand::toString() const
@@ -206,6 +208,7 @@ class TestQgsGrassProvider: public QObject
206208
bool compare( QString uri, QgsVectorLayer *expectedLayer, bool& ok );
207209
QList< TestQgsGrassCommandGroup > createCommands();
208210
QList<QgsFeature> getFeatures( QgsVectorLayer *layer );
211+
bool setAttributes( QgsFeature & feature, const QMap<QString, QVariant> &attributes );
209212
QString mGisdbase;
210213
QString mLocation;
211214
QString mReport;
@@ -887,6 +890,7 @@ QList< TestQgsGrassCommandGroup > TestQgsGrassProvider::createCommands()
887890
TestQgsGrassCommand command;
888891
TestQgsGrassFeature grassFeature;
889892
QgsGeometry *geometry;
893+
QList<QgsPointV2> pointList;
890894

891895
// Start editing
892896
command = TestQgsGrassCommand( TestQgsGrassCommand::StartEditing );
@@ -969,9 +973,65 @@ QList< TestQgsGrassCommandGroup > TestQgsGrassProvider::createCommands()
969973

970974
commandGroups << commandGroup;
971975

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+
9721013
return commandGroups;
9731014
}
9741015

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+
9751035
void TestQgsGrassProvider::edit()
9761036
{
9771037
reportHeader( "TestQgsGrassProvider::edit" );
@@ -1089,7 +1149,14 @@ void TestQgsGrassProvider::edit()
10891149
{
10901150
QgsFeatureId fid = grassFeature.id();
10911151
grassProvider->setNewFeatureType( grassFeature.grassType );
1152+
grassFeature.setFields( grassLayer->fields() );
10921153
grassFeature.initAttributes( grassLayer->fields().size() ); // attributes must match layer fields
1154+
if ( !setAttributes( grassFeature, command.attributes ) )
1155+
{
1156+
commandOk = false;
1157+
break;
1158+
}
1159+
10931160
if ( !grassLayer->addFeature( grassFeature ) )
10941161
{
10951162
reportRow( "cannot add feature" );
@@ -1101,8 +1168,13 @@ void TestQgsGrassProvider::edit()
11011168

11021169
QgsFeature expectedFeature = command.expectedFeature;
11031170
QgsFeatureId expectedFid = expectedFeature.id();
1171+
expectedFeature.setFields( expectedLayer->fields() );
11041172
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+
}
11061178
if ( !expectedLayer->addFeature( expectedFeature ) )
11071179
{
11081180
reportRow( "cannot add expectedFeature" );

0 commit comments

Comments
 (0)