@@ -32,7 +32,8 @@ class TestQgsFeature: public QObject
32
32
void cleanupTestCase ();// will be called after the last testfunction was executed.
33
33
void init ();// will be called before each testfunction is executed.
34
34
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
36
37
void copy ();// test cpy destruction (double delete)
37
38
void assignment ();
38
39
void gettersSetters (); // test getters and setters
@@ -41,6 +42,7 @@ class TestQgsFeature: public QObject
41
42
void asVariant (); // test conversion to and from a QVariant
42
43
void fields ();
43
44
void attributeUsingField ();
45
+ void dataStream ();
44
46
45
47
46
48
private:
@@ -83,6 +85,40 @@ void TestQgsFeature::cleanup()
83
85
84
86
}
85
87
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
+
86
122
void TestQgsFeature::create ()
87
123
{
88
124
// test constructors
@@ -298,6 +334,13 @@ void TestQgsFeature::fields()
298
334
QgsFeature copy ( original );
299
335
QCOMPARE ( *copy.fields (), *original.fields () );
300
336
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
+
301
344
// test detach
302
345
QgsFields newFields ( mFields );
303
346
newFields.remove ( 2 );
@@ -370,5 +413,38 @@ void TestQgsFeature::attributeUsingField()
370
413
QVERIFY ( feature.attribute ( " field1" ).isValid () );
371
414
}
372
415
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
+
373
449
QTEST_MAIN ( TestQgsFeature )
374
450
#include " testqgsfeature.moc"
0 commit comments