Skip to content

Commit e903017

Browse files
committedOct 26, 2017
Add tests for QgsFeatureListComboBox and friends
1 parent 7811f38 commit e903017

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed
 
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/***************************************************************************
2+
testqgsfeaturelistcombobox.cpp
3+
4+
---------------------
5+
begin : 3.10.2017
6+
copyright : (C) 2017 by Matthias Kuhn
7+
email : matthias@opengis.ch
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
17+
#include "qgstest.h"
18+
19+
#include "qgsapplication.h"
20+
#include "qgsfeaturelistcombobox.h"
21+
#include "qgsvectorlayer.h"
22+
#include "qgsfeaturefiltermodel.h"
23+
#include "qgsgui.h"
24+
25+
#include <memory>
26+
27+
#include <QLineEdit>
28+
29+
class TestQgsFeatureListComboBox : public QObject
30+
{
31+
Q_OBJECT
32+
public:
33+
TestQgsFeatureListComboBox() = default;
34+
35+
private slots:
36+
void initTestCase(); // will be called before the first testfunction is executed.
37+
void cleanupTestCase(); // will be called after the last testfunction was executed.
38+
void init(); // will be called before each testfunction is executed.
39+
void cleanup(); // will be called after every testfunction.
40+
41+
void testSetGetLayer();
42+
void testSetGetForeignKey();
43+
void testAllowNull();
44+
45+
private:
46+
void waitForLoaded( QgsFeatureListComboBox *cb );
47+
48+
std::unique_ptr<QgsVectorLayer> mLayer;
49+
};
50+
51+
void TestQgsFeatureListComboBox::initTestCase()
52+
{
53+
QgsApplication::init();
54+
QgsApplication::initQgis();
55+
}
56+
57+
void TestQgsFeatureListComboBox::cleanupTestCase()
58+
{
59+
QgsApplication::exitQgis();
60+
}
61+
62+
void TestQgsFeatureListComboBox::init()
63+
{
64+
// create layer
65+
mLayer.reset( new QgsVectorLayer( QStringLiteral( "LineString?field=pk:int&field=material:string&field=diameter:int&field=raccord:string" ), QStringLiteral( "vl2" ), QStringLiteral( "memory" ) ) );
66+
67+
// add features
68+
mLayer->startEditing();
69+
70+
QgsFeature ft1( mLayer->fields() );
71+
ft1.setAttribute( QStringLiteral( "pk" ), 10 );
72+
ft1.setAttribute( QStringLiteral( "material" ), "iron" );
73+
ft1.setAttribute( QStringLiteral( "diameter" ), 120 );
74+
ft1.setAttribute( QStringLiteral( "raccord" ), "brides" );
75+
mLayer->addFeature( ft1 );
76+
77+
QgsFeature ft2( mLayer->fields() );
78+
ft2.setAttribute( QStringLiteral( "pk" ), 11 );
79+
ft2.setAttribute( QStringLiteral( "material" ), "iron" );
80+
ft2.setAttribute( QStringLiteral( "diameter" ), 120 );
81+
ft2.setAttribute( QStringLiteral( "raccord" ), "sleeve" );
82+
mLayer->addFeature( ft2 );
83+
84+
QgsFeature ft3( mLayer->fields() );
85+
ft3.setAttribute( QStringLiteral( "pk" ), 12 );
86+
ft3.setAttribute( QStringLiteral( "material" ), "steel" );
87+
ft3.setAttribute( QStringLiteral( "diameter" ), 120 );
88+
ft3.setAttribute( QStringLiteral( "raccord" ), "collar" );
89+
mLayer->addFeature( ft3 );
90+
91+
mLayer->commitChanges();
92+
}
93+
94+
void TestQgsFeatureListComboBox::cleanup()
95+
{
96+
}
97+
98+
void TestQgsFeatureListComboBox::testSetGetLayer()
99+
{
100+
std::unique_ptr<QgsFeatureListComboBox> cb( new QgsFeatureListComboBox() );
101+
102+
Q_ASSERT( cb->sourceLayer() == nullptr );
103+
cb->setSourceLayer( mLayer.get() );
104+
QCOMPARE( cb->sourceLayer(), mLayer.get() );
105+
}
106+
107+
void TestQgsFeatureListComboBox::testSetGetForeignKey()
108+
{
109+
QgsFeatureListComboBox *cb = new QgsFeatureListComboBox();
110+
// std::unique_ptr<QgsFeatureListComboBox> cb( new QgsFeatureListComboBox() );
111+
112+
Q_ASSERT( cb->identifierValue().isNull() );
113+
114+
cb->setSourceLayer( mLayer.get() );
115+
cb->setDisplayExpression( "\"material\"" );
116+
cb->lineEdit()->setText( "ro" );
117+
emit cb->lineEdit()->textChanged( "ro" );
118+
Q_ASSERT( cb->identifierValue().isNull() );
119+
120+
waitForLoaded( cb );
121+
122+
Q_ASSERT( cb->identifierValue().isNull() );
123+
124+
cb->setIdentifierValue( 20 );
125+
QCOMPARE( cb->identifierValue(), QVariant( 20 ) );
126+
}
127+
128+
void TestQgsFeatureListComboBox::testAllowNull()
129+
{
130+
Q_ASSERT( false );
131+
// Note to self: implement this!
132+
}
133+
134+
void TestQgsFeatureListComboBox::waitForLoaded( QgsFeatureListComboBox *cb )
135+
{
136+
QgsFeatureFilterModel *model = qobject_cast<QgsFeatureFilterModel *>( cb->model() );
137+
138+
// Wait
139+
while ( model->isLoading() )
140+
{}
141+
}
142+
143+
QGSTEST_MAIN( TestQgsFeatureListComboBox )
144+
#include "testqgsfeaturelistcombobox.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.