def test_can_insert_udt_all_collection_datatypes(self): """ Test for inserting various types of COLLECTION_TYPES into UDT's """ c = Cluster(protocol_version=PROTOCOL_VERSION) s = c.connect("udttests") # create UDT alpha_type_list = [] start_index = ord('a') for i, collection_type in enumerate(COLLECTION_TYPES): for j, datatype in enumerate(PRIMITIVE_DATATYPES): if collection_type == "map": type_string = "{0}_{1} {2}<{3}, {3}>".format(chr(start_index + i), chr(start_index + j), collection_type, datatype) elif collection_type == "tuple": type_string = "{0}_{1} frozen<{2}<{3}>>".format(chr(start_index + i), chr(start_index + j), collection_type, datatype) else: type_string = "{0}_{1} {2}<{3}>".format(chr(start_index + i), chr(start_index + j), collection_type, datatype) alpha_type_list.append(type_string) s.execute(""" CREATE TYPE alldatatypes ({0}) """.format(', '.join(alpha_type_list)) ) s.execute("CREATE TABLE mytable (a int PRIMARY KEY, b frozen<alldatatypes>)") # register UDT alphabet_list = [] for i in range(ord('a'), ord('a') + len(COLLECTION_TYPES)): for j in range(ord('a'), ord('a') + len(PRIMITIVE_DATATYPES)): alphabet_list.append('{0}_{1}'.format(chr(i), chr(j))) Alldatatypes = namedtuple("alldatatypes", alphabet_list) c.register_user_type("udttests", "alldatatypes", Alldatatypes) # insert UDT data params = [] for collection_type in COLLECTION_TYPES: for datatype in PRIMITIVE_DATATYPES: params.append((get_collection_sample(collection_type, datatype))) insert = s.prepare("INSERT INTO mytable (a, b) VALUES (?, ?)") s.execute(insert, (0, Alldatatypes(*params))) # retrieve and verify data results = s.execute("SELECT * FROM mytable") self.assertEqual(1, len(results)) row = results[0].b for expected, actual in zip(params, row): self.assertEqual(expected, actual) c.shutdown()
def test_can_insert_collection_datatypes(self): """ Test insertion of all collection types """ c = Cluster(protocol_version=PROTOCOL_VERSION) s = c.connect(self.keyspace_name) # use tuple encoding, to convert native python tuple into raw CQL s.encoder.mapping[tuple] = s.encoder.cql_encode_tuple # create table alpha_type_list = ["zz int PRIMARY KEY"] col_names = ["zz"] start_index = ord('a') for i, collection_type in enumerate(COLLECTION_TYPES): for j, datatype in enumerate(PRIMITIVE_DATATYPES): if collection_type == "map": type_string = "{0}_{1} {2}<{3}, {3}>".format(chr(start_index + i), chr(start_index + j), collection_type, datatype) elif collection_type == "tuple": type_string = "{0}_{1} frozen<{2}<{3}>>".format(chr(start_index + i), chr(start_index + j), collection_type, datatype) else: type_string = "{0}_{1} {2}<{3}>".format(chr(start_index + i), chr(start_index + j), collection_type, datatype) alpha_type_list.append(type_string) col_names.append("{0}_{1}".format(chr(start_index + i), chr(start_index + j))) s.execute("CREATE TABLE allcoltypes ({0})".format(', '.join(alpha_type_list))) columns_string = ', '.join(col_names) # create the input for simple statement params = [0] for collection_type in COLLECTION_TYPES: for datatype in PRIMITIVE_DATATYPES: params.append((get_collection_sample(collection_type, datatype))) # insert into table as a simple statement placeholders = ', '.join(["%s"] * len(col_names)) s.execute("INSERT INTO allcoltypes ({0}) VALUES ({1})".format(columns_string, placeholders), params) # verify data results = s.execute("SELECT {0} FROM allcoltypes WHERE zz=0".format(columns_string))[0] for expected, actual in zip(params, results): self.assertEqual(actual, expected) # create the input for prepared statement params = [0] for collection_type in COLLECTION_TYPES: for datatype in PRIMITIVE_DATATYPES: params.append((get_collection_sample(collection_type, datatype))) # try the same thing with a prepared statement placeholders = ','.join(["?"] * len(col_names)) insert = s.prepare("INSERT INTO allcoltypes ({0}) VALUES ({1})".format(columns_string, placeholders)) s.execute(insert.bind(params)) # verify data results = s.execute("SELECT {0} FROM allcoltypes WHERE zz=0".format(columns_string))[0] for expected, actual in zip(params, results): self.assertEqual(actual, expected) # verify data with prepared statement query select = s.prepare("SELECT {0} FROM allcoltypes WHERE zz=?".format(columns_string)) results = s.execute(select.bind([0]))[0] for expected, actual in zip(params, results): self.assertEqual(actual, expected) # verify data with with prepared statement, use dictionary with no explicit columns s.row_factory = ordered_dict_factory select = s.prepare("SELECT * FROM allcoltypes") results = s.execute(select)[0] for expected, actual in zip(params, results.values()): self.assertEqual(actual, expected) c.shutdown()
def test_can_insert_udt_all_collection_datatypes(self): """ Test for inserting various types of COLLECTION_TYPES into UDT's """ c = TestCluster() s = c.connect(self.keyspace_name, wait_for_all_pools=True) # create UDT alpha_type_list = [] start_index = ord('a') for i, collection_type in enumerate(COLLECTION_TYPES): for j, datatype in enumerate(PRIMITIVE_DATATYPES_KEYS): if collection_type == "map": type_string = "{0}_{1} {2}<{3}, {3}>".format( chr(start_index + i), chr(start_index + j), collection_type, datatype) elif collection_type == "tuple": type_string = "{0}_{1} frozen<{2}<{3}>>".format( chr(start_index + i), chr(start_index + j), collection_type, datatype) else: type_string = "{0}_{1} {2}<{3}>".format( chr(start_index + i), chr(start_index + j), collection_type, datatype) alpha_type_list.append(type_string) s.execute(""" CREATE TYPE alldatatypes ({0}) """.format(', '.join(alpha_type_list))) s.execute( "CREATE TABLE mytable (a int PRIMARY KEY, b frozen<alldatatypes>)") # register UDT alphabet_list = [] for i in range(ord('a'), ord('a') + len(COLLECTION_TYPES)): for j in range(ord('a'), ord('a') + len(PRIMITIVE_DATATYPES_KEYS)): alphabet_list.append('{0}_{1}'.format(chr(i), chr(j))) Alldatatypes = namedtuple("alldatatypes", alphabet_list) c.register_user_type(self.keyspace_name, "alldatatypes", Alldatatypes) # insert UDT data params = [] for collection_type in COLLECTION_TYPES: for datatype in PRIMITIVE_DATATYPES_KEYS: params.append((get_collection_sample(collection_type, datatype))) insert = s.prepare("INSERT INTO mytable (a, b) VALUES (?, ?)") s.execute(insert, (0, Alldatatypes(*params))) # retrieve and verify data results = s.execute("SELECT * FROM mytable") row = results[0].b for expected, actual in zip(params, row): self.assertEqual(expected, actual) c.shutdown()