Skip to content

Commit

Permalink
Add test coverage for string and number lists, fix escape
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn authored and nyalldawson committed May 10, 2021
1 parent bb44ff6 commit 2eb46e1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/core/qgsofflineediting.cpp
Expand Up @@ -1037,7 +1037,7 @@ void QgsOfflineEditing::applyFeaturesAdded( QgsVectorLayer *offlineLayer, QgsVec
QVariant attr = attrs.at( it );
if ( remoteLayer->fields().at( remoteAttributeIndex ).type() == QVariant::StringList )
{
attr = attr.toString().split( ',' );
attr = convertStringToStringList( attr.toString() );
}
else if ( remoteLayer->fields().at( remoteAttributeIndex ).type() == QVariant::List )
{
Expand All @@ -1056,7 +1056,12 @@ void QgsOfflineEditing::applyFeaturesAdded( QgsVectorLayer *offlineLayer, QgsVec

QStringList QgsOfflineEditing::convertStringToStringList( const QString &string )
{
return string.split( QRegularExpression( "(?<!\\\\)\\s*,\\s*" ) );
QStringList stringList = string.split( QRegularExpression( "(?<!\\\\)\\s*,\\s*" ) );
for ( QString &string : stringList )
{
string.replace( QStringLiteral( "\\," ), QStringLiteral( "," ) );
}
return stringList;
}

QString QgsOfflineEditing::convertStringListToString( const QStringList &stringList )
Expand All @@ -1066,7 +1071,7 @@ QString QgsOfflineEditing::convertStringListToString( const QStringList &stringL
{
string.replace( QStringLiteral( "," ), QStringLiteral( "\\," ) );
}
return modifiedStringList.join( QStringLiteral( " , " ) );
return modifiedStringList.join( QStringLiteral( "," ) );
}

QVariantList QgsOfflineEditing::convertStringToList( const QString &string, QVariant::Type type )
Expand Down
15 changes: 11 additions & 4 deletions tests/src/core/testqgsofflineediting.cpp
Expand Up @@ -77,17 +77,16 @@ void TestQgsOfflineEditing::initTestCase()

void TestQgsOfflineEditing::cleanupTestCase()
{
delete mOfflineEditing;
QgsApplication::exitQgis();
}

void TestQgsOfflineEditing::init()
{
QString myFileName( TEST_DATA_DIR ); //defined in CmakeLists.txt
QString myTempDirName = tempDir.path();
QFile::copy( myFileName + "/points.shp", myTempDirName + "/points.shp" );
QFile::copy( myFileName + "/points.shx", myTempDirName + "/points.shx" );
QFile::copy( myFileName + "/points.dbf", myTempDirName + "/points.dbf" );
QString myTempFileName = myTempDirName + "/points.shp";
QFile::copy( myFileName + "/points.geojson", myTempDirName + "/points.geojson" );
QString myTempFileName = myTempDirName + "/points.geojson";
QFileInfo myMapFileInfo( myTempFileName );
mpLayer = new QgsVectorLayer( myMapFileInfo.filePath(),
myMapFileInfo.completeBaseName(), QStringLiteral( "ogr" ) );
Expand Down Expand Up @@ -236,8 +235,14 @@ void TestQgsOfflineEditing::createGeopackageAndSynchronizeBack()
QCOMPARE( firstFeatureInAction.attribute( QStringLiteral( "Heading" ) ).toString(), firstFeatureBeforeAction.attribute( QStringLiteral( "Heading" ) ).toString() );
QCOMPARE( firstFeatureInAction.attribute( QStringLiteral( "Cabin Crew" ) ).toString(), firstFeatureBeforeAction.attribute( QStringLiteral( "Cabin Crew" ) ).toString() );

//check converted lists values
QCOMPARE( firstFeatureInAction.attribute( QStringLiteral( "StaffNames" ) ), QStringLiteral( "Bob,Alice" ) );
QCOMPARE( firstFeatureInAction.attribute( QStringLiteral( "StaffAges" ) ), QStringLiteral( "22,33" ) );

QgsFeature newFeature( mpLayer->dataProvider()->fields() );
newFeature.setAttribute( QStringLiteral( "Class" ), QStringLiteral( "Superjet" ) );
newFeature.setAttribute( QStringLiteral( "StaffNames" ), QStringLiteral( "Sebastien, Naomi, And\\, many\\, more" ) );
newFeature.setAttribute( QStringLiteral( "StaffAges" ), QStringLiteral( "0,2" ) );
mpLayer->startEditing();
mpLayer->addFeature( newFeature );
mpLayer->commitChanges();
Expand All @@ -261,6 +266,8 @@ void TestQgsOfflineEditing::createGeopackageAndSynchronizeBack()
QgsFeature f = mpLayer->getFeature( mpLayer->dataProvider()->featureCount() - 1 );
qDebug() << "FID:" << f.id() << "Class:" << f.attribute( "Class" ).toString();
QCOMPARE( f.attribute( QStringLiteral( "Class" ) ).toString(), QStringLiteral( "Superjet" ) );
QCOMPARE( f.attribute( QStringLiteral( "StaffNames" ) ).toStringList(), QStringList() << QStringLiteral( "Sebastien" ) << QStringLiteral( "Naomi" ) << QStringLiteral( "And, many, more" ) );
QCOMPARE( f.attribute( QStringLiteral( "StaffAges" ) ).toList(), QList<QVariant>() << 0 << 2 );

QgsFeature firstFeatureAfterAction;
it = mpLayer->getFeatures();
Expand Down

0 comments on commit 2eb46e1

Please sign in to comment.