Пример #1
0
 def test_from_string(self):
     expected_date = datetime.date(1492, 10, 12)
     d = Date(expected_date)
     sd = Date('1492-10-12')
     self.assertEqual(sd, d)
     sd = Date('+1492-10-12')
     self.assertEqual(sd, d)
Пример #2
0
    def test_convert_value(self):
        op = CassandraToGoogleCloudStorageOperator
        self.assertEqual(op.convert_value("None", None), None)
        self.assertEqual(op.convert_value("int", 1), 1)
        self.assertEqual(op.convert_value("float", 1.0), 1.0)
        self.assertEqual(op.convert_value("str", "text"), "text")
        self.assertEqual(op.convert_value("bool", True), True)
        self.assertEqual(op.convert_value("dict", {"a": "b"}), {"a": "b"})

        from datetime import datetime

        now = datetime.now()
        self.assertEqual(op.convert_value("datetime", now), str(now))

        from cassandra.util import Date

        date_str = "2018-01-01"
        date = Date(date_str)
        self.assertEqual(op.convert_value("date", date), str(date_str))

        import uuid
        from base64 import b64encode

        test_uuid = uuid.uuid4()
        encoded_uuid = b64encode(test_uuid.bytes).decode("ascii")
        self.assertEqual(op.convert_value("uuid", test_uuid), encoded_uuid)

        b = b"abc"
        encoded_b = b64encode(b).decode("ascii")
        self.assertEqual(op.convert_value("binary", b), encoded_b)

        from decimal import Decimal

        d = Decimal(1.0)
        self.assertEqual(op.convert_value("decimal", d), float(d))

        from cassandra.util import Time

        time = Time(0)
        self.assertEqual(op.convert_value("time", time), "00:00:00")

        date_str_lst = ["2018-01-01", "2018-01-02", "2018-01-03"]
        date_lst = [Date(d) for d in date_str_lst]
        self.assertEqual(op.convert_value("list", date_lst), date_str_lst)

        date_tpl = tuple(date_lst)
        self.assertEqual(
            op.convert_value("tuple", date_tpl),
            {
                "field_0": "2018-01-01",
                "field_1": "2018-01-02",
                "field_2": "2018-01-03"
            },
        )
Пример #3
0
    def test_convert_value(self):
        op = CassandraToGCSOperator
        self.assertEqual(op.convert_value(None), None)
        self.assertEqual(op.convert_value(1), 1)
        self.assertEqual(op.convert_value(1.0), 1.0)
        self.assertEqual(op.convert_value("text"), "text")
        self.assertEqual(op.convert_value(True), True)
        self.assertEqual(op.convert_value({"a": "b"}), {"a": "b"})

        from datetime import datetime

        now = datetime.now()
        self.assertEqual(op.convert_value(now), str(now))

        from cassandra.util import Date

        date_str = "2018-01-01"
        date = Date(date_str)
        self.assertEqual(op.convert_value(date), str(date_str))

        import uuid
        from base64 import b64encode

        test_uuid = uuid.uuid4()
        encoded_uuid = b64encode(test_uuid.bytes).decode("ascii")
        self.assertEqual(op.convert_value(test_uuid), encoded_uuid)

        byte_str = b"abc"
        encoded_b = b64encode(byte_str).decode("ascii")
        self.assertEqual(op.convert_value(byte_str), encoded_b)

        from decimal import Decimal

        decimal = Decimal(1.0)
        self.assertEqual(op.convert_value(decimal), float(decimal))

        from cassandra.util import Time

        time = Time(0)
        self.assertEqual(op.convert_value(time), "00:00:00")

        date_str_lst = ["2018-01-01", "2018-01-02", "2018-01-03"]
        date_lst = [Date(d) for d in date_str_lst]
        self.assertEqual(op.convert_value(date_lst), date_str_lst)

        date_tpl = tuple(date_lst)
        self.assertEqual(
            op.convert_value(date_tpl),
            {
                "field_0": "2018-01-01",
                "field_1": "2018-01-02",
                "field_2": "2018-01-03"
            },
        )
