Skip to content

Commit

Permalink
Expand QgsField/QgsFields tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 1, 2015
1 parent 2fb6c10 commit d107603
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tests/src/core/testqgsfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class TestQgsField: public QObject
void asVariant(); //test conversion to and from a QVariant
void displayString();
void convertCompatible();
void dataStream();

private:
};
Expand Down Expand Up @@ -284,5 +285,28 @@ void TestQgsField::convertCompatible()
QCOMPARE( stringVar.toString(), QString( "lon" ) );
}

void TestQgsField::dataStream()
{
QgsField original;
original.setName( "name" );
original.setType( QVariant::Int );
original.setLength( 5 );
original.setPrecision( 2 );
original.setTypeName( "typename1" );
original.setComment( "comment1" );

QByteArray ba;
QDataStream ds( &ba, QIODevice::ReadWrite );;
ds << original;

QgsField result;
ds.device()->seek( 0 );
ds >> result;

QCOMPARE( result, original );
QCOMPARE( result.typeName(), original.typeName() ); //typename is NOT required for equality
QCOMPARE( result.comment(), original.comment() ); //comment is NOT required for equality
}

QTEST_MAIN( TestQgsField )
#include "testqgsfield.moc"
67 changes: 67 additions & 0 deletions tests/src/core/testqgsfields.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class TestQgsFields: public QObject
void toList();
void allAttrsList();
void appendExpressionField();
void dataStream();
void field(); //test QgsFields::Field

private:
};
Expand Down Expand Up @@ -400,5 +402,70 @@ void TestQgsFields::appendExpressionField()
QCOMPARE( fields.fieldOriginIndex( 2 ), 5 );
}

void TestQgsFields::dataStream()
{
QgsField original1;
original1.setName( "name" );
original1.setType( QVariant::Int );
original1.setLength( 5 );
original1.setPrecision( 2 );
original1.setTypeName( "typename1" );
original1.setComment( "comment1" );

QgsField original2;
original2.setName( "next name" );
original2.setType( QVariant::Double );
original2.setLength( 15 );
original2.setPrecision( 3 );
original2.setTypeName( "double" );
original2.setComment( "comment for field 2" );

QgsFields originalFields;
originalFields.append( original1 );
originalFields.append( original2 );

QByteArray ba;
QDataStream ds( &ba, QIODevice::ReadWrite );;
ds << originalFields;

QgsFields resultFields;
ds.device()->seek( 0 );
ds >> resultFields;

QCOMPARE( resultFields, originalFields );
QCOMPARE( resultFields.field( 0 ).typeName(), originalFields.field( 0 ).typeName() ); //typename is NOT required for equality
QCOMPARE( resultFields.field( 0 ).comment(), originalFields.field( 0 ).comment() ); //comment is NOT required for equality
QCOMPARE( resultFields.field( 1 ).typeName(), originalFields.field( 1 ).typeName() );
QCOMPARE( resultFields.field( 1 ).comment(), originalFields.field( 1 ).comment() );
}

void TestQgsFields::field()
{
QgsField original;
original.setName( "name" );
original.setType( QVariant::Int );
original.setLength( 5 );
original.setPrecision( 2 );

//test constructors for QgsFields::Field
QgsFields::Field fieldConstructor1( original, QgsFields::OriginJoin, 5 );
QCOMPARE( fieldConstructor1.field, original );
QCOMPARE( fieldConstructor1.origin, QgsFields::OriginJoin );
QCOMPARE( fieldConstructor1.originIndex, 5 );

QgsFields::Field fieldConstructor2;
QCOMPARE( fieldConstructor2.origin, QgsFields::OriginUnknown );
QCOMPARE( fieldConstructor2.originIndex, -1 );

//test equality operators
QgsFields::Field field1( original, QgsFields::OriginJoin, 5 );
QgsFields::Field field2( original, QgsFields::OriginJoin, 5 );
QVERIFY( field1 == field2 );
QgsFields::Field field3( original, QgsFields::OriginEdit, 5 );
QVERIFY( field1 != field3 );
QgsFields::Field field4( original, QgsFields::OriginJoin, 6 );
QVERIFY( field1 != field4 );
}

QTEST_MAIN( TestQgsFields )
#include "testqgsfields.moc"

0 comments on commit d107603

Please sign in to comment.