Skip to content

Commit 47e3e20

Browse files
committedAug 3, 2015
Boost QgsFeature test coverage to 100%
1 parent f7b4315 commit 47e3e20

File tree

1 file changed

+77
-1
lines changed

1 file changed

+77
-1
lines changed
 

‎tests/src/core/testqgsfeature.cpp

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class TestQgsFeature: public QObject
3232
void cleanupTestCase();// will be called after the last testfunction was executed.
3333
void init();// will be called before each testfunction is executed.
3434
void cleanup();// will be called after every testfunction.
35-
void create();//test creating a data defined container
35+
void attributesTest(); //test QgsAttributes
36+
void create();//test creating a feature
3637
void copy();// test cpy destruction (double delete)
3738
void assignment();
3839
void gettersSetters(); //test getters and setters
@@ -41,6 +42,7 @@ class TestQgsFeature: public QObject
4142
void asVariant(); //test conversion to and from a QVariant
4243
void fields();
4344
void attributeUsingField();
45+
void dataStream();
4446

4547

4648
private:
@@ -83,6 +85,40 @@ void TestQgsFeature::cleanup()
8385

8486
}
8587

88+
void TestQgsFeature::attributesTest()
89+
{
90+
QgsAttributes attr1;
91+
attr1 << QVariant( 5 ) << QVariant( 7 ) << QVariant( "val" );
92+
QgsAttributes attr2;
93+
attr2 << QVariant( 5 ) << QVariant( 7 ) << QVariant( "val" );
94+
QCOMPARE( attr1, attr2 );
95+
96+
//different size
97+
QgsAttributes attr3;
98+
attr3 << QVariant( 5 ) << QVariant( 7 );
99+
QVERIFY( attr1 != attr3 );
100+
101+
//different value
102+
QgsAttributes attr4;
103+
attr4 << QVariant( 5 ) << QVariant( 10 ) << QVariant( "val" );
104+
QVERIFY( attr1 != attr4 );
105+
106+
//null value
107+
QVariant nullDouble( QVariant::Double );
108+
QgsAttributes attr5;
109+
attr5 << QVariant( 5 ) << nullDouble << QVariant( "val" );
110+
QVERIFY( attr1 != attr5 );
111+
112+
//null compared to 0 (see note at QgsAttributes::operator== )
113+
QgsAttributes attr6;
114+
attr6 << QVariant( 5 ) << QVariant( 0.0 ) << QVariant( "val" );
115+
QVERIFY( attr5 != attr6 );
116+
117+
//constructed with size
118+
QgsAttributes attr7( 5 );
119+
QCOMPARE( attr7.size(), 5 );
120+
}
121+
86122
void TestQgsFeature::create()
87123
{
88124
//test constructors
@@ -298,6 +334,13 @@ void TestQgsFeature::fields()
298334
QgsFeature copy( original );
299335
QCOMPARE( *copy.fields(), *original.fields() );
300336

337+
Q_NOWARN_DEPRECATED_PUSH
338+
//test deprecated setFields( QgsFields* ) method
339+
QgsFeature feature2;
340+
feature2.setFields( &mFields );
341+
QCOMPARE( *feature2.fields(), mFields );
342+
Q_NOWARN_DEPRECATED_POP
343+
301344
//test detach
302345
QgsFields newFields( mFields );
303346
newFields.remove( 2 );
@@ -370,5 +413,38 @@ void TestQgsFeature::attributeUsingField()
370413
QVERIFY( feature.attribute( "field1" ).isValid() );
371414
}
372415

416+
void TestQgsFeature::dataStream()
417+
{
418+
QgsFeature originalFeature;
419+
originalFeature.setGeometry( new QgsGeometry( *mGeometry.data() ) );
420+
421+
QByteArray ba;
422+
QDataStream ds( &ba, QIODevice::ReadWrite );;
423+
ds << originalFeature;
424+
425+
QgsFeature resultFeature;
426+
ds.device()->seek( 0 );
427+
ds >> resultFeature;
428+
429+
QCOMPARE( resultFeature.id(), originalFeature.id() );
430+
QCOMPARE( resultFeature.attributes(), originalFeature.attributes() );
431+
QCOMPARE( *resultFeature.constGeometry()->asWkb(), *originalFeature.constGeometry()->asWkb() );
432+
QCOMPARE( resultFeature.isValid(), originalFeature.isValid() );
433+
434+
//also test with feature without geometry
435+
originalFeature.setGeometry( new QgsGeometry() );
436+
QByteArray ba2;
437+
QDataStream ds2( &ba2, QIODevice::ReadWrite );;
438+
ds2 << originalFeature;
439+
440+
ds2.device()->seek( 0 );
441+
ds2 >> resultFeature;
442+
443+
QCOMPARE( resultFeature.id(), originalFeature.id() );
444+
QCOMPARE( resultFeature.attributes(), originalFeature.attributes() );
445+
QVERIFY( resultFeature.constGeometry()->isEmpty() );
446+
QCOMPARE( resultFeature.isValid(), originalFeature.isValid() );
447+
}
448+
373449
QTEST_MAIN( TestQgsFeature )
374450
#include "testqgsfeature.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.