def test_get_objects_pass_marker_obj_when_limit_and_marker_passed(self): ctxt = context.get_admin_context() model = mock.sentinel.model marker = mock.sentinel.marker limit = mock.sentinel.limit pager = base.Pager(marker=marker, limit=limit) plugin = manager.NeutronManager.get_plugin() with mock.patch.object(plugin, "_get_collection") as get_collection: with mock.patch.object(api, "get_object") as get_object: api.get_objects(ctxt, model, _pager=pager) get_object.assert_called_with(ctxt, model, id=marker) get_collection.assert_called_with( ctxt, model, mock.ANY, filters={}, limit=limit, marker_obj=get_object.return_value )
def get_objects(cls, context, **kwargs): cls.validate_filters(**kwargs) db_objs = obj_db_api.get_objects(context, cls.db_model, **kwargs) objs = [cls(context, **db_obj) for db_obj in db_objs] for obj in objs: obj.obj_reset_changes() return 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 get_objects(cls, context, _pager=None, validate_filters=True, fields=None, **kwargs): """Fetch a list of objects Fetch all results from DB and convert them to versioned objects. :param context: :param _pager: a Pager object representing advanced sorting/pagination criteria :param validate_filters: Raises an error in case of passing an unknown filter :param fields: indicate which fields the caller is interested in using. Note that currently this is limited to avoid loading synthetic fields when possible, and does not affect db queries. Default is None, which is the same as []. Example: ['id', 'name'] :param kwargs: multiple keys defined by key=value pairs :return: list of objects of NeutronDbObject class or empty list """ if validate_filters: cls.validate_filters(**kwargs) with cls.db_context_reader(context): db_objs = obj_db_api.get_objects( cls, context, _pager=_pager, **cls.modify_fields_to_db(kwargs)) return [cls._load_object(context, db_obj, fields=fields) for db_obj in db_objs]
def test_get_objects_pass_marker_obj_when_limit_and_marker_passed(self): ctxt = context.get_admin_context() marker = mock.sentinel.marker limit = mock.sentinel.limit pager = base.Pager(marker=marker, limit=limit) with mock.patch.object( model_query, 'get_collection') as get_collection: with mock.patch.object(api, 'get_object') as get_object: api.get_objects(FakeObj, ctxt, _pager=pager) get_object.assert_called_with(FakeObj, ctxt, id=marker) get_collection.assert_called_with( ctxt, FakeObj.db_model, dict_func=None, filters={}, limit=limit, marker_obj=get_object.return_value)
def get_objects(cls, context, _pager=None, validate_filters=True, fields=None, **kwargs): """Fetch a list of objects Fetch all results from DB and convert them to versioned objects. :param context: :param _pager: a Pager object representing advanced sorting/pagination criteria :param validate_filters: Raises an error in case of passing an unknown filter :param fields: indicate which fields the caller is interested in using. Note that currently this is limited to avoid loading synthetic fields when possible, and does not affect db queries. Default is None, which is the same as []. Example: ['id', 'name'] :param kwargs: multiple keys defined by key=value pairs :return: list of objects of NeutronDbObject class or empty list """ if validate_filters: cls.validate_filters(**kwargs) with cls.db_context_reader(context): db_objs = obj_db_api.get_objects(cls, context, _pager=_pager, **cls.modify_fields_to_db(kwargs)) return [ cls._load_object(context, db_obj, fields=fields) for db_obj in db_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_objects_with_None_value_in_filters(self): obj = api.create_object(self.obj_cls, self.ctxt, {'name': 'foo'}) new_objs = api.get_objects(self.obj_cls, self.ctxt, name='foo', status=None) self.assertEqual(obj, new_objs[0])
def get_objects(cls, context, _pager=None, validate_filters=True, **kwargs): """Fetch a list of objects Fetch all results from DB and convert them to versioned objects. :param context: :param _pager: a Pager object representing advanced sorting/pagination criteria :param validate_filters: Raises an error in case of passing an unknown filter :param kwargs: multiple keys defined by key=value pairs :return: list of objects of NeutronDbObject class or empty list """ if validate_filters: cls.validate_filters(**kwargs) with cls.db_context_reader(context): db_objs = obj_db_api.get_objects(cls, context, _pager=_pager, **cls.modify_fields_to_db(kwargs)) return [cls._load_object(context, db_obj) for db_obj in db_objs]
def get_objects(cls, context, **kwargs): cls.validate_filters(**kwargs) db_objs = obj_db_api.get_objects(context, cls.db_model, **kwargs) result = [] for db_obj in db_objs: obj = cls(context, **cls.modify_fields_from_db(db_obj)) obj.obj_reset_changes() result.append(obj) return result
def test_get_objects_pass_marker_obj_when_limit_and_marker_passed(self): ctxt = context.get_admin_context() model = mock.sentinel.model marker = mock.sentinel.marker limit = mock.sentinel.limit pager = base.Pager(marker=marker, limit=limit) plugin = directory.get_plugin() with mock.patch.object(plugin, '_get_collection') as get_collection: with mock.patch.object(api, 'get_object') as get_object: api.get_objects(ctxt, model, _pager=pager) get_object.assert_called_with(ctxt, model, id=marker) get_collection.assert_called_with(ctxt, model, mock.ANY, filters={}, limit=limit, marker_obj=get_object.return_value)
def test_get_objects_with_string_matching_filters_starts(self): obj1 = api.create_object(self.obj_cls, self.ctxt, {'name': 'pre_obj1'}) obj2 = api.create_object(self.obj_cls, self.ctxt, {'name': 'pre_obj2'}) obj3 = api.create_object(self.obj_cls, self.ctxt, {'name': 'obj_3'}) objs = api.get_objects( self.obj_cls, self.ctxt, name=obj_utils.StringStarts('pre')) self.assertEqual(2, len(objs)) self.assertIn(obj1, objs) self.assertIn(obj2, objs) self.assertNotIn(obj3, objs)
def test_get_objects_with_string_matching_filters_ends(self): obj1 = api.create_object(self.ctxt, self.model, {'name': 'obj1_end'}) obj2 = api.create_object(self.ctxt, self.model, {'name': 'obj2_end'}) obj3 = api.create_object(self.ctxt, self.model, {'name': 'obj_3'}) objs = api.get_objects( self.ctxt, self.model, name=obj_utils.StringEnds('end')) self.assertEqual(2, len(objs)) self.assertIn(obj1, objs) self.assertIn(obj2, objs) self.assertNotIn(obj3, objs)
def test_get_objects_with_string_matching_filters_ends(self): obj1 = api.create_object(self.obj_cls, self.ctxt, {'name': 'obj1_end'}) obj2 = api.create_object(self.obj_cls, self.ctxt, {'name': 'obj2_end'}) obj3 = api.create_object(self.obj_cls, self.ctxt, {'name': 'obj_3'}) objs = api.get_objects( self.obj_cls, self.ctxt, name=obj_utils.StringEnds('end')) self.assertEqual(2, len(objs)) self.assertIn(obj1, objs) self.assertIn(obj2, objs) self.assertNotIn(obj3, objs)
def test_get_objects_with_string_matching_filters_ends(self): obj1 = api.create_object(self.obj_cls, self.ctxt, {'name': 'obj1_end'}) obj2 = api.create_object(self.obj_cls, self.ctxt, {'name': 'obj2_end'}) api.create_object(self.obj_cls, self.ctxt, {'name': 'obj_3'}) objs = api.get_objects( self.obj_cls, self.ctxt, name=obj_utils.StringEnds('end'), _pager=base.Pager(sorts=[('name', True)])) self.assertEqual(2, len(objs)) self._compare_objs(obj1, objs[0]) self._compare_objs(obj2, objs[1])
def test_get_objects_with_string_matching_filters_starts(self): obj1 = api.create_object(self.ctxt, self.model, {'name': 'pre_obj1'}) obj2 = api.create_object(self.ctxt, self.model, {'name': 'pre_obj2'}) obj3 = api.create_object(self.ctxt, self.model, {'name': 'obj_3'}) objs = api.get_objects(self.ctxt, self.model, name=obj_utils.StringStarts('pre')) self.assertEqual(2, len(objs)) self.assertIn(obj1, objs) self.assertIn(obj2, objs) self.assertNotIn(obj3, objs)
def get_objects(cls, context, _pager=None, **kwargs): """ Fetch objects from DB and convert them to versioned objects. :param context: :param _pager: a Pager object representing advanced sorting/pagination criteria :param kwargs: multiple keys defined by key=value pairs :return: list of objects of NeutronDbObject class """ cls.validate_filters(**kwargs) with db_api.autonested_transaction(context.session): db_objs = obj_db_api.get_objects( context, cls.db_model, _pager=_pager, **kwargs) return [cls._load_object(context, db_obj) for db_obj in db_objs]
def get_objects(cls, context, _pager=None, **kwargs): """ Fetch objects from DB and convert them to versioned objects. :param context: :param _pager: a Pager object representing advanced sorting/pagination criteria :param kwargs: multiple keys defined by key=value pairs :return: list of objects of NeutronDbObject class """ cls.validate_filters(**kwargs) with db_api.autonested_transaction(context.session): db_objs = obj_db_api.get_objects(context, cls.db_model, _pager=_pager, **kwargs) return [cls._load_object(context, db_obj) for db_obj in db_objs]
def get_objects(cls, context, _pager=None, validate_filters=True, **kwargs): """ Fetch all results from DB and convert them to versioned objects. :param context: :param _pager: a Pager object representing advanced sorting/pagination criteria :param validate_filters: Raises an error in case of passing an unknown filter :param kwargs: multiple keys defined by key=value pairs :return: list of objects of NeutronDbObject class or empty list """ if validate_filters: cls.validate_filters(**kwargs) with cls.db_context_reader(context): db_objs = obj_db_api.get_objects( cls, context, _pager=_pager, **cls.modify_fields_to_db(kwargs)) return [cls._load_object(context, db_obj) for db_obj in db_objs]
def get_objects(cls, context, **kwargs): cls.validate_filters(**kwargs) with db_api.autonested_transaction(context.session): db_objs = obj_db_api.get_objects(context, cls.db_model, **kwargs) return [cls._load_object(context, db_obj) for db_obj in db_objs]
def test_get_objects_with_None_value_in_filters(self): obj = api.create_object(self.ctxt, self.model, {'name': 'foo'}) new_objs = api.get_objects( self.ctxt, self.model, name='foo', status=None) self.assertEqual(obj, new_objs[0])