Skip to content

Commit a0d8be3

Browse files
committedDec 14, 2022
add tests, fix existing test
1 parent 111753a commit a0d8be3

File tree

1 file changed

+160
-15
lines changed

1 file changed

+160
-15
lines changed
 

‎tests/src/gui/testqgsvaluerelationwidgetwrapper.cpp

Lines changed: 160 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include <qgsvectorlayer.h>
2525
#include "qgseditorwidgetwrapper.h"
2626
#include <editorwidgets/qgsvaluerelationwidgetwrapper.h>
27-
#include <QTableWidget>
27+
#include "qgsfilterlineedit.h"
2828
#include <QComboBox>
2929
#include "qgsgui.h"
3030
#include <gdal_version.h>
@@ -60,6 +60,8 @@ class TestQgsValueRelationWidgetWrapper : public QObject
6060
void testMatchLayerName();
6161
//! Check that setFeature works correctly after regression #42003
6262
void testRegressionGH42003();
63+
void testAllowMultiColumns();
64+
void testAllowMultiAndCompleter();
6365
};
6466

6567
void TestQgsValueRelationWidgetWrapper::initTestCase()
@@ -86,24 +88,48 @@ void TestQgsValueRelationWidgetWrapper::cleanup()
8688
void TestQgsValueRelationWidgetWrapper::testScrollBarUnlocked()
8789
{
8890
// create a vector layer
89-
QgsVectorLayer vl1( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=fk|:int" ), QStringLiteral( "vl1" ), QStringLiteral( "memory" ) );
91+
QgsVectorLayer vl1( QStringLiteral( "Polygon?crs=epsg:4326&field=pk:int&field=province:int&field=municipality:string" ), QStringLiteral( "vl1" ), QStringLiteral( "memory" ) );
92+
QgsVectorLayer vl2( QStringLiteral( "Point?crs=epsg:4326&field=pk:int&field=fk_province:int&field=fk_municipality:int" ), QStringLiteral( "vl2" ), QStringLiteral( "memory" ) );
9093
QgsProject::instance()->addMapLayer( &vl1, false, false );
94+
QgsProject::instance()->addMapLayer( &vl2, false, false );
9195

92-
// build a value relation widget wrapper
93-
QgsValueRelationWidgetWrapper w( &vl1, 0, nullptr, nullptr );
94-
95-
QVariantMap config;
96-
config.insert( QStringLiteral( "AllowMulti" ), true );
97-
w.setConfig( config );
98-
w.widget();
99-
w.setEnabled( true );
96+
// insert some features
97+
QgsFeature f1( vl1.fields() );
98+
f1.setAttribute( QStringLiteral( "pk" ), 1 );
99+
f1.setAttribute( QStringLiteral( "province" ), 123 );
100+
f1.setAttribute( QStringLiteral( "municipality" ), QStringLiteral( "Some Place By The River" ) );
101+
f1.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "POLYGON(( 0 0, 0 1, 1 1, 1 0, 0 0 ))" ) ) );
102+
QVERIFY( f1.isValid() );
103+
QgsFeature f2( vl1.fields() );
104+
f2.setAttribute( QStringLiteral( "pk" ), 2 );
105+
f2.setAttribute( QStringLiteral( "province" ), 245 );
106+
f2.setAttribute( QStringLiteral( "municipality" ), QStringLiteral( "Dreamland By The Clouds" ) );
107+
f2.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "POLYGON(( 1 0, 1 1, 2 1, 2 0, 1 0 ))" ) ) );
108+
QVERIFY( f2.isValid() );
109+
QVERIFY( vl1.dataProvider()->addFeatures( QgsFeatureList() << f1 << f2 ) );
100110

