Skip to content
Permalink
Browse files

Fixes #36114 : NULL value relation sort

  • Loading branch information
troopa81 authored and nyalldawson committed May 27, 2020
1 parent 834c5ba commit f1a5c5d782b91a6c94315e38d2797bef1962c07d
@@ -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
@@ -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;
@@ -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.
You can’t perform that action at this time.