def _reset_data(self): for ks in self.keyspaces: drop_keyspace(ks, connections=self.conns) for ks in self.keyspaces: create_keyspace_simple(ks, 1, connections=self.conns) sync_table(TestModel, keyspaces=self.keyspaces, connections=self.conns)
def initialise(keyspace="indigo", hosts=('127.0.0.1', ), strategy='SimpleStrategy', repl_factor=1): """Initialise Cassandra connection""" num_retries = 6 retry_timeout = 1 for retry in xrange(num_retries): try: logger.info('Connecting to Cassandra keyspace "{2}" ' 'on "{0}" with strategy "{1}"'.format( hosts, strategy, keyspace)) connection.setup(hosts, keyspace, protocol_version=3) if strategy is 'SimpleStrategy': create_keyspace_simple(keyspace, repl_factor, True) else: create_keyspace_network_topology(keyspace, {}, True) break except dse.cluster.NoHostAvailable: logger.warning( 'Unable to connect to Cassandra. Retrying in {0} seconds...'. format(retry_timeout)) time.sleep(retry_timeout) retry_timeout *= 2
def initialise(): """Initialise the Cassandra connection repl_factor, dc_replication_map, keyspace variables are used to configure the connection. See the cfg object in the :mod:`radon.model.config` module, . :return: A boolean which indicates if the connection is successful :rtype: bool """ if not connect(): return False repl_factor = cfg.dse_repl_factor dc_replication_map = cfg.dse_dc_replication_map keyspace = cfg.dse_keyspace cluster = connection.get_cluster() if keyspace in cluster.metadata.keyspaces: # If the keyspace already exists we do not create it. Should we raise # an error return True if cfg.dse_strategy == "NetworkTopologyStrategy": create_keyspace_network_topology(keyspace, dc_replication_map, True) else: create_keyspace_simple(keyspace, repl_factor, True) return True
def test_destroy(): cfg.dse_keyspace = TEST_KEYSPACE initialise() create_keyspace_simple(TEST_KEYSPACE, 1, True) cluster = connection.get_cluster() assert TEST_KEYSPACE in cluster.metadata.keyspaces destroy() assert TEST_KEYSPACE not in cluster.metadata.keyspaces
def setup_package(): warnings.simplefilter( 'always') # for testing warnings, make sure all are let through os.environ[CQLENG_ALLOW_SCHEMA_MANAGEMENT] = '1' set_default_dse_ip() use_single_node() setup_connection(DEFAULT_KEYSPACE) create_keyspace_simple(DEFAULT_KEYSPACE, 1)
def test_can_create_same_udt_different_keyspaces(self): class User(UserType): age = columns.Integer() name = columns.Text() sync_type("cqlengine_test", User) create_keyspace_simple("simplex", 1) sync_type("simplex", User) drop_keyspace("simplex")
def test_keywords_as_names(self): """ Test for CQL keywords as names test_keywords_as_names tests that CQL keywords are properly and automatically quoted in cqlengine. It creates a keyspace, keyspace, which should be automatically quoted to "keyspace" in CQL. It then creates a table, table, which should also be automatically quoted to "table". It then verfies that operations can be done on the "keyspace"."table" which has been created. It also verifies that table alternations work and operations can be performed on the altered table. @since 2.6.0 @jira_ticket PYTHON-244 @expected_result Cqlengine should quote CQL keywords properly when creating keyspaces and tables. @test_category schema:generation """ # If the keyspace exists, it will not be re-created create_keyspace_simple('keyspace', 1) class table(Model): __keyspace__ = 'keyspace' select = columns.Integer(primary_key=True) table = columns.Text() # In case the table already exists in keyspace drop_table(table) # Create should work sync_table(table) created = table.create(select=0, table='table') selected = table.objects(select=0)[0] self.assertEqual(created.select, selected.select) self.assertEqual(created.table, selected.table) # Alter should work class table(Model): __keyspace__ = 'keyspace' select = columns.Integer(primary_key=True) table = columns.Text() where = columns.Text() sync_table(table) created = table.create(select=1, table='table') selected = table.objects(select=1)[0] self.assertEqual(created.select, selected.select) self.assertEqual(created.table, selected.table) self.assertEqual(created.where, selected.where) drop_keyspace('keyspace')
def setUpClass(cls): super(ContextQueryConnectionTests, cls).setUpClass() create_keyspace_simple('ks1', 1) conn.unregister_connection('default') conn.register_connection('fake_cluster', ['127.0.0.100'], lazy_connect=True, retry_connect=True, default=True) conn.register_connection('cluster', [DSE_IP]) with ContextQuery(TestModel, connection='cluster') as tm: sync_table(tm)
def setUpClass(cls): super(BatchQueryConnectionTests, cls).setUpClass() create_keyspace_simple('ks1', 1) sync_table(TestModel) sync_table(AnotherTestModel) conn.unregister_connection('default') conn.register_connection('fake_cluster', ['127.0.0.100'], lazy_connect=True, retry_connect=True, default=True) conn.register_connection('cluster', [DSE_IP])
def test_create_drop_succeeeds(self): cluster = get_cluster() keyspace_ss = 'test_ks_ss' self.assertNotIn(keyspace_ss, cluster.metadata.keyspaces) management.create_keyspace_simple(keyspace_ss, 2) self.assertIn(keyspace_ss, cluster.metadata.keyspaces) management.drop_keyspace(keyspace_ss) self.assertNotIn(keyspace_ss, cluster.metadata.keyspaces) keyspace_nts = 'test_ks_nts' self.assertNotIn(keyspace_nts, cluster.metadata.keyspaces) management.create_keyspace_network_topology(keyspace_nts, {'dc1': 1}) self.assertIn(keyspace_nts, cluster.metadata.keyspaces) management.drop_keyspace(keyspace_nts) self.assertNotIn(keyspace_nts, cluster.metadata.keyspaces)
def test_create_drop_keyspace(self): """ Tests drop and create keyspace with connections explicitly set @since 3.7 @jira_ticket PYTHON-613 @expected_result keyspaces should be created and dropped @test_category object_mapper """ # No connection (default is fake) with self.assertRaises(NoHostAvailable): create_keyspace_simple(self.keyspaces[0], 1) # Explicit connections for ks in self.keyspaces: create_keyspace_simple(ks, 1, connections=self.conns) for ks in self.keyspaces: drop_keyspace(ks, connections=self.conns)
def test_create_drop_table(self): """ Tests drop and create Table with connections explicitly set @since 3.7 @jira_ticket PYTHON-613 @expected_result Tables should be created and dropped @test_category object_mapper """ for ks in self.keyspaces: create_keyspace_simple(ks, 1, connections=self.conns) # No connection (default is fake) with self.assertRaises(NoHostAvailable): sync_table(TestModel) # Explicit connections sync_table(TestModel, connections=self.conns) # Explicit drop drop_table(TestModel, connections=self.conns) # Model connection TestModel.__connection__ = 'cluster' sync_table(TestModel) TestModel.__connection__ = None # No connection (default is fake) with self.assertRaises(NoHostAvailable): drop_table(TestModel) # Model connection TestModel.__connection__ = 'cluster' drop_table(TestModel) TestModel.__connection__ = None # Model connection for ks in self.keyspaces: drop_keyspace(ks, connections=self.conns)
def test_can_create_same_udt_different_keyspaces(self): sync_type(DEFAULT_KEYSPACE, User) create_keyspace_simple("simplex", 1) sync_type("simplex", User) drop_keyspace("simplex")
def setUpClass(cls): super(ContextQueryTests, cls).setUpClass() for ks in cls.KEYSPACES: create_keyspace_simple(ks, 1) sync_table(TestModel, keyspaces=cls.KEYSPACES)
def main(): connection.default() # Management functions would normally be used in development, and possibly for deployments. # They are typically not part of a core application. log.info("### creating keyspace...") management.create_keyspace_simple(KEYSPACE, 1) log.info("### syncing model...") management.sync_table(FamilyMembers) # default uuid is assigned simmons = FamilyMembers.create(surname='Simmons', name='Gene', birth_year=1949, sex='m') # add members to his family later FamilyMembers.create(id=simmons.id, surname='Simmons', name='Nick', birth_year=1989, sex='m') sophie = FamilyMembers.create(id=simmons.id, surname='Simmons', name='Sophie', sex='f') nick = FamilyMembers.objects(id=simmons.id, surname='Simmons', name='Nick') try: nick.iff(birth_year=1988).update(birth_year=1989) except LWTException: print "precondition not met" log.info("### setting individual column to NULL by updating it to None") nick.update(birth_year=None) # showing validation try: FamilyMembers.create(id=simmons.id, surname='Tweed', name='Shannon', birth_year=1957, sex='f') except ValidationError: log.exception( 'INTENTIONAL VALIDATION EXCEPTION; Failed creating instance:') FamilyMembers.create(id=simmons.id, surname='Tweed', name='Shannon', sex='f') log.info("### add multiple as part of a batch") # If creating many at one time, can use a batch to minimize round-trips hogan_id = uuid4() with BatchQuery() as b: FamilyMembers.batch(b).create(id=hogan_id, surname='Hogan', name='Hulk', sex='m') FamilyMembers.batch(b).create(id=hogan_id, surname='Hogan', name='Linda', sex='f') FamilyMembers.batch(b).create(id=hogan_id, surname='Hogan', name='Nick', sex='m') FamilyMembers.batch(b).create(id=hogan_id, surname='Hogan', name='Brooke', sex='f') log.info("### All members") for m in FamilyMembers.all(): print m, m.birth_year, m.sex log.info("### Select by partition key") for m in FamilyMembers.objects(id=simmons.id): print m, m.birth_year, m.sex log.info("### Constrain on clustering key") for m in FamilyMembers.objects(id=simmons.id, surname=simmons.surname): print m, m.birth_year, m.sex log.info("### Constrain on clustering key") kids = FamilyMembers.objects(id=simmons.id, surname=simmons.surname, name__in=['Nick', 'Sophie']) log.info("### Delete a record") FamilyMembers(id=hogan_id, surname='Hogan', name='Linda').delete() for m in FamilyMembers.objects(id=hogan_id): print m, m.birth_year, m.sex management.drop_keyspace(KEYSPACE)