Exemplo n.º 1
0
 def test_get_session_fails_without_existing_connection(self):
     """
     Users can't get the default session without having a default connection set.
     """
     with self.assertRaisesRegexp(connection.CQLEngineException,
                                  self.no_registered_connection_msg):
         connection.get_session(connection=None)
Exemplo n.º 2
0
    def delete(self, username=None):
        """Delete a collection and the associated row in the tree entry table"""
        from indigo.models import Notification
        if self.is_root:
            return
        cfg = get_config(None)
        session = connection.get_session()
        keyspace = cfg.get('KEYSPACE', 'indigo')
        session.set_keyspace(keyspace)
        query = SimpleStatement(
            """DELETE FROM tree_entry WHERE container=%s""")
        session.execute(query, (self.path, ))
        # Get the row that describe the collection as a child of its parent
        child = TreeEntry.objects.filter(container=self.container,
                                         name=u"{}/".format(
                                             self.name)).first()
        if child:
            child.delete()

        session = get_graph_session()
        session.execute_graph("""v_coll = {}.drop();
                                 """.format(gq_get_vertex_collection(self)))

        state = self.mqtt_get_state()
        payload = self.mqtt_payload(state, {})
        Notification.delete_collection(username, self.path, payload)
        self.reset()
Exemplo n.º 3
0
 def test_default_consistency(self):
     # verify that this session default is set according to connection.setup
     # assumes tests/cqlengine/__init__ setup uses CL.ONE
     session = connection.get_session()
     self.assertEqual(
         session.cluster.profile_manager.default.consistency_level,
         ConsistencyLevel.ONE)
    def test_static_columns(self):
        class StaticModel(Model):
            id = columns.Integer(primary_key=True)
            c = columns.Integer(primary_key=True)
            name = columns.Text(static=True)

        drop_table(StaticModel)

        session = get_session()

        with mock.patch.object(session, "execute", wraps=session.execute) as m:
            sync_table(StaticModel)

        self.assertGreater(m.call_count, 0)
        statement = m.call_args[0][0].query_string
        self.assertIn('"name" text static', statement)

        # if we sync again, we should not apply an alter w/ a static
        sync_table(StaticModel)

        with mock.patch.object(session, "execute",
                               wraps=session.execute) as m2:
            sync_table(StaticModel)

        self.assertEqual(len(m2.call_args_list), 0)
Exemplo n.º 5
0
 def delete_id(cls, uuid):
     """Delete all blobs for the specified uuid"""
     cfg = get_config(None)
     session = connection.get_session()
     keyspace = cfg.get('KEYSPACE', 'indigo')
     session.set_keyspace(keyspace)
     query = SimpleStatement("""DELETE FROM data_object WHERE uuid=%s""")
     session.execute(query, (uuid,))
Exemplo n.º 6
0
    def test_ttl_included_on_create(self):
        """ tests that ttls on models work as expected """
        session = get_session()

        with mock.patch.object(session, 'execute') as m:
            TestTTLModel.ttl(60).create(text="hello blake")

        query = m.call_args[0][0].query_string
        self.assertIn("USING TTL", query)
Exemplo n.º 7
0
    def test_update_includes_ttl(self):
        session = get_session()

        model = TestTTLModel.create(text="goodbye blake")
        with mock.patch.object(session, 'execute') as m:
            model.ttl(60).update(text="goodbye forever")

        query = m.call_args[0][0].query_string
        self.assertIn("USING TTL", query)
Exemplo n.º 8
0
 def get_default_ttl(self, table_name):
     session = get_session()
     try:
         default_ttl = session.execute("SELECT default_time_to_live FROM system_schema.tables "
                                       "WHERE keyspace_name = 'cqlengine_test' AND table_name = '{0}'".format(table_name))
     except InvalidRequest:
         default_ttl = session.execute("SELECT default_time_to_live FROM system.schema_columnfamilies "
                                       "WHERE keyspace_name = 'cqlengine_test' AND columnfamily_name = '{0}'".format(table_name))
     return default_ttl[0]['default_time_to_live']
Exemplo n.º 9
0
 def create_acl(self, acl_cql):
     """Replace the static acl with the given cql string"""
     cfg = get_config(None)
     session = connection.get_session()
     keyspace = cfg.get('KEYSPACE', 'indigo')
     session.set_keyspace(keyspace)
     query = SimpleStatement(u"""UPDATE data_object SET acl = {}
         WHERE uuid=%s""".format(acl_cql))
     session.execute(query, (self.uuid,))
