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 count(cls, context, **kwargs): """ Count the number of objects matching filtering criteria. :param context: :param kwargs: multiple keys defined by key=value pairs :return: number of matching objects """ cls.validate_filters(**kwargs) return obj_db_api.count(context, cls.db_model, **cls.modify_fields_to_db(kwargs))
def count(cls, context, **kwargs): """ Count the number of objects matching filtering criteria. :param context: :param kwargs: multiple keys defined by key=value pairs :return: number of matching objects """ cls.validate_filters(**kwargs) return obj_db_api.count( context, cls.db_model, **cls.modify_fields_to_db(kwargs) )
def count(cls, context, validate_filters=True, **kwargs): """Count the number of objects matching filtering criteria. :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 matching objects """ if validate_filters: cls.validate_filters(**kwargs) return obj_db_api.count(cls, context, **cls.modify_fields_to_db(kwargs))
def objects_exist(cls, context, validate_filters=True, **kwargs): """Check if objects are present in 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: boolean. True if object is present. """ if validate_filters: cls.validate_filters(**kwargs) # Succeed if at least a single object matches; no need to fetch more return bool( obj_db_api.count(cls, context, **cls.modify_fields_to_db(kwargs)))
def count(cls, context, validate_filters=True, **kwargs): """Count the number of objects matching filtering criteria. :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 matching objects """ if validate_filters: cls.validate_filters(**kwargs) return obj_db_api.count( cls, context, **cls.modify_fields_to_db(kwargs) )
def objects_exist(cls, context, validate_filters=True, **kwargs): """Check if objects are present in 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: boolean. True if object is present. """ if validate_filters: cls.validate_filters(**kwargs) # Succeed if at least a single object matches; no need to fetch more return bool(obj_db_api.count( cls, context, **cls.modify_fields_to_db(kwargs)) )