1 /***************************************************************************
2 testqgsquerybuilder.cpp
3 ---------------------------
4 begin : June 2021
5 copyright : (C) 2021 by Julien Cabieces
6 email : julien.cabieces@oslandia.com
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18
19 #include "qgstest.h"
20
21 #include <qgsvectorlayer.h>
22 #include <qgsquerybuilder.h>
23
24 class TestQgsQueryBuilder : public QObject
25 {
26 Q_OBJECT
27
28 public:
29 TestQgsQueryBuilder() = default;
30
31 private slots:
32 void initTestCase();// will be called before the first testfunction is executed.
33 void cleanupTestCase();// will be called after the last testfunction was executed.
34 void init();// will be called before each testfunction is executed.
35 void cleanup();// will be called after every testfunction.
36 void testFillValues();
37
38 private:
39
40 QStringList getModelItemDisplayStrings( QStandardItemModel *model );
41
42 };
43
initTestCase()44 void TestQgsQueryBuilder::initTestCase() // will be called before the first testfunction is executed.
45 {
46 }
47
cleanupTestCase()48 void TestQgsQueryBuilder::cleanupTestCase()
49 {
50 }
51
init()52 void TestQgsQueryBuilder::init()
53 {
54 }
55
cleanup()56 void TestQgsQueryBuilder::cleanup()
57 {
58 }
59
getModelItemDisplayStrings(QStandardItemModel * model)60 QStringList TestQgsQueryBuilder::getModelItemDisplayStrings( QStandardItemModel *model )
61 {
62 QStringList result;
63 for ( int row = 0; row < model->rowCount(); row++ )
64 {
65 result << model->item( row )->text();
66 }
67
68 result.sort();
69
70 return result;
71 }
72
73
testFillValues()74 void TestQgsQueryBuilder::testFillValues()
75 {
76 QgsVectorLayer vl( QStringLiteral( "Point?field=intarray:int[]&field=strarray:string[]&field=intf:int" ), QStringLiteral( "test" ), QStringLiteral( "memory" ) );
77
78 QgsFeature feat1( vl.fields() );
79 feat1.setAttribute( QStringLiteral( "intarray" ), QVariantList() << 1 );
80 feat1.setAttribute( QStringLiteral( "strarray" ), QVariantList() << QStringLiteral( "testA" ) );
81 feat1.setAttribute( QStringLiteral( "intf" ), 0 );
82 vl.dataProvider()->addFeature( feat1 );
83
84 QgsFeature feat2( vl.fields() );
85 feat2.setAttribute( QStringLiteral( "intarray" ), QVariantList() << 2 << 3 );
86 feat2.setAttribute( QStringLiteral( "strarray" ), QVariantList() << QVariant() );
87 feat2.setAttribute( QStringLiteral( "intf" ), 42 );
88 vl.dataProvider()->addFeature( feat2 );
89
90 QgsFeature feat3( vl.fields() );
91 feat3.setAttribute( QStringLiteral( "intarray" ), QVariantList() << QVariant() );
92 feat3.setAttribute( QStringLiteral( "strarray" ), QVariantList() << QStringLiteral( "testB" ) << QStringLiteral( "testC" ) );
93 feat3.setAttribute( QStringLiteral( "intf" ), 42 );
94 vl.dataProvider()->addFeature( feat3 );
95
96 QgsFeature feat4( vl.fields() );
97 feat4.setAttribute( QStringLiteral( "intarray" ), QVariantList() << 4 << 5 << 6 );
98 feat4.setAttribute( QStringLiteral( "strarray" ), QVariantList() << QVariant() );
99 feat4.setAttribute( QStringLiteral( "intf" ), QVariant() );
100 vl.dataProvider()->addFeature( feat4 );
101
102 QgsQueryBuilder queryBuilder( &vl );
103
104 queryBuilder.fillValues( 0, 10 );
105 QCOMPARE( getModelItemDisplayStrings( queryBuilder.mModelValues ), QStringList() << "1" << "2, 3" << "4, 5, 6" << "NULL" );
106
107 queryBuilder.fillValues( 1, 10 );
108 QCOMPARE( getModelItemDisplayStrings( queryBuilder.mModelValues ), QStringList() << "NULL" << "testA" << "testB, testC" );
109
110 queryBuilder.fillValues( 2, 10 );
111 QCOMPARE( getModelItemDisplayStrings( queryBuilder.mModelValues ), QStringList() << "0" << "42" << "NULL" );
112 }
113
114 QGSTEST_MAIN( TestQgsQueryBuilder )
115 #include "testqgsquerybuilder.moc"
116