normalise timestamp in query

when type casting query parameters, we use parse_isotime. this has
a side effect of adding a tzinfo to the resulting datetime. no db
currently stores tzinfo so we should normalise query to drop this
information. as a side effect, it also adds in missing supported type

Change-Id: Id4a5ffbb5388122b43adcf5054a7cd0cce3d275d
Closes-Bug: #1423545
This commit is contained in:
gordon chung 2015-03-10 17:59:22 -04:00
parent df10a87ea7
commit 7e35570393
2 changed files with 12 additions and 1 deletions

View File

@ -19,6 +19,7 @@
# under the License.
import ast
import datetime
import functools
import inspect
import json
@ -129,7 +130,7 @@ class Query(Base):
"""Query filter."""
# The data types supported by the query.
_supported_types = ['integer', 'float', 'string', 'boolean']
_supported_types = ['integer', 'float', 'string', 'boolean', 'datetime']
# Functions to convert the data field to the correct type.
_type_converters = {'integer': int,
@ -213,6 +214,8 @@ class Query(Base):
# _type_converters to define their own types.
raise TypeError()
converted_value = self._type_converters[type](self.value)
if isinstance(converted_value, datetime.datetime):
converted_value = timeutils.normalize_time(converted_value)
except ValueError:
msg = (_('Unable to convert the value %(value)s'
' to the expected data type %(type)s.') %

View File

@ -70,6 +70,14 @@ class TestQuery(base.BaseTestCase):
expected = 'linux'
self.assertEqual(expected, query._get_value_as_type())
def test_get_value_as_type_with_datetime(self):
query = v2_base.Query(field='metadata.date',
op='eq',
value='2014-01-01T05:00:00',
type='datetime')
self.assertIsInstance(query._get_value_as_type(), datetime.datetime)
self.assertIsNone(query._get_value_as_type().tzinfo)
def test_get_value_as_type_with_integer_without_type(self):
query = v2_base.Query(field='metadata.size',
op='eq',