Exemplo n.º 1
0
class TestTime(ProtocolV4Test):
    def setUp(self):
        if PROTOCOL_VERSION < 4:
            raise unittest.SkipTest(
                "Protocol v4 datatypes require native protocol 4+, currently using: {0}"
                .format(PROTOCOL_VERSION))

        super(TestTime, self).setUp()

    column = columns.Time

    pkey_val = Time(time(2, 12, 7, 48))
    data_val = Time(time(16, 47, 25, 7))
    def test_can_insert_model_with_all_protocol_v4_column_types(self):
        """
        Test for inserting all protocol v4 column types into a Model

        test_can_insert_model_with_all_protocol_v4_column_types tests that each cqlengine protocol v4 column type can be
        inserted into a Model. It first creates a Model that has each cqlengine protocol v4 column type. It then creates
        a Model instance where all the fields have corresponding data, which performs the insert into the Cassandra table.
        Finally, it verifies that each column read from the Model from Cassandra is the same as the input parameters.

        @since 2.6.0
        @jira_ticket PYTHON-245
        @expected_result The Model is inserted with each protocol v4 column type, and the resulting read yields proper data for each column.

        @test_category data_types:primitive
        """

        if PROTOCOL_VERSION < 4:
            raise unittest.SkipTest(
                "Protocol v4 datatypes require native protocol 4+, currently using: {0}"
                .format(PROTOCOL_VERSION))

        class v4DatatypesModel(Model):
            id = columns.Integer(primary_key=True)
            a = columns.Date()
            b = columns.SmallInt()
            c = columns.Time()
            d = columns.TinyInt()

        sync_table(v4DatatypesModel)

        input = [Date(date(1970, 1, 1)), 32523, Time(time(16, 47, 25, 7)), 123]

        v4DatatypesModel.create(id=0,
                                a=date(1970, 1, 1),
                                b=32523,
                                c=time(16, 47, 25, 7),
                                d=123)

        self.assertEqual(1, v4DatatypesModel.objects.count())
        output = v4DatatypesModel.objects.first()

        for i, i_char in enumerate(range(ord('a'), ord('a') + 3)):
            self.assertEqual(input[i], output[chr(i_char)])
Exemplo n.º 3
0
    def test_can_insert_udts_protocol_v4_datatypes(self):
        """
        Test for inserting all protocol v4 column types into a UserType

        test_can_insert_udts_protocol_v4_datatypes tests that each protocol v4 cqlengine column type can be inserted
        into a UserType. It first creates a UserType that has each protocol v4 cqlengine column type, and a corresponding
        table/Model. It then creates a UserType instance where all the fields have corresponding data, and inserts the
        UserType as an instance of the Model. Finally, it verifies that each column read from the UserType from Cassandra
        is the same as the input parameters.

        @since 2.6.0
        @jira_ticket PYTHON-245
        @expected_result The UserType is inserted with each protocol v4 column type, and the resulting read yields proper data for each column.

        @test_category data_types:udt
        """

        if PROTOCOL_VERSION < 4:
            raise unittest.SkipTest("Protocol v4 datatypes in UDTs require native protocol 4+, currently using: {0}".format(PROTOCOL_VERSION))

        class Allv4Datatypes(UserType):
            a = columns.Date()
            b = columns.SmallInt()
            c = columns.Time()
            d = columns.TinyInt()

        class Allv4DatatypesModel(Model):
            id = columns.Integer(primary_key=True)
            data = columns.UserDefinedType(Allv4Datatypes)

        sync_table(Allv4DatatypesModel)
        self.addCleanup(drop_table, Allv4DatatypesModel)

        input = Allv4Datatypes(a=Date(date(1970, 1, 1)), b=32523, c=Time(time(16, 47, 25, 7)), d=123)
        Allv4DatatypesModel.create(id=0, data=input)

        self.assertEqual(1, Allv4DatatypesModel.objects.count())
        output = Allv4DatatypesModel.objects.first().data

        for i in range(ord('a'), ord('a') + 3):
            self.assertEqual(input[chr(i)], output[chr(i)])
