Skip to content

Commit

Permalink
Modify tests for #44160 milliseconds
Browse files Browse the repository at this point in the history
  • Loading branch information
Joonalai authored and nyalldawson committed Sep 17, 2021
1 parent 68875f2 commit 606bd09
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
35 changes: 32 additions & 3 deletions tests/src/core/testqgsogrutils.cpp
Expand Up @@ -60,6 +60,7 @@ class TestQgsOgrUtils: public QObject
void parseStyleString();
void convertStyleString();
void ogrCrsConversion();
void ogrFieldToVariant();

private:

Expand Down Expand Up @@ -317,7 +318,7 @@ void TestQgsOgrUtils::getOgrFeatureAttribute()
val = QgsOgrUtils::getOgrFeatureAttribute( oFeat, fields, 4, QTextCodec::codecForName( "System" ), &ok );
QVERIFY( ok );
QVERIFY( val.isValid() );
QCOMPARE( val, QVariant( QDateTime( QDate( 2005, 3, 5 ), QTime( 6, 45, 0 ) ) ) );
QCOMPARE( val, QVariant( QDateTime( QDate( 2005, 3, 5 ), QTime( 6, 45, 0, 123 ) ) ) );

val = QgsOgrUtils::getOgrFeatureAttribute( oFeat, fields, 5, QTextCodec::codecForName( "System" ), &ok );
QVERIFY( ok );
Expand Down Expand Up @@ -358,7 +359,7 @@ void TestQgsOgrUtils::readOgrFeatureAttributes()
QCOMPARE( f.attribute( "dbl_field" ), QVariant( 8.9 ) );
QCOMPARE( f.attribute( "date_field" ), QVariant( QDate( 2005, 01, 05 ) ) );
QCOMPARE( f.attribute( "time_field" ), QVariant( QTime( 8, 11, 01 ) ) );
QCOMPARE( f.attribute( "datetime_field" ), QVariant( QDateTime( QDate( 2005, 3, 5 ), QTime( 6, 45, 0 ) ) ) );
QCOMPARE( f.attribute( "datetime_field" ), QVariant( QDateTime( QDate( 2005, 3, 5 ), QTime( 6, 45, 0, 123 ) ) ) );
QCOMPARE( f.attribute( "string_field" ), QVariant( "a string" ) );

OGR_F_Destroy( oFeat );
Expand Down Expand Up @@ -397,7 +398,7 @@ void TestQgsOgrUtils::readOgrFeature()
QCOMPARE( f.attribute( "dbl_field" ), QVariant( 8.9 ) );
QCOMPARE( f.attribute( "date_field" ), QVariant( QDate( 2005, 01, 05 ) ) );
QCOMPARE( f.attribute( "time_field" ), QVariant( QTime( 8, 11, 01 ) ) );
QCOMPARE( f.attribute( "datetime_field" ), QVariant( QDateTime( QDate( 2005, 3, 5 ), QTime( 6, 45, 0 ) ) ) );
QCOMPARE( f.attribute( "datetime_field" ), QVariant( QDateTime( QDate( 2005, 3, 5 ), QTime( 6, 45, 0, 123 ) ) ) );
QCOMPARE( f.attribute( "string_field" ), QVariant( "a string" ) );
QVERIFY( f.hasGeometry() );
QCOMPARE( f.geometry().constGet()->wkbType(), QgsWkbTypes::LineString );
Expand Down Expand Up @@ -752,5 +753,33 @@ void TestQgsOgrUtils::ogrCrsConversion()
#endif
}

