/
testqgsfieldexpressionwidget.cpp
225 lines (180 loc) · 7.29 KB
/
testqgsfieldexpressionwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/***************************************************************************
testqgsfieldexpressionwidget.cpp
--------------------------------------
Date : January 2016
Copyright : (C) 2016 Denis Rouzaud
Email : denis.rouzaud@gmail.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 <QtTest/QtTest>
#include <QObject>
//qgis includes...
#include <qgsvectorlayer.h>
#include <qgsapplication.h>
#include <qgsvectorlayerjoinbuffer.h>
#include <qgsmaplayerregistry.h>
#include <qgsfieldexpressionwidget.h>
#include <qgsproject.h>
/** @ingroup UnitTests
* This is a unit test for the field expression widget
*
* @see QgsFieldExpressionWidget
*/
class TestQgsFieldExpressionWidget : public QObject
{
Q_OBJECT
public:
TestQgsFieldExpressionWidget()
: mWidget( nullptr )
, mLayerA( nullptr )
, mLayerB( nullptr )
{}
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 testRemoveJoin();
void asExpression();
void testIsValid();
private:
QgsFieldExpressionWidget* mWidget;
QgsVectorLayer* mLayerA;
QgsVectorLayer* mLayerB;
};
// runs before all tests
void TestQgsFieldExpressionWidget::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();
// Set up the QSettings environment
QCoreApplication::setOrganizationName( "QGIS" );
QCoreApplication::setOrganizationDomain( "qgis.org" );
QCoreApplication::setApplicationName( "QGIS-TEST" );
// Create memory layers
// LAYER A //
mLayerA = new QgsVectorLayer( "Point?field=id_a:integer", "A", "memory" );
QVERIFY( mLayerA->isValid() );
QVERIFY( mLayerA->fields().count() == 1 );
QgsMapLayerRegistry::instance()->addMapLayer( mLayerA );
// LAYER B //
mLayerB = new QgsVectorLayer( "Point?field=id_b:integer&field=value_b", "B", "memory" );
QVERIFY( mLayerB->isValid() );
QVERIFY( mLayerB->fields().count() == 2 );
QgsMapLayerRegistry::instance()->addMapLayer( mLayerB );
// init widget
mWidget = new QgsFieldExpressionWidget();
mWidget->setLayer( mLayerA );
}
void TestQgsFieldExpressionWidget::init()
{
}
void TestQgsFieldExpressionWidget::cleanup()
{
}
void TestQgsFieldExpressionWidget::cleanupTestCase()
{
QgsApplication::exitQgis();
}
void TestQgsFieldExpressionWidget::testRemoveJoin()
{
QVERIFY( mLayerA->fields().count() == 1 );
QgsVectorJoinInfo joinInfo;
joinInfo.targetFieldName = "id_a";
joinInfo.joinLayerId = mLayerB->id();
joinInfo.joinFieldName = "id_b";
joinInfo.memoryCache = false;
joinInfo.prefix = "B_";
mLayerA->addJoin( joinInfo );
QVERIFY( mLayerA->fields().count() == 2 );
const QString expr = "'hello '|| B_value_b";
mWidget->setField( expr );
bool isExpression, isValid;
QVERIFY( mWidget->isValidExpression() );
QCOMPARE( mWidget->currentField( &isExpression, &isValid ), expr );
QVERIFY( isExpression );
QVERIFY( isValid );
QVERIFY( mLayerA->removeJoin( mLayerB->id() ) );
QVERIFY( mLayerA->fields().count() == 1 );
QCOMPARE( mWidget->mCombo->currentText(), expr );
QCOMPARE( mWidget->currentField( &isExpression, &isValid ), expr );
QVERIFY( isExpression );
// QVERIFY( !isValid ); TODO: the expression should not be valid anymore since the field doesn't exist anymore. Maybe we need a new expression method to get more details.
}
void TestQgsFieldExpressionWidget::asExpression()
{
QgsVectorLayer* layer = new QgsVectorLayer( "point?field=fld:int&field=fld2:int&field=fld3:int", "x", "memory" );
QgsMapLayerRegistry::instance()->addMapLayer( layer );
QScopedPointer< QgsFieldExpressionWidget > widget( new QgsFieldExpressionWidget() );
widget->setLayer( layer );
// check with field set
widget->setField( "fld" );
QCOMPARE( widget->asExpression(), QString( "\"fld\"" ) );
// check with expressions set
widget->setField( "fld + 1" );
QCOMPARE( widget->asExpression(), QString( "fld + 1" ) );
widget->setField( "1" );
QCOMPARE( widget->asExpression(), QString( "1" ) );
widget->setField( "\"fld2\"" );
QCOMPARE( widget->asExpression(), QString( "\"fld2\"" ) );
// check switching back to a field
widget->setField( "fld3" );
QCOMPARE( widget->asExpression(), QString( "\"fld3\"" ) );
QgsMapLayerRegistry::instance()->removeMapLayer( layer );
}
void TestQgsFieldExpressionWidget::testIsValid()
{
QgsVectorLayer* layer = new QgsVectorLayer( "point?field=fld:int&field=name%20with%20space:string", "x", "memory" );
QgsMapLayerRegistry::instance()->addMapLayer( layer );
QScopedPointer< QgsFieldExpressionWidget > widget( new QgsFieldExpressionWidget() );
widget->setLayer( layer );
// also check the fieldChanged signal to ensure that the emitted bool isValid value is correct
QSignalSpy spy( widget.data(), SIGNAL( fieldChanged( QString, bool ) ) );
// check with simple field name set
bool isExpression = false;
bool isValid = false;
widget->setField( "fld" );
QCOMPARE( widget->currentField( &isExpression, &isValid ), QString( "fld" ) );
QVERIFY( !isExpression );
QVERIFY( isValid );
QVERIFY( widget->isValidExpression() );
QCOMPARE( spy.count(), 1 );
QCOMPARE( spy.last().at( 0 ).toString(), QString( "fld" ) );
QVERIFY( spy.last().at( 1 ).toBool() );
//check with complex field name set
widget->setField( "name with space" );
QCOMPARE( widget->currentField( &isExpression, &isValid ), QString( "name with space" ) );
QVERIFY( !isExpression );
QVERIFY( isValid );
QVERIFY( !widget->isValidExpression() );
QCOMPARE( spy.count(), 2 );
QCOMPARE( spy.last().at( 0 ).toString(), QString( "name with space" ) );
QVERIFY( spy.last().at( 1 ).toBool() );
//check with valid expression set
widget->setField( "2 * 4" );
QCOMPARE( widget->currentField( &isExpression, &isValid ), QString( "2 * 4" ) );
QVERIFY( isExpression );
QVERIFY( isValid );
QVERIFY( widget->isValidExpression() );
QCOMPARE( spy.count(), 3 );
QCOMPARE( spy.last().at( 0 ).toString(), QString( "2 * 4" ) );
QVERIFY( spy.last().at( 1 ).toBool() );
//check with invalid expression set
widget->setField( "2 *" );
QCOMPARE( widget->currentField( &isExpression, &isValid ), QString( "2 *" ) );
QVERIFY( isExpression );
QVERIFY( !isValid );
QVERIFY( !widget->isValidExpression() );
QCOMPARE( spy.count(), 4 );
QCOMPARE( spy.last().at( 0 ).toString(), QString( "2 *" ) );
QVERIFY( !spy.last().at( 1 ).toBool() );
QgsMapLayerRegistry::instance()->removeMapLayer( layer );
}
QTEST_MAIN( TestQgsFieldExpressionWidget )
#include "testqgsfieldexpressionwidget.moc"