/
testqgsrelationreferencewidget.cpp
183 lines (158 loc) · 6.35 KB
/
testqgsrelationreferencewidget.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
/***************************************************************************
testqgsrelationreferencewidget.cpp
--------------------------------------
Date : 21 07 2017
Copyright : (C) 2017 Paul Blottiere
Email : paul dot blottiere at oslandia dot 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 <editorwidgets/core/qgseditorwidgetregistry.h>
#include <qgsapplication.h>
#include "qgseditorwidgetwrapper.h"
#include <qgsmaplayerregistry.h>
#include <editorwidgets/qgsrelationreferencewidget.h>
#include <qgsproject.h>
#include <qgsattributeform.h>
#include <qgsrelationmanager.h>
#include <attributetable/qgsattributetablefiltermodel.h>
class TestQgsRelationReferenceWidget : public QObject
{
Q_OBJECT
public:
TestQgsRelationReferenceWidget() {}
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 testChainFilter();
};
void TestQgsRelationReferenceWidget::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();
QgsEditorWidgetRegistry::initEditors();
}
void TestQgsRelationReferenceWidget::cleanupTestCase()
{
QgsApplication::exitQgis();
}
void TestQgsRelationReferenceWidget::init()
{
}
void TestQgsRelationReferenceWidget::cleanup()
{
}
void TestQgsRelationReferenceWidget::testChainFilter()
{
// create layers
QgsVectorLayer vl1( QString( "LineString?crs=epsg:3111&field=pk:int&field=fk:int" ), QString( "vl1" ), QString( "memory" ) );
QgsMapLayerRegistry::instance()->addMapLayer( &vl1 );
QgsVectorLayer vl2( QString( "LineString?field=pk:int&field=material:string&field=diameter:int&field=raccord:string" ), QString( "vl2" ), QString( "memory" ) );
QgsMapLayerRegistry::instance()->addMapLayer( &vl2 );
// create a relation between them
QgsRelation relation;
relation.setRelationId( QString( "vl1.vl2" ) );
relation.setRelationName( QString( "vl1.vl2" ) );
relation.setReferencingLayer( vl1.id() );
relation.setReferencedLayer( vl2.id() );
relation.addFieldPair( "fk", "pk" );
QVERIFY( relation.isValid() );
QgsProject::instance()->relationManager()->addRelation( relation );
// add features
QgsFeature ft0( vl1.fields() );
ft0.setAttribute( QString( "pk" ), 0 );
ft0.setAttribute( QString( "fk" ), 0 );
vl1.startEditing();
vl1.addFeature( ft0 );
vl1.commitChanges();
QgsFeature ft1( vl1.fields() );
ft1.setAttribute( QString( "pk" ), 1 );
ft1.setAttribute( QString( "fk" ), 1 );
vl1.startEditing();
vl1.addFeature( ft1 );
vl1.commitChanges();
QgsFeature ft2( vl2.fields() );
ft2.setAttribute( QString( "pk" ), 10 );
ft2.setAttribute( QString( "material" ), "iron" );
ft2.setAttribute( QString( "diameter" ), 120 );
ft2.setAttribute( QString( "raccord" ), "brides" );
vl2.startEditing();
vl2.addFeature( ft2 );
vl2.commitChanges();
QgsFeature ft3( vl2.fields() );
ft3.setAttribute( QString( "pk" ), 11 );
ft3.setAttribute( QString( "material" ), "iron" );
ft3.setAttribute( QString( "diameter" ), 120 );
ft3.setAttribute( QString( "raccord" ), "sleeve" );
vl2.startEditing();
vl2.addFeature( ft3 );
vl2.commitChanges();
QgsFeature ft4( vl2.fields() );
ft4.setAttribute( QString( "pk" ), 12 );
ft4.setAttribute( QString( "material" ), "steel" );
ft4.setAttribute( QString( "diameter" ), 120 );
ft4.setAttribute( QString( "raccord" ), "collar" );
vl2.startEditing();
vl2.addFeature( ft4 );
vl2.commitChanges();
// init a relation reference widget
QStringList filterFields = QStringList() << "material" << "diameter" << "raccord";
QgsRelationReferenceWidget w( new QWidget() );
w.setChainFilters( true );
w.setFilterFields( filterFields );
w.setRelation( relation, true );
w.init();
// check default status for comboboxes
QList<QComboBox *> cbs = w.mFilterComboBoxes;
QCOMPARE( cbs.count(), 3 );
Q_FOREACH ( const QComboBox *cb, cbs )
{
if ( cb->currentText() == "raccord" )
QCOMPARE( cb->count(), 5 );
else if ( cb->currentText() == "material" )
QCOMPARE( cb->count(), 4 );
else if ( cb->currentText() == "diameter" )
QCOMPARE( cb->count(), 3 );
}
// set first filter
cbs[0]->setCurrentIndex( cbs[0]->findText( "iron" ) );
cbs[1]->setCurrentIndex( cbs[1]->findText( "120" ) );
Q_FOREACH ( const QComboBox *cb, cbs )
{
if ( cb->itemText( 0 ) == "material" )
QCOMPARE( cb->count(), 4 );
else if ( cb->itemText( 0 ) == "diameter" )
QCOMPARE( cb->count(), 2 );
else if ( cb->itemText( 0 ) == "raccord" )
{
QStringList items;
for ( int i = 0; i < cb->count(); i++ )
items << cb->itemText( i );
QCOMPARE( cb->count(), 3 );
QCOMPARE(( bool )items.contains( "collar" ), false );
// collar should not be available in combobox as there's no existing
// feature with the filter expression:
// "material" == 'iron' AND "diameter" == '120' AND "raccord" = 'collar'
}
}
// set the filter for "raccord" and then reset filter for "diameter". As
// chain filter is activated, the filter on "raccord" field should be reset
cbs[2]->setCurrentIndex( cbs[2]->findText( "brides" ) );
cbs[1]->setCurrentIndex( cbs[1]->findText( "diameter" ) );
// combobox should propose NULL, 10 and 11 because the filter is now:
// "material" == 'iron'
QCOMPARE( w.mComboBox->count(), 3 );
// if there's no filter at all, all features' id should be proposed
cbs[0]->setCurrentIndex( cbs[0]->findText( "material" ) );
QCOMPARE( w.mComboBox->count(), 4 );
}
QTEST_MAIN( TestQgsRelationReferenceWidget )
#include "testqgsrelationreferencewidget.moc"