Skip to content

Commit ba339f2

Browse files
committedMay 15, 2018
Some more test cases for value relation widget
1 parent 63d2086 commit ba339f2

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
 

‎tests/src/gui/testqgsvaluerelationwidgetwrapper.cpp

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class TestQgsValueRelationWidgetWrapper : public QObject
4343

4444
void testScrollBarUnlocked();
4545
void testDrillDown();
46+
void testDrillDownMulti();
4647
};
4748

4849
void TestQgsValueRelationWidgetWrapper::initTestCase()
@@ -63,6 +64,7 @@ void TestQgsValueRelationWidgetWrapper::init()
6364

6465
void TestQgsValueRelationWidgetWrapper::cleanup()
6566
{
67+
6668
}
6769

6870
void TestQgsValueRelationWidgetWrapper::testScrollBarUnlocked()
@@ -166,8 +168,10 @@ void TestQgsValueRelationWidgetWrapper::testDrillDown()
166168
w_municipality.setFeature( f3 );
167169
QCOMPARE( w_municipality.mCache.size(), 1 );
168170

171+
// Check first is selected
169172
QCOMPARE( w_municipality.mComboBox->count(), 1 );
170173
QCOMPARE( w_municipality.mComboBox->itemText( 0 ), QStringLiteral( "Some Place By The River" ) );
174+
QCOMPARE( w_municipality.value().toString(), QStringLiteral( "1" ) );
171175

172176
// Filter by geometry
173177
cfg_municipality[ QStringLiteral( "FilterExpression" ) ] = QStringLiteral( "contains(buffer(@current_geometry, 1 ), $geometry)" );
@@ -190,6 +194,123 @@ void TestQgsValueRelationWidgetWrapper::testDrillDown()
190194
QCOMPARE( w_municipality.mComboBox->itemText( 0 ), QStringLiteral( "Dreamland By The Clouds" ) );
191195
QCOMPARE( w_municipality.mComboBox->itemText( 1 ), QStringLiteral( "Some Place By The River" ) );
192196

