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)
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)
 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)))
Beispiel #4
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)
    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)])
    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)])
Beispiel #7
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")]
        check_sequence_consistency(self, dates_from_string)
        check_sequence_consistency(self, 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)]
        check_sequence_consistency(self, dates_from_value)
        check_sequence_consistency(self, 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))
        ]
        check_sequence_consistency(self, dates_from_datetime)
        check_sequence_consistency(self, 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")
        ]

        check_sequence_consistency(self, dates_from_date)
        check_sequence_consistency(self, dates_from_date_equal, equal=True)

        check_sequence_consistency(
            self,
            self._shuffle_lists(dates_from_string, dates_from_value,
                                dates_from_datetime, dates_from_date))
    (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\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
 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)
 def test_out_of_range(self):
     self.assertEqual(str(Date(2932897)), '2932897')
     self.assertEqual(repr(Date(1)), 'Date(1)')
 def test_str(self):
     date_str = '2015-03-16'
     self.assertEqual(str(Date(date_str)), date_str)
 def test_from_date(self):
     expected_date = datetime.date(1492, 10, 12)
     d = Date(expected_date)
     self.assertEqual(d.date(), expected_date)
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