101-
// add an item virtually
102-
QTableWidgetItem item;
103-
item.setText( QStringLiteral( "MyText" ) );
104-
w.mTableWidget->setItem( 0, 0, &item );
111+
QgsFeature f3( vl2.fields() );
112+
f3.setAttribute( QStringLiteral( "fk_province" ), 123 );
113+
f3.setAttribute( QStringLiteral( "fk_municipality" ), 1 );
114+
f3.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "POINT( 0.5 0.5)" ) ) );
115+
QVERIFY( f3.isValid() );
116+
QVERIFY( f3.geometry().isGeosValid() );
117+
QVERIFY( vl2.dataProvider()->addFeature( f3 ) );
105118

106-
QCOMPARE( w.mTableWidget->item( 0, 0 )->text(), QString( "MyText" ) );
119+
// build a value relation widget wrapper for municipality
120+
QgsValueRelationWidgetWrapper w( &vl2, vl2.fields().indexOf( QLatin1String( "fk_municipality" ) ), nullptr, nullptr );
121+
QVariantMap cfg;
122+
cfg.insert( QStringLiteral( "Layer" ), vl1.id() );
123+
cfg.insert( QStringLiteral( "Key" ), QStringLiteral( "pk" ) );
124+
cfg.insert( QStringLiteral( "Value" ), QStringLiteral( "municipality" ) );
125+
cfg.insert( QStringLiteral( "AllowMulti" ), true );
126+
cfg.insert( QStringLiteral( "NofColumns" ), 1 );
127+
cfg.insert( QStringLiteral( "AllowNull" ), false );
128+
cfg.insert( QStringLiteral( "OrderByValue" ), true );
129+
cfg.insert( QStringLiteral( "FilterExpression" ), QStringLiteral( "\"province\" = current_value('fk_province')" ) );
130+
cfg.insert( QStringLiteral( "UseCompleter" ), false );
131+
w.setConfig( cfg );
132+
w.widget();
107133

108134
// when the widget wrapper is enabled, the container should be enabled
109135
// as well as items
@@ -1672,5 +1698,124 @@ void TestQgsValueRelationWidgetWrapper::testRegressionGH42003()
16721698

16731699
}
16741700