Пример #4
0
class TestDate(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(TestDate, self).setUp()

    column = columns.Date

    now = Date(datetime.now().date())
    pkey_val = now
    data_val = Date(now.days_from_epoch + 1)
Пример #5
0
    def test_convert_value(self):
        op = CassandraToGCSOperator
        assert op.convert_value(None) is None
        assert op.convert_value(1) == 1
        assert op.convert_value(1.0) == 1.0
        assert op.convert_value("text") == "text"
        assert op.convert_value(True) is True
        assert op.convert_value({"a": "b"}) == {"a": "b"}

        from datetime import datetime

        now = datetime.now()
        assert op.convert_value(now) == str(now)

        from cassandra.util import Date

        date_str = "2018-01-01"
        date = Date(date_str)
        assert op.convert_value(date) == str(date_str)

        import uuid
        from base64 import b64encode

        test_uuid = uuid.uuid4()
        encoded_uuid = b64encode(test_uuid.bytes).decode("ascii")
        assert op.convert_value(test_uuid) == encoded_uuid

        byte_str = b"abc"
        encoded_b = b64encode(byte_str).decode("ascii")
        assert op.convert_value(byte_str) == encoded_b

        from decimal import Decimal

        decimal = Decimal(1.0)
        assert op.convert_value(decimal) == float(decimal)

        from cassandra.util import Time

        time = Time(0)
        assert op.convert_value(time) == "00:00:00"

        date_str_lst = ["2018-01-01", "2018-01-02", "2018-01-03"]
        date_lst = [Date(d) for d in date_str_lst]
        assert op.convert_value(date_lst) == date_str_lst

        date_tpl = tuple(date_lst)
        assert op.convert_value(date_tpl) == {
            "field_0": "2018-01-01",
            "field_1": "2018-01-02",
            "field_2": "2018-01-03",
        }
Пример #6
0
    def test_convert_value(self):
        op = CassandraToGoogleCloudStorageOperator
        self.assertEqual(op.convert_value('None', None), None)
        self.assertEqual(op.convert_value('int', 1), 1)
        self.assertEqual(op.convert_value('float', 1.0), 1.0)
        self.assertEqual(op.convert_value('str', "text"), "text")
        self.assertEqual(op.convert_value('bool', True), True)
        self.assertEqual(op.convert_value('dict', {"a": "b"}), {"a": "b"})

        from datetime import datetime
        now = datetime.now()
        self.assertEqual(op.convert_value('datetime', now), str(now))

        from cassandra.util import Date
        date_str = '2018-01-01'
        date = Date(date_str)
        self.assertEqual(op.convert_value('date', date), str(date_str))

        import uuid
        from base64 import b64encode
        test_uuid = uuid.uuid4()
        encoded_uuid = b64encode(test_uuid.bytes).decode('ascii')
        self.assertEqual(op.convert_value('uuid', test_uuid), encoded_uuid)

        b = b'abc'
        encoded_b = b64encode(b).decode('ascii')
        self.assertEqual(op.convert_value('binary', b), encoded_b)

        from decimal import Decimal
        d = Decimal(1.0)
        self.assertEqual(op.convert_value('decimal', d), float(d))

        from cassandra.util import Time
        time = Time(0)
        self.assertEqual(op.convert_value('time', time), '00:00:00')

        date_str_lst = ['2018-01-01', '2018-01-02', '2018-01-03']
        date_lst = [Date(d) for d in date_str_lst]
        self.assertEqual(op.convert_value('list', date_lst), date_str_lst)

        date_tpl = tuple(date_lst)
        self.assertEqual(
            op.convert_value('tuple', date_tpl), {
                'field_0': '2018-01-01',
                'field_1': '2018-01-02',
                'field_2': '2018-01-03',
            })
Пример #7
0
class CateringBoyOrder(DjangoCassandraModel):
    id = columns.UUID(primary_key=True, default=uuid.uuid4)
    cid = columns.UUID()
    venue = columns.Map(columns.Text(), columns.Text())
    amount = columns.Integer(default=300)
    catboy_max = columns.Integer(default=0)
    timing = columns.Time(default=Time('19:00:00'))
    date = columns.Date(
        default=Date(datetime.date.today().strftime("%Y-%m-%d")))
    count = columns.Integer(default=0)
Пример #8
0
    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(self.conn, v4DatatypesModel)

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

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

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

        for i, i_char in enumerate(range(ord('a'), ord('a') + 3)):
            self.assertEqual(input[i], output[chr(i_char)])
Пример #9
0
def test_timeuuid(cql, test_keyspace):
    schema = "a int, b timeuuid, primary key (a,b)"
    with new_test_table(cql, test_keyspace, schema) as table:
        cql.execute(
            f'insert into {table} (a, b) values (0, 13814000-1dd2-11ff-8080-808080808080)'
        )
        cql.execute(
            f'insert into {table} (a, b) values (0, 6b1b3620-33fd-11eb-8080-808080808080)'
        )
        assert project(
            'system_todate_system_min_b',
            cql.execute(
                f'select todate(min(b)) from {table} where a = 0')) == [
                    Date('2020-12-01')
                ]
        assert project(
            'system_todate_system_max_b',
            cql.execute(
                f'select todate(max(b)) from {table} where a = 0')) == [
                    Date('2038-09-06')
                ]
Пример #10
0
 def test_from_days(self):
     sd = Date(0)
     self.assertEqual(sd, Date(datetime.date(1970, 1, 1)))
     sd = Date(-1)
     self.assertEqual(sd, Date(datetime.date(1969, 12, 31)))
     sd = Date(1)
     self.assertEqual(sd, Date(datetime.date(1970, 1, 2)))
Пример #11
0
class CateringServiceOrder(DjangoCassandraModel):
    receiver_id = columns.UUID(primary_key=True)
    booker_id = columns.UUID(primary_key=True)
    reg_type = columns.Text(primary_key=True)
    date = columns.Date(primary_key=True,
                        default=Date(
                            datetime.date.today().strftime("%Y-%m-%d")))
    amount = columns.Integer()
    plates = columns.Integer()
    foodlist = columns.Map(columns.Text, columns.List(columns.Text))
    address = columns.Text()
    status = columns.Text()

    class Meta:
        get_pk_field = 'receiver_id'
Пример #12
0
 def test_limits(self):
     min_builtin = Date(datetime.date(1, 1, 1))
     max_builtin = Date(datetime.date(9999, 12, 31))
     self.assertEqual(Date(min_builtin.days_from_epoch), min_builtin)
     self.assertEqual(Date(max_builtin.days_from_epoch), max_builtin)
     # just proving we can construct with on offset outside buildin range
     self.assertEqual(Date(min_builtin.days_from_epoch - 1).days_from_epoch,
                      min_builtin.days_from_epoch - 1)
     self.assertEqual(Date(max_builtin.days_from_epoch + 1).days_from_epoch,
                      max_builtin.days_from_epoch + 1)
Пример #13
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(self.conn, Allv4DatatypesModel)

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

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

        for i in range(ord("a"), ord("a") + 3):
            self.assertEqual(input[chr(i)], output[chr(i)])
Пример #14
0
    def test_date_order(self):
        """
        Test Date class is ordered consistently

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

        @test_category data_types
        """
        dates_from_string = [
            Date("2017-01-01"),
            Date("2017-01-05"),
            Date("2017-01-09"),
            Date("2017-01-13")
        ]
        dates_from_string_equal = [Date("2017-01-01"), Date("2017-01-01")]
        self._check_sequence_consistency(dates_from_string)
        self._check_sequence_consistency(dates_from_string_equal, equal=True)

        date_format = "%Y-%m-%d"

        dates_from_value = [
            Date((datetime.datetime.strptime(dtstr, date_format) -
                  datetime.datetime(1970, 1, 1)).days)
            for dtstr in ("2017-01-02", "2017-01-06", "2017-01-10",
                          "2017-01-14")
        ]
        dates_from_value_equal = [Date(1), Date(1)]
        self._check_sequence_consistency(dates_from_value)
        self._check_sequence_consistency(dates_from_value_equal, equal=True)

        dates_from_datetime = [
            Date(datetime.datetime.strptime(dtstr, date_format))
            for dtstr in ("2017-01-03", "2017-01-07", "2017-01-11",
                          "2017-01-15")
        ]
        dates_from_datetime_equal = [
            Date(datetime.datetime.strptime("2017-01-01", date_format)),
            Date(datetime.datetime.strptime("2017-01-01", date_format))
        ]
        self._check_sequence_consistency(dates_from_datetime)
        self._check_sequence_consistency(dates_from_datetime_equal, equal=True)

        dates_from_date = [
            Date(datetime.datetime.strptime(dtstr, date_format).date())
            for dtstr in ("2017-01-04", "2017-01-08", "2017-01-12",
                          "2017-01-16")
        ]
        dates_from_date_equal = [
            datetime.datetime.strptime(dtstr, date_format)
            for dtstr in ("2017-01-09", "2017-01-9")
        ]

        self._check_sequence_consistency(dates_from_date)
        self._check_sequence_consistency(dates_from_date_equal, equal=True)

        self._check_sequence_consistency(
            self._shuffle_lists(dates_from_string, dates_from_value,
                                dates_from_datetime, dates_from_date))
Пример #15
0
 def test_from_date(self):
     expected_date = datetime.date(1492, 10, 12)
     d = Date(expected_date)
     self.assertEqual(d.date(), expected_date)
Пример #16
0
    (b'A46\xa9', 'InetAddressType', '65.52.54.169'),
    (b'*\x00\x13(\xe1\x02\xcc\xc0\x00\x00\x00\x00\x00\x00\x01"', 'InetAddressType', '2a00:1328:e102:ccc0::122'),
    (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', 'MapType(DecimalType, BooleanType)', OrderedMapSerializedKey(DecimalType, 0)),
    (b'\x00\x00', 'ListType(FloatType)', []),
    (b'\x00\x00', 'SetType(IntegerType)', sortedset()),
    (b'\x00\x01\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
Пример #17
0
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',
                                     '2001:db8:85a3:8d3:1319:8a2e:370:7348')
            if six.PY3:
                import ipaddress
                sample_data[datatype] += (
                    ipaddress.IPv4Address("123.123.123.123"),
                    ipaddress.IPv6Address(
                        '2001:db8:85a3:8d3:1319:8a2e:370:7348'))

        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
Пример #18
0
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

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

    return sample_data
Пример #19
0
 def test_from_date(self):
     expected_date = datetime.date(1492, 10, 12)
     d = Date(expected_date)
     self.assertEqual(d.date(), expected_date)
Пример #20
0
 def test_equals(self):
     self.assertEqual(Date(1234), 1234)
     self.assertEqual(Date(1), datetime.date(1970, 1, 2))
     self.assertFalse(Date(2932897) == datetime.date(
         9999, 12, 31))  # date can't represent year > 9999
     self.assertEqual(Date(2932897), 2932897)
Пример #21
0
 def test_out_of_range(self):
     self.assertEqual(str(Date(2932897)), '2932897')
     self.assertEqual(repr(Date(1)), 'Date(1)')
Пример #22
0
 def test_str(self):
     date_str = '2015-03-16'
     self.assertEqual(str(Date(date_str)), date_str)