Skip to content

Commit d64bcd7

Browse files
committed
Add tests for Cython DateType deserialization
1 parent 713bfe4 commit d64bcd7

3 files changed

Lines changed: 97 additions & 0 deletions

File tree

tests/unit/cython/test_types.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2013-2016 DataStax, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from tests.unit.cython.utils import cyimport, cythontest
16+
types_testhelper = cyimport('tests.unit.cython.types_testhelper')
17+
18+
try:
19+
import unittest2 as unittest
20+
except ImportError:
21+
import unittest # noqa
22+
23+
24+
class TypesTest(unittest.TestCase):
25+
26+
@cythontest
27+
def test_datetype(self):
28+
types_testhelper.test_datetype(self.assertEqual)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright 2013-2016 DataStax, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import time
17+
import datetime
18+
19+
include '../../../cassandra/ioutils.pyx'
20+
21+
from cassandra.cqltypes import DateType
22+
from cassandra.deserializers import find_deserializer
23+
from cassandra.bytesio cimport BytesIOReader
24+
from cassandra.buffer cimport Buffer
25+
from cassandra.deserializers cimport from_binary, Deserializer
26+
27+
28+
def test_datetype(assert_equal):
29+
30+
cdef Deserializer des = find_deserializer(DateType)
31+
32+
def deserialize(timestamp):
33+
"""Serialize a datetime and deserialize it using the cython deserializer"""
34+
35+
cdef BytesIOReader reader
36+
cdef Buffer buf
37+
38+
dt = datetime.datetime.utcfromtimestamp(timestamp)
39+
reader = BytesIOReader(b'\x00\x00\x00\x08' + DateType.serialize(dt, 0))
40+
get_buf(reader, &buf)
41+
deserialized_dt = from_binary(des, &buf, 0)
42+
43+
return deserialized_dt
44+
45+
# deserialize
46+
# epoc
47+
expected = 0
48+
assert_equal(deserialize(expected), datetime.datetime.utcfromtimestamp(expected))
49+
50+
# beyond 32b
51+
expected = 2 ** 33
52+
assert_equal(deserialize(expected), datetime.datetime(2242, 3, 16, 12, 56, 32))
53+
54+
# less than epoc (PYTHON-119)
55+
expected = -770172256
56+
assert_equal(deserialize(expected), datetime.datetime(1945, 8, 5, 23, 15, 44))
57+
58+
# work around rounding difference among Python versions (PYTHON-230)
59+
# This wont pass with the cython extension until we fix the microseconds alignment with CPython
60+
#expected = 1424817268.274
61+
#assert_equal(deserialize(expected), datetime.datetime(2015, 2, 24, 22, 34, 28, 274000))
62+
63+
# Large date overflow (PYTHON-452)
64+
expected = 2177403010.123
65+
assert_equal(deserialize(expected), datetime.datetime(2038, 12, 31, 10, 10, 10, 123000))

tests/unit/test_types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ def test_datetype(self):
204204
expected = 1424817268.274
205205
self.assertEqual(DateType.deserialize(int64_pack(int(1000 * expected)), 0), datetime.datetime(2015, 2, 24, 22, 34, 28, 274000))
206206

207+
# Large date overflow (PYTHON-452)
208+
expected = 2177403010.123
209+
self.assertEqual(DateType.deserialize(int64_pack(int(1000 * expected)), 0), datetime.datetime(2038, 12, 31, 10, 10, 10, 123000))
210+
207211
def test_write_read_string(self):
208212
with tempfile.TemporaryFile() as f:
209213
value = u'test'

0 commit comments

Comments
 (0)