Skip to content

Commit

Permalink
Fix base QgsVectorDataProvider::maximumValue/minimumValue handling
Browse files Browse the repository at this point in the history
of datetime/date/time values
  • Loading branch information
nyalldawson committed May 14, 2020
1 parent 97cbb3e commit d35bc0b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
27 changes: 27 additions & 0 deletions src/core/qgsvectordataprovider.cpp
Expand Up @@ -561,6 +561,33 @@ void QgsVectorDataProvider::fillMinMaxCache() const
mCacheMaxValues[ attributeIndex ] = value;
break;
}
case QVariant::DateTime:
{
QDateTime value = varValue.toDateTime();
if ( value < mCacheMinValues[ attributeIndex ].toDateTime() || !mCacheMinValues[ attributeIndex ].isValid() )
mCacheMinValues[attributeIndex ] = value;
if ( value > mCacheMaxValues[ attributeIndex ].toDateTime() || !mCacheMaxValues[ attributeIndex ].isValid() )
mCacheMaxValues[ attributeIndex ] = value;
break;
}
case QVariant::Date:
{
QDate value = varValue.toDate();
if ( value < mCacheMinValues[ attributeIndex ].toDate() || !mCacheMinValues[ attributeIndex ].isValid() )
mCacheMinValues[attributeIndex ] = value;
if ( value > mCacheMaxValues[ attributeIndex ].toDate() || !mCacheMaxValues[ attributeIndex ].isValid() )
mCacheMaxValues[ attributeIndex ] = value;
break;
}
case QVariant::Time:
{
QTime value = varValue.toTime();
if ( value < mCacheMinValues[ attributeIndex ].toTime() || !mCacheMinValues[ attributeIndex ].isValid() )
mCacheMinValues[attributeIndex ] = value;
if ( value > mCacheMaxValues[ attributeIndex ].toTime() || !mCacheMaxValues[ attributeIndex ].isValid() )
mCacheMaxValues[ attributeIndex ] = value;
break;
}
default:
{
QString value = varValue.toString();
Expand Down
9 changes: 9 additions & 0 deletions tests/src/python/providertestbase.py
Expand Up @@ -31,6 +31,7 @@
QgsFeatureSource,
NULL
)
from qgis.PyQt.QtCore import QDate, QTime, QDateTime
from qgis.PyQt.QtTest import QSignalSpy

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

self.assertEqual(self.source.minimumValue(self.source.fields().lookupField('dt')), QDateTime(QDate(2020, 5, 3), QTime(12, 13, 14)))
self.assertEqual(self.source.minimumValue(self.source.fields().lookupField('date')), QDate(2020, 5, 3))
self.assertEqual(self.source.minimumValue(self.source.fields().lookupField('time')), QTime(12, 13, 14))

if self.source.supportsSubsetString():
subset = self.getSubsetString()
self.source.setSubsetString(subset)
Expand All @@ -360,6 +365,10 @@ def testMaxValue(self):
self.assertEqual(self.source.maximumValue(self.source.fields().lookupField('cnt')), 400)
self.assertEqual(self.source.maximumValue(self.source.fields().lookupField('name')), 'Pear')

self.assertEqual(self.source.maximumValue(self.source.fields().lookupField('dt')), QDateTime(QDate(2021, 5, 4), QTime(13, 13, 14)))
self.assertEqual(self.source.maximumValue(self.source.fields().lookupField('date')), QDate(2021, 5, 4))
self.assertEqual(self.source.maximumValue(self.source.fields().lookupField('time')), QTime(13, 13, 14))

if self.source.supportsSubsetString():
subset = self.getSubsetString2()
self.source.setSubsetString(subset)
Expand Down
26 changes: 13 additions & 13 deletions tests/src/python/test_provider_memory.py
Expand Up @@ -42,7 +42,7 @@
)

from providertestbase import ProviderTestCase
from qgis.PyQt.QtCore import QVariant, QByteArray
from qgis.PyQt.QtCore import QVariant, QByteArray, QDate, QDateTime, QTime

start_app()
TEST_DATA_DIR = unitTestDataPath()
Expand All @@ -53,27 +53,27 @@ class TestPyQgsMemoryProvider(unittest.TestCase, ProviderTestCase):
@classmethod
def createLayer(cls):
vl = QgsVectorLayer(
'Point?crs=epsg:4326&field=pk:integer&field=cnt:integer&field=name:string(0)&field=name2:string(0)&field=num_char:string&key=pk',
'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',
'test', 'memory')
assert (vl.isValid())

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

f2 = QgsFeature()
f2.setAttributes([3, 300, 'Pear', 'PEaR', '3'])
f2.setAttributes([3, 300, 'Pear', 'PEaR', '3', NULL, NULL, NULL])

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

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

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

vl.dataProvider().addFeatures([f1, f2, f3, f4, f5])
Expand Down Expand Up @@ -687,28 +687,28 @@ def setUpClass(cls):
"""Run before all tests"""
# Create test layer
cls.vl = QgsVectorLayer(
'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',
'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',
'test', 'memory')
assert (cls.vl.isValid())
cls.source = cls.vl.dataProvider()

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

f2 = QgsFeature()
f2.setAttributes([3, 300, 'Pear', 'PEaR', '3'])
f2.setAttributes([3, 300, 'Pear', 'PEaR', '3', NULL, NULL, NULL])

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

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

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

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

0 comments on commit d35bc0b

Please sign in to comment.