Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add more datetime/date/time tests to provider test suite
  • Loading branch information
nyalldawson committed May 14, 2020
1 parent 90e61ce commit 142043a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
13 changes: 7 additions & 6 deletions tests/src/python/featuresourcetestbase.py
Expand Up @@ -28,6 +28,7 @@
QgsCoordinateReferenceSystem,
NULL
)
from qgis.PyQt.QtCore import QDate, QTime, QDateTime

from utilities import compareWkt

Expand Down Expand Up @@ -76,19 +77,19 @@ def testGetFeatures(self, source=None, extra_features=[], skip_features=[], chan
self.assertTrue(f.isValid())
# some source test datasets will include additional attributes which we ignore,
# so cherry pick desired attributes
attrs = [f['pk'], f['cnt'], f['name'], f['name2'], f['num_char']]
attrs = [f['pk'], f['cnt'], f['name'], f['name2'], f['num_char'], f['dt'], f['date'], f['time']]
# force the num_char attribute to be text - some sources (e.g., delimited text) will
# automatically detect that this attribute contains numbers and set it as a numeric
# field
attrs[4] = str(attrs[4])
attributes[f['pk']] = attrs
geometries[f['pk']] = f.hasGeometry() and f.geometry().asWkt()

expected_attributes = {5: [5, -200, NULL, 'NuLl', '5'],
3: [3, 300, 'Pear', 'PEaR', '3'],
1: [1, 100, 'Orange', 'oranGe', '1'],
2: [2, 200, 'Apple', 'Apple', '2'],
4: [4, 400, 'Honey', 'Honey', '4']}
expected_attributes = {5: [5, -200, NULL, 'NuLl', '5', QDateTime(QDate(2020, 5, 4), QTime(12, 13, 14)), QDate(2020, 5, 4), QTime(12, 13, 14)],
3: [3, 300, 'Pear', 'PEaR', '3', NULL, NULL, NULL],
1: [1, 100, 'Orange', 'oranGe', '1', QDateTime(QDate(2020, 5, 3), QTime(12, 13, 14)), QDate(2020, 5, 3), QTime(12, 13, 14)],
2: [2, 200, 'Apple', 'Apple', '2', QDateTime(QDate(2020, 5, 4), QTime(12, 14, 14)), QDate(2020, 5, 4), QTime(12, 14, 14)],
4: [4, 400, 'Honey', 'Honey', '4', QDateTime(QDate(2021, 5, 4), QTime(13, 13, 14)), QDate(2021, 5, 4), QTime(13, 13, 14)]}

expected_geometries = {1: 'Point (-70.332 66.33)',
2: 'Point (-68.2 70.8)',
Expand Down
18 changes: 9 additions & 9 deletions tests/src/python/providertestbase.py
Expand Up @@ -541,11 +541,11 @@ def testAddFeature(self):
self.assertTrue(l.isValid())

f1 = QgsFeature()
f1.setAttributes([6, -220, NULL, 'String', '15'])
f1.setAttributes([6, -220, NULL, 'String', '15', QDateTime(2019, 1, 2, 3, 4, 5), QDate(2019, 1, 2), QTime(3, 4, 5)])
f1.setGeometry(QgsGeometry.fromWkt('Point (-72.345 71.987)'))

f2 = QgsFeature()
f2.setAttributes([7, 330, 'Coconut', 'CoCoNut', '13'])
f2.setAttributes([7, 330, 'Coconut', 'CoCoNut', '13', QDateTime(2018, 5, 6, 7, 8, 9), QDate(2018, 5, 6), QTime(7, 8, 9)])

if l.dataProvider().capabilities() & QgsVectorDataProvider.AddFeatures:
# expect success
Expand Down Expand Up @@ -581,11 +581,11 @@ def testAddFeatureFastInsert(self):
self.assertTrue(l.isValid())

f1 = QgsFeature()
f1.setAttributes([6, -220, NULL, 'String', '15'])
f1.setAttributes([6, -220, NULL, 'String', '15', QDateTime(2019, 1, 2, 3, 4, 5), QDate(2019, 1, 2), QTime(3, 4, 5)])
f1.setGeometry(QgsGeometry.fromWkt('Point (-72.345 71.987)'))

f2 = QgsFeature()
f2.setAttributes([7, 330, 'Coconut', 'CoCoNut', '13'])
f2.setAttributes([7, 330, 'Coconut', 'CoCoNut', '13', NULL, NULL, NULL])

if l.dataProvider().capabilities() & QgsVectorDataProvider.AddFeatures:
# expect success
Expand Down Expand Up @@ -617,8 +617,8 @@ def testAddFeatureMissingAttributes(self):
f2.setId(added[1].id())

# check result - feature attributes MUST be padded out to required number of fields
f1.setAttributes([6, -220, NULL, 'String', 'NULL'])
f2.setAttributes([7, 330, NULL, NULL, 'NULL'])
f1.setAttributes([6, -220, NULL, 'String', 'NULL', NULL, NULL, NULL])
f2.setAttributes([7, 330, NULL, NULL, 'NULL', NULL, NULL, NULL])
self.testGetFeatures(l.dataProvider(), [f1, f2])

def testAddFeatureExtraAttributes(self):
Expand All @@ -634,17 +634,17 @@ def testAddFeatureExtraAttributes(self):
# test that adding features with too many attributes drops these attributes
# we be more tricky and also add a valid feature to stress test the provider
f1 = QgsFeature()
f1.setAttributes([6, -220, NULL, 'String', '15'])
f1.setAttributes([6, -220, NULL, 'String', '15', QDateTime(2019, 1, 2, 3, 4, 5), QDate(2019, 1, 2), QTime(3, 4, 5)])
f2 = QgsFeature()
f2.setAttributes([7, -230, NULL, 'String', '15', 15, 16, 17])
f2.setAttributes([7, -230, NULL, 'String', '15', QDateTime(2019, 1, 2, 3, 4, 5), QDate(2019, 1, 2), QTime(3, 4, 5), 15, 16, 17])

result, added = l.dataProvider().addFeatures([f1, f2])
self.assertTrue(result,
'Provider returned False to addFeatures with extra attributes. Providers should accept these features but truncate the extra attributes.')

# make sure feature was added correctly
added = [f for f in l.dataProvider().getFeatures() if f['pk'] == 7][0]
self.assertEqual(added.attributes(), [7, -230, NULL, 'String', '15'])
self.assertEqual(added.attributes(), [7, -230, NULL, 'String', '15', QDateTime(2019, 1, 2, 3, 4, 5), QDate(2019, 1, 2), QTime(3, 4, 5)])

def testAddFeatureWrongGeomType(self):
if not getattr(self, 'getEditableLayer', None):
Expand Down

0 comments on commit 142043a

Please sign in to comment.