Skip to content

Commit d35bc0b

Browse files
committedMay 14, 2020
Fix base QgsVectorDataProvider::maximumValue/minimumValue handling
of datetime/date/time values
1 parent 97cbb3e commit d35bc0b

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed
 

‎src/core/qgsvectordataprovider.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,33 @@ void QgsVectorDataProvider::fillMinMaxCache() const
561561
mCacheMaxValues[ attributeIndex ] = value;
562562
break;
563563
}
564+
case QVariant::DateTime:
565+
{
566+
QDateTime value = varValue.toDateTime();
567+
if ( value < mCacheMinValues[ attributeIndex ].toDateTime() || !mCacheMinValues[ attributeIndex ].isValid() )
568+
mCacheMinValues[attributeIndex ] = value;
569+
if ( value > mCacheMaxValues[ attributeIndex ].toDateTime() || !mCacheMaxValues[ attributeIndex ].isValid() )
570+
mCacheMaxValues[ attributeIndex ] = value;
571+
break;
572+
}
573+
case QVariant::Date:
574+
{
575+
QDate value = varValue.toDate();
576+
if ( value < mCacheMinValues[ attributeIndex ].toDate() || !mCacheMinValues[ attributeIndex ].isValid() )
577+
mCacheMinValues[attributeIndex ] = value;
578+
if ( value > mCacheMaxValues[ attributeIndex ].toDate() || !mCacheMaxValues[ attributeIndex ].isValid() )
579+
mCacheMaxValues[ attributeIndex ] = value;
580+
break;
581+
}
582+
case QVariant::Time:
583+
{
584+
QTime value = varValue.toTime();
585+
if ( value < mCacheMinValues[ attributeIndex ].toTime() || !mCacheMinValues[ attributeIndex ].isValid() )
586+
mCacheMinValues[attributeIndex ] = value;
587+
if ( value > mCacheMaxValues[ attributeIndex ].toTime() || !mCacheMaxValues[ attributeIndex ].isValid() )
588+
mCacheMaxValues[ attributeIndex ] = value;
589+
break;
590+
}
564591
default:
565592
{
566593
QString value = varValue.toString();

‎tests/src/python/providertestbase.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
QgsFeatureSource,
3232
NULL
3333
)
34+
from qgis.PyQt.QtCore import QDate, QTime, QDateTime
3435
from qgis.PyQt.QtTest import QSignalSpy
3536

3637
from utilities import compareWkt
@@ -347,6 +348,10 @@ def testMinValue(self):
347348
self.assertEqual(self.source.minimumValue(self.source.fields().lookupField('cnt')), -200)
348349
self.assertEqual(self.source.minimumValue(self.source.fields().lookupField('name')), 'Apple')
349350

351+
self.assertEqual(self.source.minimumValue(self.source.fields().lookupField('dt')), QDateTime(QDate(2020, 5, 3), QTime(12, 13, 14)))
352+
self.assertEqual(self.source.minimumValue(self.source.fields().lookupField('date')), QDate(2020, 5, 3))
353+
self.assertEqual(self.source.minimumValue(self.source.fields().lookupField('time')), QTime(12, 13, 14))
354+
350355
if self.source.supportsSubsetString():
351356
subset = self.getSubsetString()
352357
self.source.setSubsetString(subset)
@@ -360,6 +365,10 @@ def testMaxValue(self):
360365
self.assertEqual(self.source.maximumValue(self.source.fields().lookupField('cnt')), 400)
361366
self.assertEqual(self.source.maximumValue(self.source.fields().lookupField('name')), 'Pear')
362367

368+
self.assertEqual(self.source.maximumValue(self.source.fields().lookupField('dt')), QDateTime(QDate(2021, 5, 4), QTime(13, 13, 14)))
369+
self.assertEqual(self.source.maximumValue(self.source.fields().lookupField('date')), QDate(2021, 5, 4))
370+
self.assertEqual(self.source.maximumValue(self.source.fields().lookupField('time')), QTime(13, 13, 14))
371+
363372
if self.source.supportsSubsetString():
364373
subset = self.getSubsetString2()
365374
self.source.setSubsetString(subset)

‎tests/src/python/test_provider_memory.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
)
4343

4444
from providertestbase import ProviderTestCase
45-
from qgis.PyQt.QtCore import QVariant, QByteArray
45+
from qgis.PyQt.QtCore import QVariant, QByteArray, QDate, QDateTime, QTime
4646

4747
start_app()
4848
TEST_DATA_DIR = unitTestDataPath()
@@ -53,27 +53,27 @@ class TestPyQgsMemoryProvider(unittest.TestCase, ProviderTestCase):
5353
@classmethod
5454
def createLayer(cls):
5555
vl = QgsVectorLayer(
56-
'Point?crs=epsg:4326&field=pk:integer&field=cnt:integer&field=name:string(0)&field=name2:string(0)&field=num_char:string&key=pk',
56+
'Point?crs=epsg:4326&field=pk:integer&field=cnt:integer&field=name:string(0)&field=name2:string(0)&field=num_char:string&field=dt:datetime&field=date:date&field=time:time&key=pk',
5757
'test', 'memory')
5858
assert (vl.isValid())
5959