Exemplo n.º 10
0
 def update_container_acl(self, acl_cql):
     """Update the static acl with the given cql string"""
     cfg = get_config(None)
     session = connection.get_session()
     keyspace = cfg.get('KEYSPACE', 'indigo')
     session.set_keyspace(keyspace)
     query = SimpleStatement(
         u"""UPDATE tree_entry SET container_acl=container_acl+{} 
         WHERE container=%s""".format(acl_cql))
     session.execute(query, (self.container, ))
    def test_delete_on_subclass_does_not_include_disc_value(self):
        p1 = Inherit1.create()
        session = get_session()
        with mock.patch.object(session, 'execute') as m:
            Inherit1.objects(partition=p1.partition).delete()

        # make sure our discriminator value isn't in the CQL
        # not sure how we would even get here if it was in there
        # since the CQL would fail.

        self.assertNotIn("row_type", m.call_args[0][0].query_string)
Exemplo n.º 12
0
    def test_ttl_included_with_blind_update(self):
        session = get_session()

        o = TestTTLModel.create(text="whatever")
        tid = o.id

        with mock.patch.object(session, 'execute') as m:
            TestTTLModel.objects(id=tid).ttl(60).update(text="bacon")

        query = m.call_args[0][0].query_string
        self.assertIn("USING TTL", query)
Exemplo n.º 13
0
 def delete_id(cls, uuid):
     """
     Delete all blobs for the specified uuid
     
     :param uuid: A CDMI uuid
     :type uuid: str
     """
     session = connection.get_session()
     keyspace = radon.cfg.dse_keyspace
     session.set_keyspace(keyspace)
     query = SimpleStatement("""DELETE FROM data_object WHERE uuid=%s""")
     session.execute(query, (uuid, ))
Exemplo n.º 14
0
    def test_ttl_is_include_with_query_on_update(self):
        session = get_session()

        o = TestTTLModel.create(text="whatever")
        o.text = "new stuff"
        o = o.ttl(60)

        with mock.patch.object(session, 'execute') as m:
            o.save()

        query = m.call_args[0][0].query_string
        self.assertIn("USING TTL", query)
Exemplo n.º 15
0
 def create_entry_acl(self, acl_cql):
     """Replace the acl with the given cql string
     """
     cfg = get_config(None)
     session = connection.get_session()
     keyspace = cfg.get('KEYSPACE', 'indigo')
     session.set_keyspace(keyspace)
     query = SimpleStatement(u"""UPDATE tree_entry SET acl={} 
         WHERE container=%s and name=%s""".format(acl_cql))
     session.execute(query, (
         self.container,
         self.name,
     ))
Exemplo n.º 16
0
    def test_override_default_ttl(self):
        session = get_session()
        o = TestDefaultTTLModel.create(text="some text on ttl")
        tid = o.id

        o.ttl(3600)
        self.assertEqual(o._ttl, 3600)

        with mock.patch.object(session, 'execute') as m:
            TestDefaultTTLModel.objects(id=tid).ttl(None).update(text="aligators expired")

        query = m.call_args[0][0].query_string
        self.assertNotIn("USING TTL", query)
Exemplo n.º 17
0
 def create_acl(self, acl_cql):
     """
     Replace the acl with the given cql string
     
     :param acl_cql: The acl string to put in Cassandra, can be easily
       generated in :meth:`radon.model.acl.acl_list_to_cql`
     :type acl_cql: str
     """
     session = connection.get_session()
     keyspace = radon.cfg.dse_keyspace
     session.set_keyspace(keyspace)
     query = SimpleStatement(u"""UPDATE tree_node SET acl={} 
         WHERE container=%s and name=%s and version=%s""".format(acl_cql))
     session.execute(query, (self.container, self.name, self.version))
Exemplo n.º 18
0
    def test_default_ttl_modify(self):
        session = get_session()

        default_ttl = self.get_default_ttl('test_default_ttlmodel')
        self.assertEqual(default_ttl, 20)

        TestDefaultTTLModel.__options__ = {'default_time_to_live': 10}
        sync_table(TestDefaultTTLModel)

        default_ttl = self.get_default_ttl('test_default_ttlmodel')
        self.assertEqual(default_ttl, 10)

        # Restore default TTL
        TestDefaultTTLModel.__options__ = {'default_time_to_live': 20}
        sync_table(TestDefaultTTLModel)
