Skip to content

Commit

Permalink
Fixes #36114 : NULL value relation sort
Browse files Browse the repository at this point in the history
  • Loading branch information
troopa81 authored and nyalldawson committed Jun 2, 2020
1 parent 834c5ba commit f1a5c5d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/core/fieldformatter/qgsvaluerelationfieldformatter.cpp
Expand Up @@ -110,7 +110,7 @@ QString QgsValueRelationFieldFormatter::representValue( QgsVectorLayer *layer, i

QVariant QgsValueRelationFieldFormatter::sortValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
{
return representValue( layer, fieldIndex, config, cache, value );
return value.isNull() ? QString() : representValue( layer, fieldIndex, config, cache, value );
}

QVariant QgsValueRelationFieldFormatter::createCache( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config ) const
Expand Down
22 changes: 18 additions & 4 deletions tests/src/core/testqgsvaluerelationfieldformatter.cpp
Expand Up @@ -37,6 +37,7 @@ class TestQgsValueRelationFieldFormatter: public QObject
void init(); // will be called before each testfunction is executed.
void cleanup(); // will be called after every testfunction.
void testDependencies();
void testSortValueNull();

private:
std::unique_ptr<QgsVectorLayer> mLayer1;
Expand Down Expand Up @@ -146,9 +147,22 @@ void TestQgsValueRelationFieldFormatter::testDependencies()
QCOMPARE( dependency.source, mLayer2->publicSource() );
}

void TestQgsValueRelationFieldFormatter::testSortValueNull()
{
QgsValueRelationFieldFormatter formatter;
QVariantMap config;
config.insert( QStringLiteral( "Layer" ), mLayer2->id() );
config.insert( QStringLiteral( "Key" ), QStringLiteral( "pk" ) );
config.insert( QStringLiteral( "Value" ), QStringLiteral( "material" ) );

// when sorting, a null value is represented with a null QString, not "NULL" string
// if not, the NULL values will take place between M and O (see https://github.com/qgis/QGIS/issues/36114)
QVariant value = formatter.sortValue( mLayer2.get(), 1, config, QVariant(), QVariant() );
QCOMPARE( value, QVariant( QString() ) );

value = formatter.sortValue( mLayer2.get(), 1, config, QVariant(), QVariant( 10 ) );
QCOMPARE( value, QVariant( QString( "iron" ) ) );
}

QGSTEST_MAIN( TestQgsValueRelationFieldFormatter )
#include "testqgsvaluerelationfieldformatter.moc"




0 comments on commit f1a5c5d

Please sign in to comment.