|
| 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)) |
0 commit comments