def test_can_insert_udts_with_nulls(self): """ Test the insertion of UDTs with null and empty string fields """ c = Cluster(protocol_version=PROTOCOL_VERSION) s = c.connect(self.keyspace_name, wait_for_all_pools=True) s.execute("CREATE TYPE user (a text, b int, c uuid, d blob)") User = namedtuple('user', ('a', 'b', 'c', 'd')) c.register_user_type(self.keyspace_name, "user", User) s.execute("CREATE TABLE mytable (a int PRIMARY KEY, b frozen<user>)") insert = s.prepare("INSERT INTO mytable (a, b) VALUES (0, ?)") s.execute(insert, [User(None, None, None, None)]) results = s.execute("SELECT b FROM mytable WHERE a=0") self.assertEqual((None, None, None, None), results[0].b) select = s.prepare("SELECT b FROM mytable WHERE a=0") self.assertEqual((None, None, None, None), s.execute(select)[0].b) # also test empty strings s.execute(insert, [User('', None, None, six.binary_type())]) results = s.execute("SELECT b FROM mytable WHERE a=0") self.assertEqual(('', None, None, six.binary_type()), results[0].b) c.shutdown()
def test_can_insert_prepared_registered_udts(self): """ Test the insertion of prepared, registered UDTs """ c = Cluster(protocol_version=PROTOCOL_VERSION) s = c.connect(self.keyspace_name, wait_for_all_pools=True) s.execute("CREATE TYPE user (age int, name text)") User = namedtuple('user', ('age', 'name')) c.register_user_type(self.keyspace_name, "user", User) s.execute("CREATE TABLE mytable (a int PRIMARY KEY, b frozen<user>)") insert = s.prepare("INSERT INTO mytable (a, b) VALUES (?, ?)") s.execute(insert, (0, User(42, 'bob'))) select = s.prepare("SELECT b FROM mytable WHERE a=?") result = s.execute(select, (0, )) row = result[0] self.assertEqual(42, row.b.age) self.assertEqual('bob', row.b.name) self.assertTrue(type(row.b) is User) # use the same UDT name in a different keyspace s.execute(""" CREATE KEYSPACE udt_test_prepared_registered2 WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor': '1' } """) s.set_keyspace("udt_test_prepared_registered2") s.execute("CREATE TYPE user (state text, is_cool boolean)") User = namedtuple('user', ('state', 'is_cool')) c.register_user_type("udt_test_prepared_registered2", "user", User) s.execute("CREATE TABLE mytable (a int PRIMARY KEY, b frozen<user>)") insert = s.prepare("INSERT INTO mytable (a, b) VALUES (?, ?)") s.execute(insert, (0, User('Texas', True))) select = s.prepare("SELECT b FROM mytable WHERE a=?") result = s.execute(select, (0, )) row = result[0] self.assertEqual('Texas', row.b.state) self.assertEqual(True, row.b.is_cool) self.assertTrue(type(row.b) is User) s.execute("DROP KEYSPACE udt_test_prepared_registered2") c.shutdown()
def test_can_insert_udt_all_datatypes(self): """ Test for inserting various types of PRIMITIVE_DATATYPES into UDT's """ c = Cluster(protocol_version=PROTOCOL_VERSION) s = c.connect(self.keyspace_name, wait_for_all_pools=True) # create UDT alpha_type_list = [] start_index = ord('a') for i, datatype in enumerate(PRIMITIVE_DATATYPES): alpha_type_list.append("{0} {1}".format(chr(start_index + i), datatype)) 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(PRIMITIVE_DATATYPES)): alphabet_list.append('{0}'.format(chr(i))) Alldatatypes = namedtuple("alldatatypes", alphabet_list) c.register_user_type(self.keyspace_name, "alldatatypes", Alldatatypes) # insert UDT data params = [] for datatype in PRIMITIVE_DATATYPES: params.append((get_sample(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()
def test_can_insert_udts_with_varying_lengths(self): """ Test for ensuring extra-lengthy udts are properly inserted """ c = Cluster(protocol_version=PROTOCOL_VERSION) s = c.connect(self.keyspace_name, wait_for_all_pools=True) max_test_length = 254 # create the seed udt, increase timeout to avoid the query failure on slow systems s.execute("CREATE TYPE lengthy_udt ({0})".format(', '.join( ['v_{0} int'.format(i) for i in range(max_test_length)]))) # create a table with multiple sizes of nested udts # no need for all nested types, only a spot checked few and the largest one s.execute("CREATE TABLE mytable (" "k int PRIMARY KEY, " "v frozen<lengthy_udt>)") # create and register the seed udt type udt = namedtuple( 'lengthy_udt', tuple(['v_{0}'.format(i) for i in range(max_test_length)])) c.register_user_type(self.keyspace_name, "lengthy_udt", udt) # verify inserts and reads for i in (0, 1, 2, 3, max_test_length): # create udt params = [j for j in range(i) ] + [None for j in range(max_test_length - i)] created_udt = udt(*params) # write udt s.execute("INSERT INTO mytable (k, v) VALUES (0, %s)", (created_udt, )) # verify udt was written and read correctly, increase timeout to avoid the query failure on slow systems result = s.execute("SELECT v FROM mytable WHERE k=0")[0] self.assertEqual(created_udt, result.v) c.shutdown()
def test_raise_error_on_nonexisting_udts(self): """ Test for ensuring that an error is raised for operating on a nonexisting udt or an invalid keyspace """ c = Cluster(protocol_version=PROTOCOL_VERSION) s = c.connect(self.keyspace_name, wait_for_all_pools=True) User = namedtuple('user', ('age', 'name')) with self.assertRaises(UserTypeDoesNotExist): c.register_user_type("some_bad_keyspace", "user", User) with self.assertRaises(UserTypeDoesNotExist): c.register_user_type("system", "user", User) with self.assertRaises(InvalidRequest): s.execute( "CREATE TABLE mytable (a int PRIMARY KEY, b frozen<user>)") c.shutdown()
def test_can_register_udt_before_connecting(self): """ Test the registration of UDTs before session creation """ c = Cluster(protocol_version=PROTOCOL_VERSION) s = c.connect(wait_for_all_pools=True) s.execute(""" CREATE KEYSPACE udt_test_register_before_connecting WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor': '1' } """) s.set_keyspace("udt_test_register_before_connecting") s.execute("CREATE TYPE user (age int, name text)") s.execute("CREATE TABLE mytable (a int PRIMARY KEY, b frozen<user>)") s.execute(""" CREATE KEYSPACE udt_test_register_before_connecting2 WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor': '1' } """) s.set_keyspace("udt_test_register_before_connecting2") s.execute("CREATE TYPE user (state text, is_cool boolean)") s.execute("CREATE TABLE mytable (a int PRIMARY KEY, b frozen<user>)") # now that types are defined, shutdown and re-create Cluster c.shutdown() c = Cluster(protocol_version=PROTOCOL_VERSION) User1 = namedtuple('user', ('age', 'name')) User2 = namedtuple('user', ('state', 'is_cool')) c.register_user_type("udt_test_register_before_connecting", "user", User1) c.register_user_type("udt_test_register_before_connecting2", "user", User2) s = c.connect(wait_for_all_pools=True) s.set_keyspace("udt_test_register_before_connecting") s.execute("INSERT INTO mytable (a, b) VALUES (%s, %s)", (0, User1(42, 'bob'))) result = s.execute("SELECT b FROM mytable WHERE a=0") row = result[0] self.assertEqual(42, row.b.age) self.assertEqual('bob', row.b.name) self.assertTrue(type(row.b) is User1) # use the same UDT name in a different keyspace s.set_keyspace("udt_test_register_before_connecting2") s.execute("INSERT INTO mytable (a, b) VALUES (%s, %s)", (0, User2('Texas', True))) result = s.execute("SELECT b FROM mytable WHERE a=0") row = result[0] self.assertEqual('Texas', row.b.state) self.assertEqual(True, row.b.is_cool) self.assertTrue(type(row.b) is User2) s.execute("DROP KEYSPACE udt_test_register_before_connecting") s.execute("DROP KEYSPACE udt_test_register_before_connecting2") c.shutdown()