Exemplo n.º 19
0
    def test_default_ttl_not_set(self):
        session = get_session()

        o = TestTTLModel.create(text="some text")
        tid = o.id

        self.assertIsNone(o._ttl)

        default_ttl = self.get_default_ttl('test_ttlmodel')
        self.assertEqual(default_ttl, 0)

        with mock.patch.object(session, 'execute') as m:
            TestTTLModel.objects(id=tid).update(text="aligators")

        query = m.call_args[0][0].query_string
        self.assertNotIn("USING TTL", query)
Exemplo n.º 20
0
 def update(self, **kwargs):
     """Update a collection"""
     cfg = get_config(None)
     session = connection.get_session()
     keyspace = cfg.get('KEYSPACE', 'indigo')
     session.set_keyspace(keyspace)
     for arg in kwargs:
         # For static fields we can't use the name in the where condition
         if arg in static_fields:
             query = SimpleStatement(u"""UPDATE tree_entry SET {}=%s
                 WHERE container=%s""".format(arg))
             session.execute(query, (kwargs[arg], self.container))
         else:
             query = SimpleStatement(u"""UPDATE tree_entry SET {}=%s
                 WHERE container=%s and name=%s""".format(arg))
             session.execute(query,
                             (kwargs[arg], self.container, self.name))
     return self
Exemplo n.º 21
0
    def test_default_ttl_set(self):
        session = get_session()

        o = TestDefaultTTLModel.create(text="some text on ttl")
        tid = o.id

        # Should not be set, it's handled by Cassandra
        self.assertIsNone(o._ttl)

        default_ttl = self.get_default_ttl('test_default_ttlmodel')
        self.assertEqual(default_ttl, 20)

        with mock.patch.object(session, 'execute') as m:
            TestTTLModel.objects(id=tid).update(text="aligators expired")

        # Should not be set either
        query = m.call_args[0][0].query_string
        self.assertNotIn("USING TTL", query)
Exemplo n.º 22
0
 def update(self, **kwargs):
     """Update a data object"""
     cfg = get_config(None)
     session = connection.get_session()
     keyspace = cfg.get('KEYSPACE', 'indigo')
     session.set_keyspace(keyspace)
     for arg in kwargs:
         # For static fields we can't use the name in the where condition
         if arg in static_fields:
             query = SimpleStatement("""UPDATE data_object SET {}=%s
                 WHERE uuid=%s""".format(arg))
             session.execute(query, (kwargs[arg], self.uuid))
         else:
             print """UPDATE data_object SET {}=%s
                 WHERE uuid=%s and sequence_number=%s""".format(arg)
             query = SimpleStatement("""UPDATE data_object SET {}=%s
                 WHERE uuid=%s and sequence_number=%s""".format(arg))
             session.execute(query, (kwargs[arg], self.uuid, self.sequence_number))
     return self
Exemplo n.º 23
0
    def delete(self, username=None):
        """
        Delete a collection and the associated row in the tree_node table
        
        :param username: The name of the user who deleted the collection
        :type username: str, optional
        """
        from radon.model import Resource
        if not username:
            username = radon.cfg.sys_lib_user

        if self.is_root:
            return

        # We don't need the suffixes for the resources, otherwise we won't
        # find them
        child_container, child_dataobject = self.get_child(False)
        for child_str in child_container:
            child = Collection.find(self.path + child_str)
            if child:
                child.delete()
        for child_str in child_dataobject:
            child = Resource.find(self.path + child_str)
            if child:
                child.delete()

        session = connection.get_session()
        keyspace = radon.cfg.dse_keyspace
        session.set_keyspace(keyspace)
        query = SimpleStatement(
            """DELETE FROM tree_node WHERE container=%s and name=%s""")
        session.execute(query, (
            self.container,
            self.name,
        ))

        from radon.model import Notification
        state = self.mqtt_get_state()
        payload = self.mqtt_payload(state, {})
        Notification.delete_collection(username, self.path, payload)
Exemplo n.º 24
0
 def setUp(self):
     self.session = get_session()