Exemplo n.º 4
0
    def test_timer_order(self):
        """
        Test Time class is ordered consistently

        @since 3.9
        @jira_ticket PYTHON-714
        @expected_result the times are ordered correctly

        @test_category data_types
        """
        time_from_int = [Time(1000), Time(4000), Time(7000), Time(10000)]
        time_from_int_equal = [Time(1), Time(1)]
        check_sequence_consistency(self, time_from_int)
        check_sequence_consistency(self, time_from_int_equal, equal=True)

        time_from_datetime = [
            Time(datetime.time(hour=0, minute=0, second=0, microsecond=us))
            for us in (2, 5, 8, 11)
        ]
        time_from_datetime_equal = [
            Time(datetime.time(hour=0, minute=0, second=0, microsecond=us))
            for us in (1, 1)
        ]
        check_sequence_consistency(self, time_from_datetime)
        check_sequence_consistency(self, time_from_datetime_equal, equal=True)

        time_from_string = [
            Time("00:00:00.000003000"),
            Time("00:00:00.000006000"),
            Time("00:00:00.000009000"),
            Time("00:00:00.000012000")
        ]
        time_from_string_equal = [
            Time("00:00:00.000004000"),
            Time("00:00:00.000004000")
        ]
        check_sequence_consistency(self, time_from_string)
        check_sequence_consistency(self, time_from_string_equal, equal=True)

        check_sequence_consistency(
            self,
            self._shuffle_lists(time_from_int, time_from_datetime,
                                time_from_string))
Exemplo n.º 5
0
    (b'\xe3\x81\xbe\xe3\x81\x97\xe3\x81\xa6', 'UTF8Type', u'\u307e\u3057\u3066'),
    (b'\xe3\x81\xbe\xe3\x81\x97\xe3\x81\xa6' * 1000, 'UTF8Type', u'\u307e\u3057\u3066' * 1000),
    (b'', 'UTF8Type', u''),
    (b'\xff' * 16, 'UUIDType', UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')),
    (b'I\x15~\xfc\xef<\x9d\xe3\x16\x98\xaf\x80\x1f\xb4\x0b*', 'UUIDType', UUID('49157efc-ef3c-9de3-1698-af801fb40b2a')),
    (b'', 'UUIDType', None),
    (b'', 'MapType(AsciiType, BooleanType)', None),
    (b'', 'ListType(FloatType)', None),
    (b'', 'SetType(LongType)', None),
    (b'\x00\x00\x00\x00', 'MapType(DecimalType, BooleanType)', OrderedMapSerializedKey(DecimalType, 0)),
    (b'\x00\x00\x00\x00', 'ListType(FloatType)', []),
    (b'\x00\x00\x00\x00', 'SetType(IntegerType)', sortedset()),
    (b'\x00\x00\x00\x01\x00\x00\x00\x10\xafYC\xa3\xea<\x11\xe1\xabc\xc4,\x03"y\xf0', 'ListType(TimeUUIDType)', [UUID(bytes=b'\xafYC\xa3\xea<\x11\xe1\xabc\xc4,\x03"y\xf0')]),
    (b'\x80\x00\x00\x01', 'SimpleDateType', Date(1)),
    (b'\x7f\xff\xff\xff', 'SimpleDateType', Date('1969-12-31')),
    (b'\x00\x00\x00\x00\x00\x00\x00\x01', 'TimeType', Time(1)),
    (b'\x7f', 'ByteType', 127),
    (b'\x80', 'ByteType', -128),
    (b'\x7f\xff', 'ShortType', 32767),
    (b'\x80\x00', 'ShortType', -32768)
)

ordered_map_value = OrderedMapSerializedKey(UTF8Type, 2)
ordered_map_value._insert(u'\u307fbob', 199)
ordered_map_value._insert(u'', -1)
ordered_map_value._insert(u'\\', 0)