6060
f1 = QgsFeature()
61-
f1.setAttributes([5, -200, NULL, 'NuLl', '5'])
61+
f1.setAttributes([5, -200, NULL, 'NuLl', '5', QDateTime(QDate(2020, 5, 4), QTime(12, 13, 14)), QDate(2020, 5, 4), QTime(12, 13, 14)])
6262
f1.setGeometry(QgsGeometry.fromWkt('Point (-71.123 78.23)'))
6363

6464
f2 = QgsFeature()
65-
f2.setAttributes([3, 300, 'Pear', 'PEaR', '3'])
65+
f2.setAttributes([3, 300, 'Pear', 'PEaR', '3', NULL, NULL, NULL])
6666

6767
f3 = QgsFeature()
68-
f3.setAttributes([1, 100, 'Orange', 'oranGe', '1'])
68+
f3.setAttributes([1, 100, 'Orange', 'oranGe', '1', QDateTime(QDate(2020, 5, 3), QTime(12, 13, 14)), QDate(2020, 5, 3), QTime(12, 13, 14)])
6969
f3.setGeometry(QgsGeometry.fromWkt('Point (-70.332 66.33)'))
7070

7171
f4 = QgsFeature()
72-
f4.setAttributes([2, 200, 'Apple', 'Apple', '2'])
72+
f4.setAttributes([2, 200, 'Apple', 'Apple', '2', QDateTime(QDate(2020, 5, 4), QTime(12, 14, 14)), QDate(2020, 5, 4), QTime(12, 14, 14)])
7373
f4.setGeometry(QgsGeometry.fromWkt('Point (-68.2 70.8)'))
7474

7575
f5 = QgsFeature()
76-
f5.setAttributes([4, 400, 'Honey', 'Honey', '4'])
76+
f5.setAttributes([4, 400, 'Honey', 'Honey', '4', QDateTime(QDate(2021, 5, 4), QTime(13, 13, 14)), QDate(2021, 5, 4), QTime(13, 13, 14)])
7777
f5.setGeometry(QgsGeometry.fromWkt('Point (-65.32 78.3)'))
7878

7979
vl.dataProvider().addFeatures([f1, f2, f3, f4, f5])
@@ -687,28 +687,28 @@ def setUpClass(cls):
687687
"""Run before all tests"""
688688
# Create test layer
689689
cls.vl = QgsVectorLayer(
690-
'Point?crs=epsg:4326&index=yes&field=pk:integer&field=cnt:int8&field=name:string(0)&field=name2:string(0)&field=num_char:string&key=pk',
690+
'Point?crs=epsg:4326&field=pk:integer&field=cnt:integer&field=name:string(0)&field=name2:string(0)&field=num_char:string&field=dt:datetime&field=date:date&field=time:time&key=pk',
691691
'test', 'memory')
692692
assert (cls.vl.isValid())
693693
cls.source = cls.vl.dataProvider()
694694

695695
f1 = QgsFeature()
696-
f1.setAttributes([5, -200, NULL, 'NuLl', '5'])
696+
f1.setAttributes([5, -200, NULL, 'NuLl', '5', QDateTime(QDate(2020, 5, 4), QTime(12, 13, 14)), QDate(2020, 5, 4), QTime(12, 13, 14)])
697697
f1.setGeometry(QgsGeometry.fromWkt('Point (-71.123 78.23)'))
698698

699699
f2 = QgsFeature()
700-
f2.setAttributes([3, 300, 'Pear', 'PEaR', '3'])
700+
f2.setAttributes([3, 300, 'Pear', 'PEaR', '3', NULL, NULL, NULL])
701701

702702
f3 = QgsFeature()
703-
f3.setAttributes([1, 100, 'Orange', 'oranGe', '1'])
703+
f3.setAttributes([1, 100, 'Orange', 'oranGe', '1', QDateTime(QDate(2020, 5, 3), QTime(12, 13, 14)), QDate(2020, 5, 3), QTime(12, 13, 14)])
704704
f3.setGeometry(QgsGeometry.fromWkt('Point (-70.332 66.33)'))
705705

706706
f4 = QgsFeature()
707-
f4.setAttributes([2, 200, 'Apple', 'Apple', '2'])
707+
f4.setAttributes([2, 200, 'Apple', 'Apple', '2', QDateTime(QDate(2020, 5, 4), QTime(12, 14, 14)), QDate(2020, 5, 4), QTime(12, 14, 14)])
708708
f4.setGeometry(QgsGeometry.fromWkt('Point (-68.2 70.8)'))
709709

710710
f5 = QgsFeature()
711-
f5.setAttributes([4, 400, 'Honey', 'Honey', '4'])
711+
f5.setAttributes([4, 400, 'Honey', 'Honey', '4', QDateTime(QDate(2021, 5, 4), QTime(13, 13, 14)), QDate(2021, 5, 4), QTime(13, 13, 14)])
712712
f5.setGeometry(QgsGeometry.fromWkt('Point (-65.32 78.3)'))
713713

714714
cls.source.addFeatures([f1, f2, f3, f4, f5])

0 commit comments

Comments
 (0)
Please sign in to comment.