def test_datetype(self): now_time_seconds = time.time() now_datetime = datetime.datetime.utcfromtimestamp(now_time_seconds) # Cassandra timestamps in millis now_timestamp = now_time_seconds * 1e3 # same results serialized self.assertEqual(DateType.serialize(now_datetime, 0), DateType.serialize(now_timestamp, 0)) # deserialize # epoc expected = 0 self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected)) # beyond 32b expected = 2**33 self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime(2242, 3, 16, 12, 56, 32)) # less than epoc (PYTHON-119) expected = -770172256 self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime(1945, 8, 5, 23, 15, 44)) # work around rounding difference among Python versions (PYTHON-230) expected = 1424817268.274 self.assertEqual( DateType.deserialize(int64_pack(int(1000 * expected)), 0), datetime.datetime(2015, 2, 24, 22, 34, 28, 274000))
def test_datetype(self): now_timestamp = time.time() now_datetime = datetime.datetime.utcfromtimestamp(now_timestamp) # same results serialized (must scale the timestamp to milliseconds) self.assertEqual(DateType.serialize(now_datetime, 0), DateType.serialize(now_timestamp * 1e3, 0)) # from timestamp date_type = DateType(now_timestamp) self.assertEqual(date_type.my_timestamp(), now_timestamp) # from datetime object date_type = DateType(now_datetime) self.assertEqual(date_type.my_timestamp(), now_datetime) # deserialize # epoc expected = 0 self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected)) # beyond 32b expected = 2**33 self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected)) # less than epoc (PYTHON-119) expected = -770172256 self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime(1945, 8, 5, 23, 15, 44)) self.assertRaises(ValueError, date_type.interpret_datestring, 'fakestring')
def test_datetype(self): now_timestamp = time.time() now_datetime = datetime.datetime.utcfromtimestamp(now_timestamp) # same results serialized # (this could change if we follow up on the timestamp multiplication warning in DateType.serialize) self.assertEqual(DateType.serialize(now_datetime, 0), DateType.serialize(now_timestamp, 0)) # from timestamp date_type = DateType(now_timestamp) self.assertEqual(date_type.my_timestamp(), now_timestamp) # from datetime object date_type = DateType(now_datetime) self.assertEqual(date_type.my_timestamp(), now_datetime) # deserialize # epoc expected = 0 self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected)) # beyond 32b expected = 2**33 self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected)) # less than epoc (PYTHON-119) expected = -770172256 self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime(1945, 8, 5, 23, 15, 44)) self.assertRaises(ValueError, date_type.interpret_datestring, 'fakestring')
def test_datetype(self): now_time_seconds = time.time() now_datetime = datetime.datetime.utcfromtimestamp(now_time_seconds) # Cassandra timestamps in millis now_timestamp = now_time_seconds * 1e3 # same results serialized self.assertEqual(DateType.serialize(now_datetime, 0), DateType.serialize(now_timestamp, 0)) # deserialize # epoc expected = 0 self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected)) # beyond 32b expected = 2 ** 33 self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime(2242, 3, 16, 12, 56, 32)) # less than epoc (PYTHON-119) expected = -770172256 self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime(1945, 8, 5, 23, 15, 44)) # work around rounding difference among Python versions (PYTHON-230) expected = 1424817268.274 self.assertEqual(DateType.deserialize(int64_pack(int(1000 * expected)), 0), datetime.datetime(2015, 2, 24, 22, 34, 28, 274000)) # Large date overflow (PYTHON-452) expected = 2177403010.123 self.assertEqual(DateType.deserialize(int64_pack(int(1000 * expected)), 0), datetime.datetime(2038, 12, 31, 10, 10, 10, 123000))
def test_datetype(self): now_time_seconds = time.time() now_datetime = datetime.datetime.utcfromtimestamp(now_time_seconds) # Cassandra timestamps in millis now_timestamp = now_time_seconds * 1e3 # same results serialized self.assertEqual(DateType.serialize(now_datetime, 0), DateType.serialize(now_timestamp, 0)) # from timestamp date_type = DateType(now_timestamp) self.assertEqual(date_type.my_timestamp(), now_timestamp) # from datetime object date_type = DateType(now_datetime) self.assertEqual(date_type.my_timestamp(), now_datetime) # deserialize # epoc expected = 0 self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected)) # beyond 32b expected = 2**33 self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected)) # less than epoc (PYTHON-119) expected = -770172256 self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime(1945, 8, 5, 23, 15, 44)) self.assertRaises(ValueError, date_type.interpret_datestring, 'fakestring')
def test_deserialize_closed_range(self): serialized = (int8_pack(1) + int64_pack(self.timestamp) + int8_pack(2) + int64_pack(self.timestamp) + int8_pack(6)) self.assertEqual( DateRangeType.deserialize(serialized, 5), util.DateRange(lower_bound=util.DateRangeBound( value=datetime.datetime(2017, 2, 1, 0, 0), precision='DAY'), upper_bound=util.DateRangeBound( value=datetime.datetime(2017, 2, 1, 15, 42, 12, 404000), precision='MILLISECOND')))
def test_serialize_closed_range(self): serialized = (int8_pack(1) + int64_pack(self.timestamp) + int8_pack(5) + int64_pack(self.timestamp) + int8_pack(0)) deserialized = DateRangeType.deserialize(serialized, 5) self.assertEqual( deserialized, util.DateRange(lower_bound=util.DateRangeBound( value=datetime.datetime(2017, 2, 1, 15, 42, 12), precision='SECOND'), upper_bound=util.DateRangeBound( value=datetime.datetime(2017, 12, 31), precision='YEAR')))
def test_deserialize_zero_datetime(self): """ Test deserialization where timestamp = 0 Reproduces PYTHON-729 @since 2.0.0 @jira_ticket PYTHON-729 @expected_result deserialization doesn't raise an error @test_category data_types """ DateRangeType.deserialize( (int8_pack(1) + int64_pack(0) + int8_pack(0) + int64_pack(0) + int8_pack(0)), 5)
def test_deserialize_single_value(self): serialized = (int8_pack(0) + int64_pack(self.timestamp) + int8_pack(3)) self.assertEqual( DateRangeType.deserialize(serialized, 5), util.DateRange(value=util.DateRangeBound(value=datetime.datetime( 2017, 2, 1, 15, 42, 12, 404000), precision='HOUR')))
def test_serialize_open_low(self): serialized = (int8_pack(2) + int64_pack(self.timestamp) + int8_pack(3)) deserialized = DateRangeType.deserialize(serialized, 5) self.assertEqual( deserialized, util.DateRange(lower_bound=util.DateRangeBound( value=datetime.datetime(2017, 2, 1, 15), precision='HOUR'), upper_bound=util.OPEN_BOUND))
def test_serialize_single_value(self): serialized = (int8_pack(0) + int64_pack(self.timestamp) + int8_pack(5)) deserialized = DateRangeType.deserialize(serialized, 5) self.assertEqual( deserialized, util.DateRange(value=util.DateRangeBound(value=datetime.datetime( 2017, 2, 1, 15, 42, 12), precision='SECOND')))
def test_deserialize_open_low(self): serialized = (int8_pack(3) + int64_pack(self.timestamp) + int8_pack(4)) deserialized = DateRangeType.deserialize(serialized, 5) self.assertEqual( deserialized, util.DateRange(lower_bound=util.OPEN_BOUND, upper_bound=util.DateRangeBound( value=datetime.datetime(2017, 2, 1, 15, 42, 20, 1000), precision='MINUTE')))
def test_datetype(self): now_time_seconds = time.time() now_datetime = datetime.datetime.utcfromtimestamp(now_time_seconds) # Cassandra timestamps in millis now_timestamp = now_time_seconds * 1e3 # same results serialized self.assertEqual(DateType.serialize(now_datetime, 0), DateType.serialize(now_timestamp, 0)) # from timestamp date_type = DateType(now_timestamp) self.assertEqual(date_type.my_timestamp(), now_timestamp) # from datetime object date_type = DateType(now_datetime) self.assertEqual(date_type.my_timestamp(), now_datetime) # deserialize # epoc expected = 0 self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected)) # beyond 32b expected = 2**33 self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected)) # less than epoc (PYTHON-119) expected = -770172256 self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime(1945, 8, 5, 23, 15, 44)) self.assertRaises(ValueError, date_type.interpret_datestring, 'fakestring') # work around rounding difference among Python versions (PYTHON-230) expected = 1424817268.274 self.assertEqual( DateType.deserialize(int64_pack(int(1000 * expected)), 0), datetime.datetime(2015, 2, 24, 22, 34, 28, 274000))
def test_datetype(self): now_time_seconds = time.time() now_datetime = datetime.datetime.utcfromtimestamp(now_time_seconds) # Cassandra timestamps in millis now_timestamp = now_time_seconds * 1e3 # same results serialized self.assertEqual(DateType.serialize(now_datetime, 0), DateType.serialize(now_timestamp, 0)) # from timestamp date_type = DateType(now_timestamp) self.assertEqual(date_type.my_timestamp(), now_timestamp) # from datetime object date_type = DateType(now_datetime) self.assertEqual(date_type.my_timestamp(), now_datetime) # deserialize # epoc expected = 0 self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected)) # beyond 32b expected = 2 ** 33 self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime.utcfromtimestamp(expected)) # less than epoc (PYTHON-119) expected = -770172256 self.assertEqual(DateType.deserialize(int64_pack(1000 * expected), 0), datetime.datetime(1945, 8, 5, 23, 15, 44)) self.assertRaises(ValueError, date_type.interpret_datestring, 'fakestring') # work around rounding difference among Python versions (PYTHON-230) expected = 1424817268.274 self.assertEqual(DateType.deserialize(int64_pack(int(1000 * expected)), 0), datetime.datetime(2015, 2, 24, 22, 34, 28, 274000))