def test_delete_objects_removes_all_matching_objects(self): # create some objects with identical description for i in range(10): api.create_object( self.ctxt, self.model, {'name': 'foo%d' % i, 'description': 'bar'}) # create some more objects with a different description descriptions = set() for i in range(10, 20): desc = 'bar%d' % i descriptions.add(desc) api.create_object( self.ctxt, self.model, {'name': 'foo%d' % i, 'description': desc}) # make sure that all objects are in the database self.assertEqual(20, api.count(self.ctxt, self.model)) # now delete just those with the 'bar' description api.delete_objects(self.ctxt, self.model, description='bar') # check that half of objects are gone, and remaining have expected # descriptions objs = api.get_objects(self.ctxt, self.model) self.assertEqual(10, len(objs)) self.assertEqual( descriptions, {obj.description for obj in objs})
def test_delete_objects_removes_all_matching_objects(self): # create some objects with identical description for i in range(10): api.create_object(self.ctxt, self.model, { 'name': 'foo%d' % i, 'description': 'bar' }) # create some more objects with a different description descriptions = set() for i in range(10, 20): desc = 'bar%d' % i descriptions.add(desc) api.create_object(self.ctxt, self.model, { 'name': 'foo%d' % i, 'description': desc }) # make sure that all objects are in the database self.assertEqual(20, api.count(self.ctxt, self.model)) # now delete just those with the 'bar' description api.delete_objects(self.ctxt, self.model, description='bar') # check that half of objects are gone, and remaining have expected # descriptions objs = api.get_objects(self.ctxt, self.model) self.assertEqual(10, len(objs)) self.assertEqual(descriptions, {obj.description for obj in objs})
def test_get_object_create_update_delete(self): obj = api.create_object(self.ctxt, self.model, {'name': 'foo'}) new_obj = api.get_object(self.ctxt, self.model, id=obj.id) self.assertEqual(obj, new_obj) obj = new_obj api.update_object(self.ctxt, self.model, {'name': 'bar'}, id=obj.id) new_obj = api.get_object(self.ctxt, self.model, id=obj.id) self.assertEqual(obj, new_obj) obj = new_obj api.delete_object(self.ctxt, self.model, id=obj.id) new_obj = api.get_object(self.ctxt, self.model, id=obj.id) self.assertIsNone(new_obj) # delete_object raises an exception on missing object self.assertRaises( n_exc.ObjectNotFound, api.delete_object, self.ctxt, self.model, id=obj.id) # but delete_objects does not not api.delete_objects(self.ctxt, self.model, id=obj.id)
def test_get_object_create_update_delete(self): obj = api.create_object(self.ctxt, self.model, {'name': 'foo'}) new_obj = api.get_object(self.ctxt, self.model, id=obj.id) self.assertEqual(obj, new_obj) obj = new_obj api.update_object(self.ctxt, self.model, {'name': 'bar'}, id=obj.id) new_obj = api.get_object(self.ctxt, self.model, id=obj.id) self.assertEqual(obj, new_obj) obj = new_obj api.delete_object(self.ctxt, self.model, id=obj.id) new_obj = api.get_object(self.ctxt, self.model, id=obj.id) self.assertIsNone(new_obj) # delete_object raises an exception on missing object self.assertRaises(n_exc.ObjectNotFound, api.delete_object, self.ctxt, self.model, id=obj.id) # but delete_objects does not not api.delete_objects(self.ctxt, self.model, id=obj.id)
def _attach_security_groups(self, sg_ids): # TODO(ihrachys): consider introducing an (internal) object for the # binding to decouple database operations a bit more obj_db_api.delete_objects( SecurityGroupPortBinding, self.obj_context, port_id=self.id) if sg_ids: for sg_id in sg_ids: self._attach_security_group(sg_id) self.security_group_ids = sg_ids self.obj_reset_changes(['security_group_ids'])
def _set_dns_domain(self, dns_domain): obj_db_api.delete_objects( self.obj_context, dns_db.NetworkDNSDomain, network_id=self.id, ) if dns_domain: obj_db_api.create_object( self.obj_context, dns_db.NetworkDNSDomain, {'network_id': self.id, 'dns_domain': dns_domain} ) self.dns_domain = dns_domain self.obj_reset_changes(['dns_domain'])
def _attach_qos_policy(self, qos_policy_id): # TODO(ihrachys): introduce an object for the binding to isolate # database access in a single place, currently scattered between port # and policy objects obj_db_api.delete_objects( self.obj_context, qos_models.QosPortPolicyBinding, port_id=self.id) if qos_policy_id: obj_db_api.create_object( self.obj_context, qos_models.QosPortPolicyBinding, {'port_id': self.id, 'policy_id': qos_policy_id} ) self.qos_policy_id = qos_policy_id self.obj_reset_changes(['qos_policy_id'])
def delete_objects(cls, context, validate_filters=True, **kwargs): """Delete objects that match filtering criteria from DB. :param context: :param validate_filters: Raises an error in case of passing an unknown filter :param kwargs: multiple keys defined by key=value pairs :return: Number of entries deleted """ if validate_filters: cls.validate_filters(**kwargs) with cls.db_context_writer(context): return obj_db_api.delete_objects(cls, context, **cls.modify_fields_to_db(kwargs))
def delete_objects(cls, context, validate_filters=True, **kwargs): """Delete objects that match filtering criteria from DB. :param context: :param validate_filters: Raises an error in case of passing an unknown filter :param kwargs: multiple keys defined by key=value pairs :return: Number of entries deleted """ if validate_filters: cls.validate_filters(**kwargs) with cls.db_context_writer(context): return obj_db_api.delete_objects( cls, context, **cls.modify_fields_to_db(kwargs))