1701+
void TestQgsValueRelationWidgetWrapper::testAllowMultiColumns()
1702+
{
1703+
// create ogr gpkg layers
1704+
const QString myFileName( TEST_DATA_DIR ); //defined in CmakeLists.txt
1705+
const QString myTempDirName = tempDir.path();
1706+
QFile::copy( myFileName + "/provider/test_json.gpkg", myTempDirName + "/test_json.gpkg" );
1707+
const QString myTempFileName = myTempDirName + "/test_json.gpkg";
1708+
const QFileInfo myMapFileInfo( myTempFileName );
1709+
QgsVectorLayer *vl_text = new QgsVectorLayer( myMapFileInfo.filePath() + "|layername=foo", "test", QStringLiteral( "ogr" ) );
1710+
QgsVectorLayer *vl_authors = new QgsVectorLayer( myMapFileInfo.filePath() + "|layername=author", "test", QStringLiteral( "ogr" ) );
1711+
QVERIFY( vl_text->isValid() );
1712+
QVERIFY( vl_authors->isValid() );
1713+
1714+
QgsProject::instance()->addMapLayer( vl_text, false, false );
1715+
QgsProject::instance()->addMapLayer( vl_authors, false, false );
1716+
vl_text->startEditing();
1717+
1718+
// build a value relation widget wrapper for authors
1719+
QgsValueRelationWidgetWrapper w_favoriteauthors( vl_text, vl_text->fields().indexOf( QLatin1String( "PRFEDEA" ) ), nullptr, nullptr );
1720+
QVariantMap cfg_favoriteauthors;
1721+
cfg_favoriteauthors.insert( QStringLiteral( "Layer" ), vl_authors->id() );
1722+
cfg_favoriteauthors.insert( QStringLiteral( "Key" ), QStringLiteral( "fid" ) );
1723+
cfg_favoriteauthors.insert( QStringLiteral( "Value" ), QStringLiteral( "NAME" ) );
1724+
cfg_favoriteauthors.insert( QStringLiteral( "AllowMulti" ), true );
1725+
cfg_favoriteauthors.insert( QStringLiteral( "NofColumns" ), 3 );
1726+
cfg_favoriteauthors.insert( QStringLiteral( "AllowNull" ), false );
1727+
cfg_favoriteauthors.insert( QStringLiteral( "OrderByValue" ), false );
1728+
cfg_favoriteauthors.insert( QStringLiteral( "UseCompleter" ), false );
1729+
w_favoriteauthors.setConfig( cfg_favoriteauthors );
1730+
w_favoriteauthors.widget();
1731+
w_favoriteauthors.setEnabled( true );
1732+
1733+
//check if set up nice
1734+
QCOMPARE( w_favoriteauthors.mTableWidget->rowCount(), 2 );
1735+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->text(), QStringLiteral( "Erich Gamma" ) );
1736+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->data( Qt::UserRole ).toString(), QStringLiteral( "1" ) );
1737+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 1 )->text(), QStringLiteral( "Richard Helm" ) );
1738+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 1 )->data( Qt::UserRole ).toString(), QStringLiteral( "2" ) );
1739+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 2 )->text(), QStringLiteral( "Ralph Johnson" ) );
1740+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 2 )->data( Qt::UserRole ).toString(), QStringLiteral( "3" ) );
1741+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->text(), QStringLiteral( "John Vlissides" ) );
1742+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->data( Qt::UserRole ).toString(), QStringLiteral( "4" ) );
1743+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 1 )->text(), QStringLiteral( "Douglas Adams" ) );
1744+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 1 )->data( Qt::UserRole ).toString(), QStringLiteral( "5" ) );
1745+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 2 )->text(), QStringLiteral( "Ken Follett" ) );
1746+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 2 )->data( Qt::UserRole ).toString(), QStringLiteral( "6" ) );
1747+
}
1748+
1749+
void TestQgsValueRelationWidgetWrapper::testAllowMultiAndCompleter()
1750+
{
1751+
// create ogr gpkg layers
1752+
const QString myFileName( TEST_DATA_DIR ); //defined in CmakeLists.txt
1753+
const QString myTempDirName = tempDir.path();
1754+
QFile::copy( myFileName + "/provider/test_json.gpkg", myTempDirName + "/test_json.gpkg" );
1755+
const QString myTempFileName = myTempDirName + "/test_json.gpkg";
1756+
const QFileInfo myMapFileInfo( myTempFileName );
1757+
QgsVectorLayer *vl_text = new QgsVectorLayer( myMapFileInfo.filePath() + "|layername=foo", "test", QStringLiteral( "ogr" ) );
1758+
QgsVectorLayer *vl_authors = new QgsVectorLayer( myMapFileInfo.filePath() + "|layername=author", "test", QStringLiteral( "ogr" ) );
1759+
QVERIFY( vl_text->isValid() );
1760+
QVERIFY( vl_authors->isValid() );
1761+
1762+
QgsProject::instance()->addMapLayer( vl_text, false, false );
1763+
QgsProject::instance()->addMapLayer( vl_authors, false, false );
1764+
vl_text->startEditing();
1765+
1766+
// build a value relation widget wrapper for authors
1767+
QgsValueRelationWidgetWrapper w_favoriteauthors( vl_text, vl_text->fields().indexOf( QLatin1String( "PRFEDEA" ) ), nullptr, nullptr );
1768+
QVariantMap cfg_favoriteauthors;
1769+
cfg_favoriteauthors.insert( QStringLiteral( "Layer" ), vl_authors->id() );
1770+
cfg_favoriteauthors.insert( QStringLiteral( "Key" ), QStringLiteral( "fid" ) );
1771+
cfg_favoriteauthors.insert( QStringLiteral( "Value" ), QStringLiteral( "NAME" ) );
1772+
cfg_favoriteauthors.insert( QStringLiteral( "AllowMulti" ), true );
1773+
cfg_favoriteauthors.insert( QStringLiteral( "NofColumns" ), 3 );
1774+
cfg_favoriteauthors.insert( QStringLiteral( "AllowNull" ), false );
1775+
cfg_favoriteauthors.insert( QStringLiteral( "OrderByValue" ), false );
1776+
cfg_favoriteauthors.insert( QStringLiteral( "UseCompleter" ), true );
1777+
w_favoriteauthors.setConfig( cfg_favoriteauthors );
1778+
w_favoriteauthors.widget();
1779+
w_favoriteauthors.setEnabled( true );
1780+
1781+
//check if set up nice
1782+
QCOMPARE( w_favoriteauthors.mTableWidget->rowCount(), 2 );
1783+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->text(), QStringLiteral( "Erich Gamma" ) );
1784+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->data( Qt::UserRole ).toString(), QStringLiteral( "1" ) );
1785+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 1 )->text(), QStringLiteral( "Richard Helm" ) );
1786+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 1 )->data( Qt::UserRole ).toString(), QStringLiteral( "2" ) );
1787+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 2 )->text(), QStringLiteral( "Ralph Johnson" ) );
1788+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 2 )->data( Qt::UserRole ).toString(), QStringLiteral( "3" ) );
1789+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->text(), QStringLiteral( "John Vlissides" ) );
1790+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->data( Qt::UserRole ).toString(), QStringLiteral( "4" ) );
1791+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 1 )->text(), QStringLiteral( "Douglas Adams" ) );
1792+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 1 )->data( Qt::UserRole ).toString(), QStringLiteral( "5" ) );
1793+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 2 )->text(), QStringLiteral( "Ken Follett" ) );
1794+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 2 )->data( Qt::UserRole ).toString(), QStringLiteral( "6" ) );
1795+
1796+
// set a filter string and check if items are filtered
1797+
w_favoriteauthors.mTableWidget->mSearchWidget->setText( QStringLiteral( "john" ) );
1798+
QCOMPARE( w_favoriteauthors.mTableWidget->rowCount(), 1 );
1799+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->text(), QStringLiteral( "Ralph Johnson" ) );
1800+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->data( Qt::UserRole ).toString(), QStringLiteral( "3" ) );
1801+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 1 )->text(), QStringLiteral( "John Vlissides" ) );
1802+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 1 )->data( Qt::UserRole ).toString(), QStringLiteral( "4" ) );
1803+
1804+
// clear the filter and check that all are back
1805+
w_favoriteauthors.mTableWidget->mSearchWidget->clear();
1806+
QCOMPARE( w_favoriteauthors.mTableWidget->rowCount(), 2 );
1807+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->text(), QStringLiteral( "Erich Gamma" ) );
1808+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 0 )->data( Qt::UserRole ).toString(), QStringLiteral( "1" ) );
1809+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 1 )->text(), QStringLiteral( "Richard Helm" ) );
1810+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 1 )->data( Qt::UserRole ).toString(), QStringLiteral( "2" ) );
1811+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 2 )->text(), QStringLiteral( "Ralph Johnson" ) );
1812+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 0, 2 )->data( Qt::UserRole ).toString(), QStringLiteral( "3" ) );
1813+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->text(), QStringLiteral( "John Vlissides" ) );
1814+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 0 )->data( Qt::UserRole ).toString(), QStringLiteral( "4" ) );
1815+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 1 )->text(), QStringLiteral( "Douglas Adams" ) );
1816+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 1 )->data( Qt::UserRole ).toString(), QStringLiteral( "5" ) );
1817+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 2 )->text(), QStringLiteral( "Ken Follett" ) );
1818+
QCOMPARE( w_favoriteauthors.mTableWidget->item( 1, 2 )->data( Qt::UserRole ).toString(), QStringLiteral( "6" ) );
1819+
}
16751820
QGSTEST_MAIN( TestQgsValueRelationWidgetWrapper )
16761821
#include "testqgsvaluerelationwidgetwrapper.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.