# these following entries work for me right now, but they're dependent on
# vagaries of internal python ordering for unordered types
marshalled_value_pairs_unsafe = (
    (b'\x00\x00\x00\x03\x00\x00\x00\x06\xe3\x81\xbfbob\x00\x00\x00\x04\x00\x00\x00\xc7\x00\x00\x00\x00\x00\x00\x00\x04\xff\xff\xff\xff\x00\x00\x00\x01\\\x00\x00\x00\x04\x00\x00\x00\x00', 'MapType(UTF8Type, Int32Type)', ordered_map_value),
Exemplo n.º 6
0
    def test_units_from_string(self):
        one_micro = 1000
        one_milli = 1000 * one_micro
        one_second = 1000 * one_milli
        one_minute = 60 * one_second
        one_hour = 60 * one_minute

        tt = Time('00:00:00.000000001')
        self.assertEqual(tt.nanosecond_time, 1)
        tt = Time('00:00:00.000001')
        self.assertEqual(tt.nanosecond_time, one_micro)
        tt = Time('00:00:00.001')
        self.assertEqual(tt.nanosecond_time, one_milli)
        tt = Time('00:00:01')
        self.assertEqual(tt.nanosecond_time, one_second)
        tt = Time('00:01:00')
        self.assertEqual(tt.nanosecond_time, one_minute)
        tt = Time('01:00:00')
        self.assertEqual(tt.nanosecond_time, one_hour)
        tt = Time('01:00:00.')
        self.assertEqual(tt.nanosecond_time, one_hour)

        tt = Time('23:59:59.123456')
        self.assertEqual(
            tt.nanosecond_time, 23 * one_hour + 59 * one_minute +
            59 * one_second + 123 * one_milli + 456 * one_micro)

        tt = Time('23:59:59.1234567')
        self.assertEqual(
            tt.nanosecond_time, 23 * one_hour + 59 * one_minute +
            59 * one_second + 123 * one_milli + 456 * one_micro + 700)

        tt = Time('23:59:59.12345678')
        self.assertEqual(
            tt.nanosecond_time, 23 * one_hour + 59 * one_minute +
            59 * one_second + 123 * one_milli + 456 * one_micro + 780)

        tt = Time('23:59:59.123456789')
        self.assertEqual(
            tt.nanosecond_time, 23 * one_hour + 59 * one_minute +
            59 * one_second + 123 * one_milli + 456 * one_micro + 789)
Exemplo n.º 7
0
 def test_str_repr(self):
     time_str = '12:13:14.123456789'
     self.assertEqual(str(Time(time_str)), time_str)
     self.assertEqual(repr(Time(1)), 'Time(1)')
Exemplo n.º 8
0
 def test_equals(self):
     # util.Time self equality
     self.assertEqual(Time(1234), Time(1234))
Exemplo n.º 9
0
 def test_as_time(self):
     expected_time = datetime.time(12, 1, 2, 3)
     tt = Time(expected_time)
     self.assertEqual(tt.time(), expected_time)
Exemplo n.º 10
0
 def test_from_int(self):
     tt = Time(12345678)
     self.assertEqual(tt.nanosecond_time, 12345678)
Exemplo n.º 11
0
 def test_micro_precision(self):
     Time('23:59:59.1')
     Time('23:59:59.12')
     Time('23:59:59.123')
     Time('23:59:59.1234')
     Time('23:59:59.12345')
def get_sample_data():
    sample_data = {}

    for datatype in PRIMITIVE_DATATYPES:
        if datatype == 'ascii':
            sample_data[datatype] = 'ascii'

        elif datatype == 'bigint':
            sample_data[datatype] = 2**63 - 1

        elif datatype == 'blob':
            sample_data[datatype] = bytearray(b'hello world')

        elif datatype == 'boolean':
            sample_data[datatype] = True

        elif datatype == 'decimal':
            sample_data[datatype] = Decimal('12.3E+7')

        elif datatype == 'double':
            sample_data[datatype] = 1.23E+8

        elif datatype == 'float':
            sample_data[datatype] = 3.4028234663852886e+38

        elif datatype == 'inet':
            sample_data[datatype] = '123.123.123.123'

        elif datatype == 'int':
            sample_data[datatype] = 2147483647

        elif datatype == 'text':
            sample_data[datatype] = 'text'

        elif datatype == 'timestamp':
            sample_data[datatype] = datetime(2013, 12, 31, 23, 59, 59, 999000)

        elif datatype == 'timeuuid':
            sample_data[datatype] = uuid1()

        elif datatype == 'uuid':
            sample_data[datatype] = uuid4()

        elif datatype == 'varchar':
            sample_data[datatype] = 'varchar'

        elif datatype == 'varint':
            sample_data[datatype] = int(str(2147483647) + '000')

        elif datatype == 'date':
            sample_data[datatype] = Date(date(2015, 1, 15))

        elif datatype == 'time':
            sample_data[datatype] = Time(time(16, 47, 25, 7))

        elif datatype == 'tinyint':
            sample_data[datatype] = 123

        elif datatype == 'smallint':
            sample_data[datatype] = 32523

        elif datatype == 'duration':
            sample_data[datatype] = Duration(months=2,
                                             days=12,
                                             nanoseconds=21231)

        else:
            raise Exception("Missing handling of {0}".format(datatype))

    return sample_data