Skip to content

Commit

Permalink
Allow casting date/datetime values to string to compare to expected r…
Browse files Browse the repository at this point in the history
…esults
  • Loading branch information
nyalldawson committed Feb 5, 2021
1 parent 45ad2f1 commit 318dbc8
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions python/testing/__init__.py
Expand Up @@ -28,7 +28,7 @@
import filecmp
import tempfile

from qgis.PyQt.QtCore import QVariant
from qgis.PyQt.QtCore import QVariant, QDateTime, QDate
from qgis.core import (
QgsApplication,
QgsFeatureRequest,
Expand Down Expand Up @@ -305,8 +305,18 @@ def checkAttributesEqual(self, feat0, feat1, fields_expected, use_asserts, compa
attr_result = float(attr_result) if attr_result else None
isNumber = True
if cmp['cast'] == 'str':
attr_expected = str(attr_expected) if attr_expected else None
attr_result = str(attr_result) if attr_result else None
if isinstance(attr_expected, QDateTime):
attr_expected = attr_expected.toString('yyyy/MM/dd hh:mm:ss')
elif isinstance(attr_expected, QDate):
attr_expected = attr_expected.toString('yyyy/MM/dd')
else:
attr_expected = str(attr_expected) if attr_expected else None
if isinstance(attr_result, QDateTime):
attr_result = attr_result.toString('yyyy/MM/dd hh:mm:ss')
elif isinstance(attr_result, QDate):
attr_result = attr_result.toString('yyyy/MM/dd')
else:
attr_result = str(attr_result) if attr_result else None

# Round field (only numeric so it works with __all__)
if 'precision' in cmp and (field_expected.type() in [QVariant.Int, QVariant.Double, QVariant.LongLong] or isNumber):
Expand Down

0 comments on commit 318dbc8

Please sign in to comment.