void TestQgsOgrUtils::ogrFieldToVariant()
{
OGRDataSourceH hDS = OGROpen( mTestFile.toUtf8().constData(), false, nullptr );
QVERIFY( hDS );
OGRLayerH ogrLayer = OGR_DS_GetLayer( hDS, 0 );
QVERIFY( ogrLayer );
OGRFeatureH oFeat;
oFeat = OGR_L_GetNextFeature( ogrLayer );
QVERIFY( oFeat );
OGRField oFieldInt, oFieldDbl, oFieldDate, oFieldTime, oFieldDatetime, oFieldString;
oFieldInt = *OGR_F_GetRawFieldRef( oFeat, 0 );
oFieldDbl = *OGR_F_GetRawFieldRef( oFeat, 1 );
oFieldDate = *OGR_F_GetRawFieldRef( oFeat, 2 );
oFieldTime = *OGR_F_GetRawFieldRef( oFeat, 3 );
oFieldDatetime = *OGR_F_GetRawFieldRef( oFeat, 4 );
oFieldString = *OGR_F_GetRawFieldRef( oFeat, 5 );

QCOMPARE( QgsOgrUtils::OGRFieldtoVariant( &oFieldInt, OGRFieldType::OFTInteger ), QVariant( 5 ) );
QCOMPARE( QgsOgrUtils::OGRFieldtoVariant( &oFieldDbl, OGRFieldType::OFTReal ), QVariant( 8.9 ) );
QCOMPARE( QgsOgrUtils::OGRFieldtoVariant( &oFieldDate, OGRFieldType::OFTDate ), QVariant( QDate( 2005, 01, 05 ) ) );
QCOMPARE( QgsOgrUtils::OGRFieldtoVariant( &oFieldTime, OGRFieldType::OFTTime ), QVariant( QTime( 8, 11, 01 ) ) );
QCOMPARE( QgsOgrUtils::OGRFieldtoVariant( &oFieldDatetime, OGRFieldType::OFTDateTime ), QVariant( QDateTime( QDate( 2005, 3, 5 ), QTime( 6, 45, 0, 123 ) ) ) );
QCOMPARE( QgsOgrUtils::OGRFieldtoVariant( &oFieldString, OGRFieldType::OFTString ), QVariant( "a string" ) );

OGR_F_Destroy( oFeat );
OGR_DS_Destroy( hDS );
}

QGSTEST_MAIN( TestQgsOgrUtils )
#include "testqgsogrutils.moc"
40 changes: 39 additions & 1 deletion tests/src/python/test_qgsjsonutils.py
Expand Up @@ -29,7 +29,7 @@
QgsRelation,
QgsEditorWidgetSetup
)
from qgis.PyQt.QtCore import QVariant, QTextCodec, QLocale
from qgis.PyQt.QtCore import QVariant, QTextCodec, QLocale, Qt

start_app()
codec = QTextCodec.codecForName("System")
Expand Down Expand Up @@ -81,6 +81,44 @@ def testStringToFeatureList(self):
self.assertEqual(point.y(), 20.0)
self.assertEqual(features[1]['name'], "Henry Gale Island")

def testStringToFeatureListWithDatetimeProperty_regression44160(self):
"""Test that milliseconds and time zone information is parsed from datetime properties"""
fields = QgsFields()
fields.append(QgsField("some_time_field", QVariant.DateTime))

date = "2020-01-01T"

def geojson_with_time(timepart):
return '{"type": "Feature","geometry": {"type": "Point","coordinates": [0,0]},"properties": {"some_time_field": "' + date + timepart + '"}}'

# No milliseconds
features = QgsJsonUtils.stringToFeatureList(geojson_with_time('22:00:10'), fields)
self.assertEqual(len(features), 1)
self.assertEqual(features[0]['some_time_field'].toString(Qt.ISODateWithMs), f"{date}22:00:10.000")

# milliseconds
features = QgsJsonUtils.stringToFeatureList(geojson_with_time('22:00:10.123'), fields)
self.assertEqual(len(features), 1)
self.assertEqual(features[0]['some_time_field'].toString(Qt.ISODateWithMs), f"{date}22:00:10.123")

def testStringToFeatureListWithTimeProperty_regression44160(self):
"""Test that milliseconds and time zone information is parsed from time properties"""
fields = QgsFields()
fields.append(QgsField("some_time_field", QVariant.Time))

def geojson_with_time(timepart):
return '{"type": "Feature","geometry": {"type": "Point","coordinates": [0,0]},"properties": {"some_time_field": "' + timepart + '"}}'

# No milliseconds
features = QgsJsonUtils.stringToFeatureList(geojson_with_time('22:00:10'), fields)
self.assertEqual(len(features), 1)
self.assertEqual(features[0]['some_time_field'].toString(Qt.ISODateWithMs), "22:00:10.000")

# milliseconds
features = QgsJsonUtils.stringToFeatureList(geojson_with_time('22:00:10.123'), fields)
self.assertEqual(len(features), 1)
self.assertEqual(features[0]['some_time_field'].toString(Qt.ISODateWithMs), "22:00:10.123")

def testStringToFields(self):
"""test retrieving fields from GeoJSON strings"""

Expand Down
Binary file modified tests/testdata/ogr_types.dat
Binary file not shown.
Binary file modified tests/testdata/ogr_types.id
Binary file not shown.
Binary file modified tests/testdata/ogr_types.map
Binary file not shown.

0 comments on commit 606bd09

Please sign in to comment.