Skip to content
Permalink
Browse files

Boost test coverage of QgsField / QgsFields

  • Loading branch information
nyalldawson committed May 13, 2015
1 parent 03da1fc commit d955b0392432592d4e32897aee61b23218efceba
Showing with 279 additions and 1 deletion.
  1. +125 −1 tests/src/core/testqgsfield.cpp
  2. +154 −0 tests/src/core/testqgsfields.cpp
@@ -36,12 +36,18 @@ class TestQgsField: public QObject
void gettersSetters(); //test getters and setters
void equality(); //test equality operators
void asVariant(); //test conversion to and from a QVariant
void displayString();
void convertCompatible();

private:
};

void TestQgsField::initTestCase()
{

// Set up the QSettings environment
QCoreApplication::setOrganizationName( "QGIS" );
QCoreApplication::setOrganizationDomain( "qgis.org" );
QCoreApplication::setApplicationName( "QGIS-TEST" );
}

void TestQgsField::cleanupTestCase()
@@ -160,5 +166,123 @@ void TestQgsField::asVariant()
QCOMPARE( fromVar, original );
}

void TestQgsField::displayString()
{
QgsField stringField( "string", QVariant::String, "string" );

//test string value
QString test( "test string" );
QCOMPARE( stringField.displayString( test ), test );

//test NULL
QSettings s;
s.setValue( "qgis/nullValue", "TEST NULL" );
QVariant nullString = QVariant( QVariant::String );
QCOMPARE( stringField.displayString( nullString ), QString( "TEST NULL" ) );

//test int value
QgsField intField( "int", QVariant::String, "int" );
QCOMPARE( intField.displayString( 5 ), QString( "5" ) );
//test NULL int
QVariant nullInt = QVariant( QVariant::Int );
QCOMPARE( intField.displayString( nullInt ), QString( "TEST NULL" ) );

//test double value
QgsField doubleField( "double", QVariant::Double, "double", 10, 3 );
QCOMPARE( doubleField.displayString( 5.005005 ), QString( "5.005" ) );
//test NULL double
QVariant nullDouble = QVariant( QVariant::Double );
QCOMPARE( doubleField.displayString( nullDouble ), QString( "TEST NULL" ) );

}

void TestQgsField::convertCompatible()
{
//test string field
QgsField stringField( "string", QVariant::String, "string" );

QVariant stringVar( "test string" );
QVERIFY( stringField.convertCompatible( stringVar ) );
QCOMPARE( stringVar.toString( ), QString( "test string" ) );
QVariant nullString = QVariant( QVariant::String );
QVERIFY( stringField.convertCompatible( nullString ) );
QCOMPARE( nullString.type(), QVariant::String );
QVERIFY( nullString.isNull() );
QVariant intVar( 5 );
QVERIFY( stringField.convertCompatible( intVar ) );
QCOMPARE( intVar.type(), QVariant::String );
QCOMPARE( intVar, QVariant( "5" ) );
QVariant nullInt = QVariant( QVariant::Int );
QVERIFY( stringField.convertCompatible( nullInt ) );
QCOMPARE( nullInt.type(), QVariant::String );
QVERIFY( nullInt.isNull() );
QVariant doubleVar( 9.7 );
QVERIFY( stringField.convertCompatible( doubleVar ) );
QCOMPARE( doubleVar.type(), QVariant::String );
QCOMPARE( doubleVar, QVariant( "9.7" ) );
QVariant nullDouble = QVariant( QVariant::Double );
QVERIFY( stringField.convertCompatible( nullDouble ) );
QCOMPARE( nullDouble.type(), QVariant::String );
QVERIFY( nullDouble.isNull() );

//test double
QgsField doubleField( "double", QVariant::Double, "double" );

stringVar = QVariant( "test string" );
QVERIFY( !doubleField.convertCompatible( stringVar ) );
QCOMPARE( stringVar.type(), QVariant::Double );
QVERIFY( stringVar.isNull( ) );
nullString = QVariant( QVariant::String );
QVERIFY( doubleField.convertCompatible( nullString ) );
QCOMPARE( nullString.type(), QVariant::Double );
QVERIFY( nullString.isNull() );
intVar = QVariant( 5 );
QVERIFY( doubleField.convertCompatible( intVar ) );
QCOMPARE( intVar.type(), QVariant::Double );
QCOMPARE( intVar, QVariant( 5.0 ) );
nullInt = QVariant( QVariant::Int );
QVERIFY( doubleField.convertCompatible( nullInt ) );
QCOMPARE( nullInt.type(), QVariant::Double );
QVERIFY( nullInt.isNull() );
doubleVar = QVariant( 9.7 );
QVERIFY( doubleField.convertCompatible( doubleVar ) );
QCOMPARE( doubleVar.type(), QVariant::Double );
QCOMPARE( doubleVar, QVariant( 9.7 ) );
nullDouble = QVariant( QVariant::Double );
QVERIFY( doubleField.convertCompatible( nullDouble ) );
QCOMPARE( nullDouble.type(), QVariant::Double );
QVERIFY( nullDouble.isNull() );

//test special rules

//conversion of longlong to int
QgsField intField( "int", QVariant::Int, "int" );
QVariant longlong( 99999999999999999LL );
QVERIFY( !intField.convertCompatible( longlong ) );
QCOMPARE( longlong.type(), QVariant::Int );
QVERIFY( longlong.isNull( ) );
//conversion of longlong to longlong field
QgsField longlongField( "long", QVariant::LongLong, "longlong" );
longlong = QVariant( 99999999999999999LL );
QVERIFY( longlongField.convertCompatible( longlong ) );
QCOMPARE( longlong.type(), QVariant::LongLong );
QCOMPARE( longlong, QVariant( 99999999999999999LL ) );

//double with precision
QgsField doubleWithPrecField( "double", QVariant::Double, "double", 10, 3 );
doubleVar = QVariant( 10.12345678 );
//note - this returns true!
QVERIFY( doubleWithPrecField.convertCompatible( doubleVar ) );
QCOMPARE( doubleVar.type(), QVariant::Double );
QCOMPARE( doubleVar.toDouble(), 10.123 );

//truncating string length
QgsField stringWithLen( "string", QVariant::String, "string", 3 );
stringVar = QVariant( "longstring" );
QVERIFY( !stringWithLen.convertCompatible( stringVar ) );
QCOMPARE( stringVar.type(), QVariant::String );
QCOMPARE( stringVar.toString(), QString( "lon" ) );
}

