Skip to content
Permalink
Browse files
Fixes #33108 : Fix displayed array list in query builder
  • Loading branch information
troopa81 authored and nyalldawson committed Jun 21, 2021
1 parent 9c87980 commit aca1a9dc10c266af124b6c851502bb71d86e3ccd
Showing with 131 additions and 0 deletions.
  1. +10 −0 src/gui/qgsquerybuilder.cpp
  2. +5 −0 src/gui/qgsquerybuilder.h
  3. +1 −0 tests/src/gui/CMakeLists.txt
  4. +115 −0 tests/src/gui/testqgsquerybuilder.cpp
@@ -172,6 +172,16 @@ void QgsQueryBuilder::fillValues( int idx, int limit )
value = nullValue;
else if ( var.type() == QVariant::Date && mLayer->providerType() == QLatin1String( "ogr" ) && mLayer->storageType() == QLatin1String( "ESRI Shapefile" ) )
value = var.toDate().toString( QStringLiteral( "yyyy/MM/dd" ) );
else if ( var.type() == QVariant::List || var.type() == QVariant::StringList )
{
const QVariantList list = var.toList();
for ( const QVariant &val : list )
{
if ( !value.isEmpty() )
value.append( ", " );
value.append( val.isNull() ? nullValue : val.toString() );
}
}
else
value = var.toString();

@@ -150,5 +150,10 @@ class GUI_EXPORT QgsQueryBuilder : public QDialog, private Ui::QgsQueryBuilderBa

//! original subset string
QString mOrigSubsetString;

//! whether to ignore subsetStringChanged() signal from the layer
bool mIgnoreLayerSubsetStringChangedSignal = false;

friend class TestQgsQueryBuilder;
};
#endif //QGSQUERYBUILDER_H
@@ -173,3 +173,4 @@ ADD_QGIS_TEST(singlebandpseudocolorrendererwidget testqgssinglebandpseudocolorre
ADD_QGIS_TEST(doublevalidator testqgsdoublevalidator.cpp)
ADD_QGIS_TEST(meshlayerpropertiesdialog testqgsmeshlayerpropertiesdialog.cpp)
ADD_QGIS_TEST(externalresourcewidgetwrapper testqgsexternalresourcewidgetwrapper.cpp)
ADD_QGIS_TEST(querybuilder testqgsquerybuilder.cpp)
@@ -0,0 +1,115 @@
/***************************************************************************
testqgsquerybuilder.cpp
---------------------------
begin : June 2021
copyright : (C) 2021 by Julien Cabieces
email : julien.cabieces@oslandia.com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/


#include "qgstest.h"

#include <qgsvectorlayer.h>
#include <qgsquerybuilder.h>

class TestQgsQueryBuilder : public QObject
{
Q_OBJECT

public:
TestQgsQueryBuilder() = default;

private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.
void init();// will be called before each testfunction is executed.
void cleanup();// will be called after every testfunction.
void testFillValues();

private:

QStringList getModelItemDisplayStrings( QStandardItemModel *model );

};

void TestQgsQueryBuilder::initTestCase() // will be called before the first testfunction is executed.
{
}

void TestQgsQueryBuilder::cleanupTestCase()
{
}

void TestQgsQueryBuilder::init()
{
}

void TestQgsQueryBuilder::cleanup()
{
}

QStringList TestQgsQueryBuilder::getModelItemDisplayStrings( QStandardItemModel *model )
{
QStringList result;
for ( int row = 0; row < model->rowCount(); row++ )
{
result << model->item( row )->text();
}

result.sort();

return result;
}


void TestQgsQueryBuilder::testFillValues()
{
QgsVectorLayer vl( QStringLiteral( "Point?field=intarray:int[]&field=strarray:string[]&field=intf:int" ), QStringLiteral( "test" ), QStringLiteral( "memory" ) );

QgsFeature feat1( vl.fields() );
feat1.setAttribute( QStringLiteral( "intarray" ), QVariantList() << 1 );
feat1.setAttribute( QStringLiteral( "strarray" ), QVariantList() << QStringLiteral( "testA" ) );
feat1.setAttribute( QStringLiteral( "intf" ), 0 );
vl.dataProvider()->addFeature( feat1 );

QgsFeature feat2( vl.fields() );
feat2.setAttribute( QStringLiteral( "intarray" ), QVariantList() << 2 << 3 );
feat2.setAttribute( QStringLiteral( "strarray" ), QVariantList() << QVariant() );
feat2.setAttribute( QStringLiteral( "intf" ), 42 );
vl.dataProvider()->addFeature( feat2 );

QgsFeature feat3( vl.fields() );
feat3.setAttribute( QStringLiteral( "intarray" ), QVariantList() << QVariant() );
feat3.setAttribute( QStringLiteral( "strarray" ), QVariantList() << QStringLiteral( "testB" ) << QStringLiteral( "testC" ) );
feat3.setAttribute( QStringLiteral( "intf" ), 42 );
vl.dataProvider()->addFeature( feat3 );

QgsFeature feat4( vl.fields() );
feat4.setAttribute( QStringLiteral( "intarray" ), QVariantList() << 4 << 5 << 6 );
feat4.setAttribute( QStringLiteral( "strarray" ), QVariantList() << QVariant() );
feat4.setAttribute( QStringLiteral( "intf" ), QVariant() );
vl.dataProvider()->addFeature( feat4 );

QgsQueryBuilder queryBuilder( &vl );

queryBuilder.fillValues( 0, 10 );
QCOMPARE( getModelItemDisplayStrings( queryBuilder.mModelValues ), QStringList() << "1" << "2, 3" << "4, 5, 6" << "NULL" );

queryBuilder.fillValues( 1, 10 );
QCOMPARE( getModelItemDisplayStrings( queryBuilder.mModelValues ), QStringList() << "NULL" << "testA" << "testB, testC" );

queryBuilder.fillValues( 2, 10 );
QCOMPARE( getModelItemDisplayStrings( queryBuilder.mModelValues ), QStringList() << "0" << "42" << "NULL" );
}

QGSTEST_MAIN( TestQgsQueryBuilder )
#include "testqgsquerybuilder.moc"

0 comments on commit aca1a9d

Please sign in to comment.