def test_str(self): self.assertEqual(str(Duration(1, 1, 1)), "1mo1d1ns") self.assertEqual(str(Duration(1, 1, -1)), "-1mo1d1ns") self.assertEqual(str(Duration(1, 1, 1000000000000000)), "1mo1d1000000000000000ns") self.assertEqual(str(Duration(52, 23, 564564)), "52mo23d564564ns")
def test_equality(self): first = Duration(1, 1, 1) second = Duration(-1, 1, 1) self.assertNotEqual(first, second) first = Duration(1, 1, 1) second = Duration(1, 1, 1) self.assertEqual(first, second) first = Duration() second = Duration(0, 0, 0) self.assertEqual(first, second) first = Duration(1000, 10000, 2345345) second = Duration(1000, 10000, 2345345) self.assertEqual(first, second) first = Duration(12, 0, 100) second = Duration(nanoseconds=100, months=12) self.assertEqual(first, second)
def test_valid_format(self): valid = Duration(1, 1, 1) self.assertEqual(valid.months, 1) self.assertEqual(valid.days, 1) self.assertEqual(valid.nanoseconds, 1) valid = Duration(nanoseconds=100000) self.assertEqual(valid.months, 0) self.assertEqual(valid.days, 0) self.assertEqual(valid.nanoseconds, 100000) valid = Duration() self.assertEqual(valid.months, 0) self.assertEqual(valid.days, 0) self.assertEqual(valid.nanoseconds, 0) valid = Duration(-10, -21, -1000) self.assertEqual(valid.months, -10) self.assertEqual(valid.days, -21) self.assertEqual(valid.nanoseconds, -1000)
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
def test_can_insert_model_with_all_column_types(self): """ Test for inserting all column types into a Model test_can_insert_model_with_all_column_types tests that each cqlengine column type can be inserted into a Model. It first creates a Model that has each cqlengine 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-246 @expected_result The Model is inserted with each column type, and the resulting read yields proper data for each column. @test_category data_types:primitive """ class AllDatatypesModel(Model): id = columns.Integer(primary_key=True) a = columns.Ascii() b = columns.BigInt() c = columns.Blob() d = columns.Boolean() e = columns.DateTime() f = columns.Decimal() g = columns.Double() h = columns.Float() i = columns.Inet() j = columns.Integer() k = columns.Text() l = columns.TimeUUID() m = columns.UUID() n = columns.VarInt() o = columns.Duration() sync_table(AllDatatypesModel) input = [ 'ascii', 2**63 - 1, bytearray(b'hello world'), True, datetime.utcfromtimestamp(872835240), Decimal('12.3E+7'), 2.39, 3.4028234663852886e+38, '123.123.123.123', 2147483647, 'text', UUID('FE2B4360-28C6-11E2-81C1-0800200C9A66'), UUID('067e6162-3b6f-4ae2-a171-2470b63dff00'), int(str(2147483647) + '000') ] AllDatatypesModel.create( id=0, a='ascii', b=2**63 - 1, c=bytearray(b'hello world'), d=True, e=datetime.utcfromtimestamp(872835240), f=Decimal('12.3E+7'), g=2.39, h=3.4028234663852886e+38, i='123.123.123.123', j=2147483647, k='text', l=UUID('FE2B4360-28C6-11E2-81C1-0800200C9A66'), m=UUID('067e6162-3b6f-4ae2-a171-2470b63dff00'), n=int(str(2147483647) + '000'), o=Duration(2, 3, 4)) self.assertEqual(1, AllDatatypesModel.objects.count()) output = AllDatatypesModel.objects.first() for i, i_char in enumerate(range(ord('a'), ord('a') + 14)): self.assertEqual(input[i], output[chr(i_char)])