197+
// Check with allow null
198+
cfg_municipality[QStringLiteral( "AllowNull" )] = true;
199+
w_municipality.setConfig( cfg_municipality );
200+
w_municipality.setFeature( QgsFeature() );
201+
202+
// Check null is selected
203+
QCOMPARE( w_municipality.mComboBox->count(), 3 );
204+
QCOMPARE( w_municipality.mComboBox->itemText( 0 ), QStringLiteral( "(no selection)" ) );
205+
QCOMPARE( w_municipality.value().toString(), QStringLiteral( "" ) );
206+
207+
// Check order by value false
208+
cfg_municipality[QStringLiteral( "AllowNull" )] = false;
209+
cfg_municipality[QStringLiteral( "OrderByValue" )] = false;
210+
w_municipality.setConfig( cfg_municipality );
211+
w_municipality.setFeature( f3 );
212+
QCOMPARE( w_municipality.mComboBox->itemText( 1 ), QStringLiteral( "Dreamland By The Clouds" ) );
213+
QCOMPARE( w_municipality.mComboBox->itemText( 0 ), QStringLiteral( "Some Place By The River" ) );
214+
215+
}
216+
217+
218+
void TestQgsValueRelationWidgetWrapper::testDrillDownMulti()
219+
{
220+
// create a vector layer
221+
QgsVectorLayer vl1( QStringLiteral( "Polygon?crs=epsg:4326&field=pk:int&field=province:int&field=municipality:string" ), QStringLiteral( "vl1" ), QStringLiteral( "memory" ) );
222+
QgsVectorLayer vl2( QStringLiteral( "Point?crs=epsg:4326&field=pk:int&field=fk_province:int&field=fk_municipality:int" ), QStringLiteral( "vl2" ), QStringLiteral( "memory" ) );
223+
QgsProject::instance()->addMapLayer( &vl1, false, false );
224+
QgsProject::instance()->addMapLayer( &vl2, false, false );
225+
226+
// insert some features
227+
QgsFeature f1( vl1.fields() );
228+
f1.setAttribute( QStringLiteral( "pk" ), 1 );
229+
f1.setAttribute( QStringLiteral( "province" ), 123 );
230+
f1.setAttribute( QStringLiteral( "municipality" ), QStringLiteral( "Some Place By The River" ) );
231+
f1.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "POLYGON(( 0 0, 0 1, 1 1, 1 0, 0 0 ))" ) ) );
232+
QVERIFY( f1.isValid() );
233+
QgsFeature f2( vl1.fields() );
234+
f2.setAttribute( QStringLiteral( "pk" ), 2 );
235+
f2.setAttribute( QStringLiteral( "province" ), 245 );
236+
f2.setAttribute( QStringLiteral( "municipality" ), QStringLiteral( "Dreamland By The Clouds" ) );
237+
f2.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "POLYGON(( 1 0, 1 1, 2 1, 2 0, 1 0 ))" ) ) );
238+
QVERIFY( f2.isValid() );
239+
QVERIFY( vl1.dataProvider()->addFeatures( QgsFeatureList() << f1 << f2 ) );
240+
241+
QgsFeature f3( vl2.fields() );
242+
f3.setAttribute( QStringLiteral( "fk_province" ), 123 );
243+
f3.setAttribute( QStringLiteral( "fk_municipality" ), QStringLiteral( "{1}" ) );
244+
f3.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "POINT( 0.5 0.5)" ) ) );
245+
QVERIFY( f3.isValid() );
246+
QVERIFY( f3.geometry().isGeosValid() );
247+
QVERIFY( vl2.dataProvider()->addFeature( f3 ) );
248+
249+
// build a value relation widget wrapper for municipality
250+
QgsValueRelationWidgetWrapper w_municipality( &vl2, vl2.fields().indexOf( QStringLiteral( "fk_municipality" ) ), nullptr, nullptr );
251+
QVariantMap cfg_municipality;
252+
cfg_municipality.insert( QStringLiteral( "Layer" ), vl1.id() );
253+
cfg_municipality.insert( QStringLiteral( "Key" ), QStringLiteral( "pk" ) );
254+
cfg_municipality.insert( QStringLiteral( "Value" ), QStringLiteral( "municipality" ) );
255+
cfg_municipality.insert( QStringLiteral( "AllowMulti" ), true );
256+
cfg_municipality.insert( QStringLiteral( "NofColumns" ), 1 );
257+
cfg_municipality.insert( QStringLiteral( "AllowNull" ), false );
258+
cfg_municipality.insert( QStringLiteral( "OrderByValue" ), true );
259+
cfg_municipality.insert( QStringLiteral( "FilterExpression" ), QStringLiteral( "\"province\" = current_value('fk_province')" ) );
260+
cfg_municipality.insert( QStringLiteral( "UseCompleter" ), false );
261+
w_municipality.setConfig( cfg_municipality );
262+
w_municipality.widget();
263+
w_municipality.setEnabled( true );
264+
265+
QCOMPARE( w_municipality.mCache.size(), 2 );
266+
QCOMPARE( w_municipality.mTableWidget->rowCount(), 2 );
267+
w_municipality.setFeature( f3 );
268+
QCOMPARE( w_municipality.mCache.size(), 1 );
269+
270+
QCOMPARE( w_municipality.mTableWidget->rowCount(), 1 );
271+
QCOMPARE( w_municipality.mTableWidget->item( 0, 0 )->text(), QStringLiteral( "Some Place By The River" ) );
272+
QCOMPARE( w_municipality.value().toString(), QStringLiteral( "{1}" ) );
273+
274+
// Filter by geometry
275+
cfg_municipality[ QStringLiteral( "FilterExpression" ) ] = QStringLiteral( "contains(buffer(@current_geometry, 1 ), $geometry)" );
276+
w_municipality.setConfig( cfg_municipality );
277+
w_municipality.setFeature( f3 );
278+
QCOMPARE( w_municipality.mTableWidget->rowCount(), 1 );
279+
QCOMPARE( w_municipality.mTableWidget->item( 0, 0 )->text(), QStringLiteral( "Some Place By The River" ) );
280+
281+
// Move the point to 1.5 0.5
282+
f3.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "POINT( 1.5 0.5)" ) ) );
283+
w_municipality.setFeature( f3 );
284+
QCOMPARE( w_municipality.mTableWidget->rowCount(), 1 );
285+
QCOMPARE( w_municipality.mTableWidget->item( 0, 0 )->text(), QStringLiteral( "Dreamland By The Clouds" ) );
286+
287+
// Enlarge the buffer
288+
cfg_municipality[ QStringLiteral( "FilterExpression" ) ] = QStringLiteral( "contains(buffer(@current_geometry, 3 ), $geometry)" );
289+
w_municipality.setConfig( cfg_municipality );
290+
w_municipality.setFeature( f3 );
291+
QCOMPARE( w_municipality.mTableWidget->rowCount(), 2 );
292+
QCOMPARE( w_municipality.mTableWidget->item( 0, 0 )->text(), QStringLiteral( "Dreamland By The Clouds" ) );
293+
QCOMPARE( w_municipality.mTableWidget->item( 0, 0 )->data( Qt::UserRole ).toString(), QStringLiteral( "2" ) );
294+
QCOMPARE( w_municipality.mTableWidget->item( 1, 0 )->text(), QStringLiteral( "Some Place By The River" ) );
295+
QCOMPARE( w_municipality.mTableWidget->item( 1, 0 )->data( Qt::UserRole ).toString(), QStringLiteral( "1" ) );
296+
QCOMPARE( w_municipality.value().toString(), QStringLiteral( "{1}" ) );
297+
QCOMPARE( w_municipality.mTableWidget->item( 0, 0 )->checkState(), Qt::Unchecked );
298+
QCOMPARE( w_municipality.mTableWidget->item( 1, 0 )->checkState(), Qt::Checked );
299+
w_municipality.setValue( QStringLiteral( "{1,2}" ) );
300+
QCOMPARE( w_municipality.value().toString(), QStringLiteral( "{2,1}" ) );
301+
QCOMPARE( w_municipality.mTableWidget->item( 0, 0 )->checkState(), Qt::Checked );
302+
QCOMPARE( w_municipality.mTableWidget->item( 1, 0 )->checkState(), Qt::Checked );
303+
304+
// Check values are checked
305+
f3.setAttribute( QStringLiteral( "fk_municipality" ), QStringLiteral( "{1,2}" ) );
306+
w_municipality.setFeature( f3 );
307+
QCOMPARE( w_municipality.mTableWidget->rowCount(), 2 );
308+
QCOMPARE( w_municipality.mTableWidget->item( 0, 0 )->text(), QStringLiteral( "Dreamland By The Clouds" ) );
309+
QCOMPARE( w_municipality.mTableWidget->item( 1, 0 )->text(), QStringLiteral( "Some Place By The River" ) );
310+
QCOMPARE( w_municipality.mTableWidget->item( 0, 0 )->checkState(), Qt::Checked );
311+
QCOMPARE( w_municipality.mTableWidget->item( 1, 0 )->checkState(), Qt::Checked );
312+
QCOMPARE( w_municipality.value().toString(), QStringLiteral( "{2,1}" ) );
313+
193314
}
194315

195316
QGSTEST_MAIN( TestQgsValueRelationWidgetWrapper )

0 commit comments

Comments
 (0)
Please sign in to comment.