void TestQgsField::displayString() { QgsField stringField( QStringLiteral( "string" ), QVariant::String, QStringLiteral( "string" ) ); //test string value QString test( QStringLiteral( "test string" ) ); QCOMPARE( stringField.displayString( test ), test ); //test NULL QSettings s; s.setValue( QStringLiteral( "qgis/nullValue" ), "TEST NULL" ); QVariant nullString = QVariant( QVariant::String ); QCOMPARE( stringField.displayString( nullString ), QString( "TEST NULL" ) ); //test int value QgsField intField( QStringLiteral( "int" ), QVariant::String, QStringLiteral( "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( QStringLiteral( "double" ), QVariant::Double, QStringLiteral( "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::displayString() { QgsField stringField( QStringLiteral( "string" ), QVariant::String, QStringLiteral( "string" ) ); //test string value QString test( QStringLiteral( "test string" ) ); QCOMPARE( stringField.displayString( test ), test ); //test NULL QgsApplication::setNullRepresentation( QStringLiteral( "TEST NULL" ) ); QVariant nullString = QVariant( QVariant::String ); QCOMPARE( stringField.displayString( nullString ), QString( "TEST NULL" ) ); //test int value QgsField intField( QStringLiteral( "int" ), QVariant::String, QStringLiteral( "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( QStringLiteral( "double" ), QVariant::Double, QStringLiteral( "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( QStringLiteral( "string" ), QVariant::String, QStringLiteral( "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( 1.25 ); QVERIFY( stringField.convertCompatible( doubleVar ) ); QCOMPARE( doubleVar.type(), QVariant::String ); QCOMPARE( doubleVar, QVariant( "1.25" ) ); QVariant nullDouble = QVariant( QVariant::Double ); QVERIFY( stringField.convertCompatible( nullDouble ) ); QCOMPARE( nullDouble.type(), QVariant::String ); QVERIFY( nullDouble.isNull() ); //test double QgsField doubleField( QStringLiteral( "double" ), QVariant::Double, QStringLiteral( "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( 1.25 ); QVERIFY( doubleField.convertCompatible( doubleVar ) ); QCOMPARE( doubleVar.type(), QVariant::Double ); QCOMPARE( doubleVar, QVariant( 1.25 ) ); nullDouble = QVariant( QVariant::Double ); QVERIFY( doubleField.convertCompatible( nullDouble ) ); QCOMPARE( nullDouble.type(), QVariant::Double ); QVERIFY( nullDouble.isNull() ); //test special rules //conversion of double to int QgsField intField( QStringLiteral( "int" ), QVariant::Int, QStringLiteral( "int" ) ); //small double, should be rounded QVariant smallDouble( 45.7 ); QVERIFY( intField.convertCompatible( smallDouble ) ); QCOMPARE( smallDouble.type(), QVariant::Int ); QCOMPARE( smallDouble, QVariant( 46 ) ); QVariant negativeSmallDouble( -9345.754534525235235 ); QVERIFY( intField.convertCompatible( negativeSmallDouble ) ); QCOMPARE( negativeSmallDouble.type(), QVariant::Int ); QCOMPARE( negativeSmallDouble, QVariant( -9346 ) ); //large double, cannot be converted QVariant largeDouble( 9999999999.99 ); QVERIFY( !intField.convertCompatible( largeDouble ) ); QCOMPARE( largeDouble.type(), QVariant::Int ); QVERIFY( largeDouble.isNull() ); //conversion of string double value to int QVariant notNumberString( "notanumber" ); QVERIFY( !intField.convertCompatible( notNumberString ) ); QCOMPARE( notNumberString.type(), QVariant::Int ); QVERIFY( notNumberString.isNull() ); //small double, should be rounded QVariant smallDoubleString( "45.7" ); QVERIFY( intField.convertCompatible( smallDoubleString ) ); QCOMPARE( smallDoubleString.type(), QVariant::Int ); QCOMPARE( smallDoubleString, QVariant( 46 ) ); QVariant negativeSmallDoubleString( "-9345.754534525235235" ); QVERIFY( intField.convertCompatible( negativeSmallDoubleString ) ); QCOMPARE( negativeSmallDoubleString.type(), QVariant::Int ); QCOMPARE( negativeSmallDoubleString, QVariant( -9346 ) ); //large double, cannot be converted QVariant largeDoubleString( "9999999999.99" ); QVERIFY( !intField.convertCompatible( largeDoubleString ) ); QCOMPARE( largeDoubleString.type(), QVariant::Int ); QVERIFY( largeDoubleString.isNull() ); //conversion of longlong to int QVariant longlong( 99999999999999999LL ); QVERIFY( !intField.convertCompatible( longlong ) ); QCOMPARE( longlong.type(), QVariant::Int ); QVERIFY( longlong.isNull() ); QVariant smallLonglong( 99LL ); QVERIFY( intField.convertCompatible( smallLonglong ) ); QCOMPARE( smallLonglong.type(), QVariant::Int ); QCOMPARE( smallLonglong, QVariant( 99 ) ); //conversion of longlong to longlong field QgsField longlongField( QStringLiteral( "long" ), QVariant::LongLong, QStringLiteral( "longlong" ) ); longlong = QVariant( 99999999999999999LL ); QVERIFY( longlongField.convertCompatible( longlong ) ); QCOMPARE( longlong.type(), QVariant::LongLong ); QCOMPARE( longlong, QVariant( 99999999999999999LL ) ); //double with precision QgsField doubleWithPrecField( QStringLiteral( "double" ), QVariant::Double, QStringLiteral( "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( QStringLiteral( "string" ), QVariant::String, QStringLiteral( "string" ), 3 ); stringVar = QVariant( "longstring" ); QVERIFY( !stringWithLen.convertCompatible( stringVar ) ); QCOMPARE( stringVar.type(), QVariant::String ); QCOMPARE( stringVar.toString(), QString( "lon" ) ); }
void TestQgsField::convertCompatible() { //test string field QgsField stringField( QStringLiteral( "string" ), QVariant::String, QStringLiteral( "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( 1.25 ); QVERIFY( stringField.convertCompatible( doubleVar ) ); QCOMPARE( doubleVar.type(), QVariant::String ); QCOMPARE( doubleVar, QVariant( "1.25" ) ); QVariant nullDouble = QVariant( QVariant::Double ); QVERIFY( stringField.convertCompatible( nullDouble ) ); QCOMPARE( nullDouble.type(), QVariant::String ); QVERIFY( nullDouble.isNull() ); //test double QgsField doubleField( QStringLiteral( "double" ), QVariant::Double, QStringLiteral( "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( 1.25 ); QVERIFY( doubleField.convertCompatible( doubleVar ) ); QCOMPARE( doubleVar.type(), QVariant::Double ); QCOMPARE( doubleVar, QVariant( 1.25 ) ); nullDouble = QVariant( QVariant::Double ); QVERIFY( doubleField.convertCompatible( nullDouble ) ); QCOMPARE( nullDouble.type(), QVariant::Double ); QVERIFY( nullDouble.isNull() ); //test special rules //conversion of double to int QgsField intField( QStringLiteral( "int" ), QVariant::Int, QStringLiteral( "int" ) ); //small double, should be rounded QVariant smallDouble( 45.7 ); QVERIFY( intField.convertCompatible( smallDouble ) ); QCOMPARE( smallDouble.type(), QVariant::Int ); QCOMPARE( smallDouble, QVariant( 46 ) ); QVariant negativeSmallDouble( -9345.754534525235235 ); QVERIFY( intField.convertCompatible( negativeSmallDouble ) ); QCOMPARE( negativeSmallDouble.type(), QVariant::Int ); QCOMPARE( negativeSmallDouble, QVariant( -9346 ) ); //large double, cannot be converted QVariant largeDouble( 9999999999.99 ); QVERIFY( !intField.convertCompatible( largeDouble ) ); QCOMPARE( largeDouble.type(), QVariant::Int ); QVERIFY( largeDouble.isNull() ); //conversion of string double value to int QVariant notNumberString( "notanumber" ); QVERIFY( !intField.convertCompatible( notNumberString ) ); QCOMPARE( notNumberString.type(), QVariant::Int ); QVERIFY( notNumberString.isNull() ); //small double, should be rounded QVariant smallDoubleString( "45.7" ); QVERIFY( intField.convertCompatible( smallDoubleString ) ); QCOMPARE( smallDoubleString.type(), QVariant::Int ); QCOMPARE( smallDoubleString, QVariant( 46 ) ); QVariant negativeSmallDoubleString( "-9345.754534525235235" ); QVERIFY( intField.convertCompatible( negativeSmallDoubleString ) ); QCOMPARE( negativeSmallDoubleString.type(), QVariant::Int ); QCOMPARE( negativeSmallDoubleString, QVariant( -9346 ) ); //large double, cannot be converted QVariant largeDoubleString( "9999999999.99" ); QVERIFY( !intField.convertCompatible( largeDoubleString ) ); QCOMPARE( largeDoubleString.type(), QVariant::Int ); QVERIFY( largeDoubleString.isNull() ); //conversion of longlong to int QVariant longlong( 99999999999999999LL ); QVERIFY( !intField.convertCompatible( longlong ) ); QCOMPARE( longlong.type(), QVariant::Int ); QVERIFY( longlong.isNull() ); QVariant smallLonglong( 99LL ); QVERIFY( intField.convertCompatible( smallLonglong ) ); QCOMPARE( smallLonglong.type(), QVariant::Int ); QCOMPARE( smallLonglong, QVariant( 99 ) ); //conversion of longlong to longlong field QgsField longlongField( QStringLiteral( "long" ), QVariant::LongLong, QStringLiteral( "longlong" ) ); longlong = QVariant( 99999999999999999LL ); QVERIFY( longlongField.convertCompatible( longlong ) ); QCOMPARE( longlong.type(), QVariant::LongLong ); QCOMPARE( longlong, QVariant( 99999999999999999LL ) ); //string representation of an int QVariant stringInt( "123456" ); QVERIFY( intField.convertCompatible( stringInt ) ); QCOMPARE( stringInt.type(), QVariant::Int ); QCOMPARE( stringInt, QVariant( 123456 ) ); // now with group separator for english locale stringInt = QVariant( "123,456" ); QVERIFY( intField.convertCompatible( stringInt ) ); QCOMPARE( stringInt.type(), QVariant::Int ); QCOMPARE( stringInt, QVariant( "123456" ) ); //string representation of a longlong QVariant stringLong( "99999999999999999" ); QVERIFY( longlongField.convertCompatible( stringLong ) ); QCOMPARE( stringLong.type(), QVariant::LongLong ); QCOMPARE( stringLong, QVariant( 99999999999999999LL ) ); // now with group separator for english locale stringLong = QVariant( "99,999,999,999,999,999" ); QVERIFY( longlongField.convertCompatible( stringLong ) ); QCOMPARE( stringLong.type(), QVariant::LongLong ); QCOMPARE( stringLong, QVariant( 99999999999999999LL ) ); //string representation of a double QVariant stringDouble( "123456.012345" ); QVERIFY( doubleField.convertCompatible( stringDouble ) ); QCOMPARE( stringDouble.type(), QVariant::Double ); QCOMPARE( stringDouble, QVariant( 123456.012345 ) ); // now with group separator for english locale stringDouble = QVariant( "1,223,456.012345" ); QVERIFY( doubleField.convertCompatible( stringDouble ) ); QCOMPARE( stringDouble.type(), QVariant::Double ); QCOMPARE( stringDouble, QVariant( 1223456.012345 ) ); // This should not convert stringDouble = QVariant( "1.223.456,012345" ); QVERIFY( ! doubleField.convertCompatible( stringDouble ) ); //double with precision QgsField doubleWithPrecField( QStringLiteral( "double" ), QVariant::Double, QStringLiteral( "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( QStringLiteral( "string" ), QVariant::String, QStringLiteral( "string" ), 3 ); stringVar = QVariant( "longstring" ); QVERIFY( !stringWithLen.convertCompatible( stringVar ) ); QCOMPARE( stringVar.type(), QVariant::String ); QCOMPARE( stringVar.toString(), QString( "lon" ) ); ///////////////////////////////////////////////////////// // German locale tests //double with ',' as decimal separator for German locale QLocale::setDefault( QLocale::German ); QVariant doubleCommaVar( "1,2345" ); QVERIFY( doubleField.convertCompatible( doubleCommaVar ) ); QCOMPARE( doubleCommaVar.type(), QVariant::Double ); QCOMPARE( doubleCommaVar.toString(), QString( "1.2345" ) ); //string representation of an int stringInt = QVariant( "123456" ); QVERIFY( intField.convertCompatible( stringInt ) ); QCOMPARE( stringInt.type(), QVariant::Int ); QCOMPARE( stringInt, QVariant( 123456 ) ); // now with group separator for german locale stringInt = QVariant( "123.456" ); QVERIFY( intField.convertCompatible( stringInt ) ); QCOMPARE( stringInt.type(), QVariant::Int ); QCOMPARE( stringInt, QVariant( "123456" ) ); //string representation of a longlong stringLong = QVariant( "99999999999999999" ); QVERIFY( longlongField.convertCompatible( stringLong ) ); QCOMPARE( stringLong.type(), QVariant::LongLong ); QCOMPARE( stringLong, QVariant( 99999999999999999LL ) ); // now with group separator for german locale stringLong = QVariant( "99.999.999.999.999.999" ); QVERIFY( longlongField.convertCompatible( stringLong ) ); QCOMPARE( stringLong.type(), QVariant::LongLong ); QCOMPARE( stringLong, QVariant( 99999999999999999LL ) ); //string representation of a double stringDouble = QVariant( "123456,012345" ); QVERIFY( doubleField.convertCompatible( stringDouble ) ); QCOMPARE( stringDouble.type(), QVariant::Double ); QCOMPARE( stringDouble, QVariant( 123456.012345 ) ); // For doubles we also want to accept dot as a decimal point stringDouble = QVariant( "123456.012345" ); QVERIFY( doubleField.convertCompatible( stringDouble ) ); QCOMPARE( stringDouble.type(), QVariant::Double ); QCOMPARE( stringDouble, QVariant( 123456.012345 ) ); // now with group separator for german locale stringDouble = QVariant( "1.223.456,012345" ); QVERIFY( doubleField.convertCompatible( stringDouble ) ); QCOMPARE( stringDouble.type(), QVariant::Double ); QCOMPARE( stringDouble, QVariant( 1223456.012345 ) ); // Be are good citizens and we also accept english locale stringDouble = QVariant( "1,223,456.012345" ); QVERIFY( doubleField.convertCompatible( stringDouble ) ); QCOMPARE( stringDouble.type(), QVariant::Double ); QCOMPARE( stringDouble, QVariant( 1223456.012345 ) ); // Test that wrongly formatted decimal separator are also accepted QLocale::setDefault( QLocale::German ); stringDouble = QVariant( "12.23.456,012345" ); QVERIFY( doubleField.convertCompatible( stringDouble ) ); QCOMPARE( stringDouble.type(), QVariant::Double ); QCOMPARE( stringDouble, QVariant( 1223456.012345 ) ); }
void TestQgsField::displayString() { QgsField stringField( QStringLiteral( "string" ), QVariant::String, QStringLiteral( "string" ) ); //test string value QString test( QStringLiteral( "test string" ) ); QCOMPARE( stringField.displayString( test ), test ); //test NULL QgsApplication::setNullRepresentation( QStringLiteral( "TEST NULL" ) ); QVariant nullString = QVariant( QVariant::String ); QCOMPARE( stringField.displayString( nullString ), QString( "TEST NULL" ) ); //test int value in string type QgsField intField( QStringLiteral( "int" ), QVariant::String, QStringLiteral( "int" ) ); QCOMPARE( intField.displayString( 5 ), QString( "5" ) ); QCOMPARE( intField.displayString( 599999898999LL ), QString( "599999898999" ) ); //test int value in int type QgsField intField2( QStringLiteral( "int" ), QVariant::Int, QStringLiteral( "int" ) ); QCOMPARE( intField2.displayString( 5 ), QString( "5" ) ); QCOMPARE( intField2.displayString( 599999898999LL ), QString( "599,999,898,999" ) ); //test long type QgsField longField( QStringLiteral( "long" ), QVariant::LongLong, QStringLiteral( "longlong" ) ); QCOMPARE( longField.displayString( 5 ), QString( "5" ) ); QCOMPARE( longField.displayString( 599999898999LL ), QString( "599,999,898,999" ) ); //test NULL int QVariant nullInt = QVariant( QVariant::Int ); QCOMPARE( intField.displayString( nullInt ), QString( "TEST NULL" ) ); //test double value QgsField doubleField( QStringLiteral( "double" ), QVariant::Double, QStringLiteral( "double" ), 10, 3 ); QCOMPARE( doubleField.displayString( 5.005005 ), QString( "5.005" ) ); QgsField doubleFieldNoPrec( QStringLiteral( "double" ), QVariant::Double, QStringLiteral( "double" ), 10 ); QCOMPARE( doubleFieldNoPrec.displayString( 5.005005 ), QString( "5.005005" ) ); QCOMPARE( doubleFieldNoPrec.displayString( 5.005005005 ), QString( "5.005005005" ) ); QCOMPARE( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator, QLocale::NumberOption::DefaultNumberOptions ); QCOMPARE( doubleFieldNoPrec.displayString( 599999898999.0 ), QString( "599,999,898,999" ) ); //test NULL double QVariant nullDouble = QVariant( QVariant::Double ); QCOMPARE( doubleField.displayString( nullDouble ), QString( "TEST NULL" ) ); //test double value with German locale QLocale::setDefault( QLocale::German ); QCOMPARE( doubleField.displayString( 5.005005 ), QString( "5,005" ) ); QCOMPARE( doubleFieldNoPrec.displayString( 5.005005 ), QString( "5,005005" ) ); QCOMPARE( doubleFieldNoPrec.displayString( 5.005005005 ), QString( "5,005005005" ) ); QCOMPARE( doubleFieldNoPrec.displayString( 599999898999.0 ), QString( "599.999.898.999" ) ); QCOMPARE( doubleFieldNoPrec.displayString( 5999.123456 ), QString( "5.999,123456" ) ); //test value with custom German locale (OmitGroupSeparator) QLocale customGerman( QLocale::German ); customGerman.setNumberOptions( QLocale::NumberOption::OmitGroupSeparator ); QLocale::setDefault( customGerman ); QCOMPARE( doubleField.displayString( 5.005005 ), QString( "5,005" ) ); QCOMPARE( doubleFieldNoPrec.displayString( 5.005005 ), QString( "5,005005" ) ); QCOMPARE( doubleFieldNoPrec.displayString( 5.005005005 ), QString( "5,005005005" ) ); QCOMPARE( doubleFieldNoPrec.displayString( 599999898999.0 ), QString( "599999898999" ) ); QCOMPARE( doubleFieldNoPrec.displayString( 5999.123456 ), QString( "5999,123456" ) ); //test int value in int type with custom German locale (OmitGroupSeparator) QCOMPARE( intField2.displayString( 5 ), QString( "5" ) ); QCOMPARE( intField2.displayString( 599999898999LL ), QString( "599999898999" ) ); //test long type with custom German locale (OmitGroupSeparator) QCOMPARE( longField.displayString( 5 ), QString( "5" ) ); QCOMPARE( longField.displayString( 599999898999LL ), QString( "599999898999" ) ); //test value with custom english locale (OmitGroupSeparator) QLocale customEnglish( QLocale::English ); customEnglish.setNumberOptions( QLocale::NumberOption::OmitGroupSeparator ); QLocale::setDefault( customEnglish ); QCOMPARE( doubleField.displayString( 5.005005 ), QString( "5.005" ) ); QCOMPARE( doubleFieldNoPrec.displayString( 5.005005 ), QString( "5.005005" ) ); QCOMPARE( doubleFieldNoPrec.displayString( 5.005005005 ), QString( "5.005005005" ) ); QCOMPARE( doubleFieldNoPrec.displayString( 599999898999.0 ), QString( "599999898999" ) ); QCOMPARE( doubleFieldNoPrec.displayString( 5999.123456 ), QString( "5999.123456" ) ); //test int value in int type with custom english locale (OmitGroupSeparator) QCOMPARE( intField2.displayString( 5 ), QString( "5" ) ); QCOMPARE( intField2.displayString( 599999898999LL ), QString( "599999898999" ) ); //test long type with custom english locale (OmitGroupSeparator) QCOMPARE( longField.displayString( 5 ), QString( "5" ) ); QCOMPARE( longField.displayString( 599999898999LL ), QString( "599999898999" ) ); // binary field QgsField binaryField( QStringLiteral( "binary" ), QVariant::ByteArray, QStringLiteral( "Binary" ) ); QString testBAString( QStringLiteral( "test string" ) ); QByteArray testBA( testBAString.toLocal8Bit() ); QCOMPARE( binaryField.displayString( testBA ), QStringLiteral( "BLOB" ) ); }
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" ) ); }