Skip to content

Commit 75bb5c4

Browse files
committed
Add test for QgsVectorLayerUtils::getFeatureSource
1 parent 44ce897 commit 75bb5c4

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

tests/src/core/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ SET(TESTS
192192
testqgsvectorlayercache.cpp
193193
testqgsvectorlayerjoinbuffer.cpp
194194
testqgsvectorlayer.cpp
195+
testqgsvectorlayerutils.cpp
195196
testziplayer.cpp
196197
testqgsmeshlayer.cpp
197198
testqgsmeshlayerrenderer.cpp
+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/***************************************************************************
2+
test_template.cpp
3+
--------------------------------------
4+
Date : Sun Sep 16 12:22:23 AKDT 2007
5+
Copyright : (C) 2007 by Gary E. Sherman
6+
Email : sherman at mrcc dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#include "qgstest.h"
16+
17+
#include "qgsvectorlayerutils.h"
18+
19+
/**
20+
* \ingroup UnitTests
21+
* This is a unit test for the vector layer class.
22+
*/
23+
class TestQgsVectorLayerUtils : public QObject
24+
{
25+
Q_OBJECT
26+
public:
27+
TestQgsVectorLayerUtils() = default;
28+
29+
private:
30+
bool mTestHasError = false ;
31+
QgsMapLayer *mpPointsLayer = nullptr;
32+
QgsMapLayer *mpLinesLayer = nullptr;
33+
QgsMapLayer *mpPolysLayer = nullptr;
34+
QgsVectorLayer *mpNonSpatialLayer = nullptr;
35+
QString mTestDataDir;
36+
QString mReport;
37+
38+
private slots:
39+
40+
void initTestCase(); // will be called before the first testfunction is executed.
41+
void cleanupTestCase(); // will be called after the last testfunction was executed.
42+
void init() {} // will be called before each testfunction is executed.
43+
void cleanup() {} // will be called after every testfunction.
44+
45+
void testGetFeatureSource();
46+
};
47+
48+
void TestQgsVectorLayerUtils::initTestCase()
49+
{
50+
QgsApplication::init();
51+
QgsApplication::initQgis();
52+
QgsApplication::showSettings();
53+
54+
}
55+
56+
void TestQgsVectorLayerUtils::cleanupTestCase()
57+
{
58+
QgsApplication::exitQgis();
59+
}
60+
61+
class FeatureFetcher : public QThread
62+
{
63+
Q_OBJECT
64+
65+
public:
66+
FeatureFetcher( QPointer<QgsVectorLayer> layer )
67+
: mLayer( layer )
68+
{
69+
}
70+
71+
void run() override
72+
{
73+
QgsFeature feat;
74+
QgsVectorLayerUtils::getFeatureSource( mLayer ).get()->getFeatures().nextFeature( feat );
75+
emit resultReady( feat.attribute( QStringLiteral( "col1" ) ) );
76+
}
77+
78+
signals:
79+
void resultReady( const QVariant &attribute );
80+
81+
private:
82+
QPoinst<QgsVectorLayer> mLayer;
83+
};
84+
85+
86+
void TestQgsVectorLayerUtils::testGetFeatureSource()
87+
{
88+
QgsVectorLayer *vl = new QgsVectorLayer( QStringLiteral( "Point?field=col1:integer" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) );
89+
vl->startEditing();
90+
QgsFeature f1( vl->fields(), 1 );
91+
f1.setAttribute( QStringLiteral( "col1" ), 10 );
92+
vl->addFeature( f1 );
93+
94+
QPointer<QgsVectorLayer> vlPtr( vl );
95+
96+
QgsFeature feat;
97+
QgsVectorLayerUtils::getFeatureSource( vlPtr ).get()->getFeatures().nextFeature( feat );
98+
QCOMPARE( feat.attribute( QStringLiteral( "col1" ) ).toInt(), 10 );
99+
100+
FeatureFetcher *thread = new FeatureFetcher();
101+
102+
bool finished = false;
103+
QVariant result;
104+
connect( thread, &FeatureFetcher::resultReady, this, [finished, result]( const QVariant & res )
105+
{
106+
finished = true;
107+
result = res;
108+
} );
109+
connect( thread, &QThread::finished, thread, &QThread::deleteLater );
110+
111+
thread->start();
112+
while ( !finished )
113+
QCoreApplication::processEvents();
114+
QCOMPARE( result.toInt(), 10 );
115+
116+
thread->quit();
117+
}
118+
119+
QGSTEST_MAIN( TestQgsVectorLayerUtils )
120+
#include "testqgsvectorlayerutils.moc"

0 commit comments

Comments
 (0)