QTEST_MAIN( TestQgsField )
#include "testqgsfield.moc"
@@ -35,6 +35,17 @@ class TestQgsFields: public QObject
void assignment();
void equality(); //test equality operators
void asVariant(); //test conversion to and from a QVariant
void clear();
void exists();
void count();
void isEmpty();
void remove();
void extend();
void byIndex();
void byName();
void fieldOrigin();
void fieldOriginIndex();

private:
};

@@ -144,5 +155,148 @@ void TestQgsFields::asVariant()
QCOMPARE( fromVar, original );
}

void TestQgsFields::clear()
{
QgsFields original;
QgsField field( "testfield" );
original.append( field );
QCOMPARE( original.count(), 1 );
QgsFields copy( original );

copy.clear();
QCOMPARE( copy.count(), 0 );
QCOMPARE( original.count(), 1 );
}

void TestQgsFields::exists()
{
QgsFields fields;
QgsField field( "testfield" );
fields.append( field );

QVERIFY( !fields.exists( -1 ) );
QVERIFY( !fields.exists( 1 ) );
QVERIFY( fields.exists( 0 ) );
}

void TestQgsFields::count()
{
QgsFields fields;
QCOMPARE( fields.count(), 0 );
QCOMPARE( fields.size(), 0 );

QgsField field( "testfield" );
fields.append( field );
QCOMPARE( fields.count(), 1 );
QCOMPARE( fields.size(), 1 );

QgsField field2( "testfield2" );
fields.append( field2 );
QCOMPARE( fields.count(), 2 );
QCOMPARE( fields.size(), 2 );
}

void TestQgsFields::isEmpty()
{
QgsFields fields;
QVERIFY( fields.isEmpty() );

QgsField field( "testfield" );
fields.append( field );
QVERIFY( !fields.isEmpty() );
}

void TestQgsFields::remove()
{
QgsFields fields;

//test for no crash
fields.remove( 1 );

QgsField field( "testfield" );
fields.append( field );
QgsField field2( "testfield2" );
fields.append( field2 );

//test for no crash
fields.remove( -1 );
fields.remove( 5 );

//remove valid field
fields.remove( 0 );
QCOMPARE( fields.count(), 1 );
QCOMPARE( fields.at( 0 ).name(), QString( "testfield2" ) );
}

void TestQgsFields::extend()
{
QgsFields destination;
QgsField field( "testfield" );
destination.append( field );
QgsField field2( "testfield2" );
destination.append( field2 );

QgsFields source;
QgsField field3( "testfield3" );
source.append( field3, QgsFields::OriginJoin, 5 );
QgsField field4( "testfield4" );
source.append( field4 );

QCOMPARE( destination.count(), 2 );
destination.extend( source );
QCOMPARE( destination.count(), 4 );
QCOMPARE( destination.at( 2 ), field3 );
QCOMPARE( destination.at( 3 ), field4 );
}

void TestQgsFields::byIndex()
{
QgsFields fields;
QgsField field( "testfield" );
fields.append( field );
QgsField field2( "testfield2" );
fields.append( field2 );

QCOMPARE( fields[0], field );
QCOMPARE( fields[1], field2 );

const QgsFields& constFields = fields;
QCOMPARE( constFields[0], field );
QCOMPARE( constFields[1], field2 );
QCOMPARE( constFields.at( 0 ), field );
QCOMPARE( constFields.at( 1 ), field2 );
QCOMPARE( constFields.field( 0 ), field );
QCOMPARE( constFields.field( 1 ), field2 );
}

void TestQgsFields::byName()
{
QgsFields fields;
QgsField field( "testfield" );
fields.append( field );
QgsField field2( "testfield2" );
fields.append( field2 );

QCOMPARE( fields.field( "testfield" ), field );
QCOMPARE( fields.field( "testfield2" ), field2 );
}

void TestQgsFields::fieldOrigin()
{
QgsFields fields;
QgsField field( QString( "testfield" ) );
fields.append( field , QgsFields::OriginJoin );
QgsField field2( QString( "testfield2" ) );
fields.append( field2, QgsFields::OriginExpression );

QCOMPARE( fields.fieldOrigin( 0 ), QgsFields::OriginJoin );
QCOMPARE( fields.fieldOrigin( 1 ), QgsFields::OriginExpression );
}

void TestQgsFields::fieldOriginIndex()
{

}

QTEST_MAIN( TestQgsFields )
#include "testqgsfields.moc"

0 comments on commit d955b03

Please sign in to comment.
You can’t perform that action at this time.