本文整理汇总了Python中oslo_versionedobjects.fields.ListOfObjectsField方法的典型用法代码示例。如果您正苦于以下问题:Python fields.ListOfObjectsField方法的具体用法?Python fields.ListOfObjectsField怎么用?Python fields.ListOfObjectsField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类oslo_versionedobjects.fields
的用法示例。
在下文中一共展示了fields.ListOfObjectsField方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_obj_make_list
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import ListOfObjectsField [as 别名]
def test_obj_make_list(self):
@base.VersionedObjectRegistry.register
class MyList(base.ObjectListBase, base.VersionedObject):
fields = {
'objects': fields.ListOfObjectsField('MyObj'),
}
db_objs = [{'foo': 1, 'bar': 'baz', 'missing': 'banana'},
{'foo': 2, 'bar': 'bat', 'missing': 'apple'},
]
mylist = base.obj_make_list('ctxt', MyList(), MyObj, db_objs)
self.assertEqual(2, len(mylist))
self.assertEqual('ctxt', mylist._context)
for index, item in enumerate(mylist):
self.assertEqual(db_objs[index]['foo'], item.foo)
self.assertEqual(db_objs[index]['bar'], item.bar)
self.assertEqual(db_objs[index]['missing'], item.missing)
示例2: test_serialization
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import ListOfObjectsField [as 别名]
def test_serialization(self):
@base.VersionedObjectRegistry.register
class Foo(base.ObjectListBase, base.VersionedObject):
fields = {'objects': fields.ListOfObjectsField('Bar')}
@base.VersionedObjectRegistry.register
class Bar(base.VersionedObject):
fields = {'foo': fields.Field(fields.String())}
obj = Foo(objects=[])
for i in 'abc':
bar = Bar(foo=i)
obj.objects.append(bar)
obj2 = base.VersionedObject.obj_from_primitive(obj.obj_to_primitive())
self.assertFalse(obj is obj2)
self.assertEqual([x.foo for x in obj],
[y.foo for y in obj2])
示例3: test_obj_make_compatible_child_versions
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import ListOfObjectsField [as 别名]
def test_obj_make_compatible_child_versions(self):
@base.VersionedObjectRegistry.register
class MyElement(base.VersionedObject):
fields = {'foo': fields.IntegerField()}
@base.VersionedObjectRegistry.register
class Foo(base.ObjectListBase, base.VersionedObject):
VERSION = '1.1'
fields = {'objects': fields.ListOfObjectsField('MyElement')}
child_versions = {'1.0': '1.0', '1.1': '1.0'}
subobj = MyElement(foo=1)
obj = Foo(objects=[subobj])
primitive = obj.obj_to_primitive()['versioned_object.data']
with mock.patch.object(subobj, 'obj_make_compatible') as mock_compat:
obj.obj_make_compatible(copy.copy(primitive), '1.1')
self.assertTrue(mock_compat.called)
示例4: test_obj_make_compatible_obj_relationships
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import ListOfObjectsField [as 别名]
def test_obj_make_compatible_obj_relationships(self):
@base.VersionedObjectRegistry.register
class MyElement(base.VersionedObject):
fields = {'foo': fields.IntegerField()}
@base.VersionedObjectRegistry.register
class Bar(base.ObjectListBase, base.VersionedObject):
VERSION = '1.1'
fields = {'objects': fields.ListOfObjectsField('MyElement')}
obj_relationships = {
'objects': [('1.0', '1.0'), ('1.1', '1.0')]
}
subobj = MyElement(foo=1)
obj = Bar(objects=[subobj])
primitive = obj.obj_to_primitive()['versioned_object.data']
with mock.patch.object(subobj, 'obj_make_compatible') as mock_compat:
obj.obj_make_compatible(copy.copy(primitive), '1.1')
self.assertTrue(mock_compat.called)
示例5: test_obj_make_compatible_no_relationships
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import ListOfObjectsField [as 别名]
def test_obj_make_compatible_no_relationships(self):
@base.VersionedObjectRegistry.register
class MyElement(base.VersionedObject):
fields = {'foo': fields.IntegerField()}
@base.VersionedObjectRegistry.register
class Baz(base.ObjectListBase, base.VersionedObject):
VERSION = '1.1'
fields = {'objects': fields.ListOfObjectsField('MyElement')}
subobj = MyElement(foo=1)
obj = Baz(objects=[subobj])
primitive = obj.obj_to_primitive()['versioned_object.data']
with mock.patch.object(subobj, 'obj_make_compatible') as mock_compat:
obj.obj_make_compatible(copy.copy(primitive), '1.1')
self.assertTrue(mock_compat.called)
示例6: test_list_changes
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import ListOfObjectsField [as 别名]
def test_list_changes(self):
@base.VersionedObjectRegistry.register
class Foo(base.ObjectListBase, base.VersionedObject):
fields = {'objects': fields.ListOfObjectsField('Bar')}
@base.VersionedObjectRegistry.register
class Bar(base.VersionedObject):
fields = {'foo': fields.StringField()}
obj = Foo(objects=[])
self.assertEqual(set(['objects']), obj.obj_what_changed())
obj.objects.append(Bar(foo='test'))
self.assertEqual(set(['objects']), obj.obj_what_changed())
obj.obj_reset_changes()
# This should still look dirty because the child is dirty
self.assertEqual(set(['objects']), obj.obj_what_changed())
obj.objects[0].obj_reset_changes()
# This should now look clean because the child is clean
self.assertEqual(set(), obj.obj_what_changed())
示例7: test_complex
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import ListOfObjectsField [as 别名]
def test_complex(self):
@base.VersionedObjectRegistry.register
class TestChild(base.VersionedObject):
VERSION = '2.34'
@base.VersionedObjectRegistry.register
class TestChildTwo(base.VersionedObject):
VERSION = '4.56'
fields = {
'sibling': fields.ObjectField('TestChild'),
}
@base.VersionedObjectRegistry.register
class TestObject(base.VersionedObject):
VERSION = '1.23'
fields = {
'child': fields.ObjectField('TestChild'),
'childtwo': fields.ListOfObjectsField('TestChildTwo'),
}
tree = base.obj_tree_get_versions('TestObject')
self.assertEqual({'TestObject': '1.23',
'TestChild': '2.34',
'TestChildTwo': '4.56'},
tree)
示例8: test_list_object_concat
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import ListOfObjectsField [as 别名]
def test_list_object_concat(self):
@base.VersionedObjectRegistry.register_if(False)
class MyList(base.ObjectListBase, base.VersionedObject):
fields = {'objects': fields.ListOfObjectsField('MyOwnedObject')}
values = [1, 2, 42]
list1 = MyList(objects=[MyOwnedObject(baz=values[0]),
MyOwnedObject(baz=values[1])])
list2 = MyList(objects=[MyOwnedObject(baz=values[2])])
concat_list = list1 + list2
for idx, obj in enumerate(concat_list):
self.assertEqual(values[idx], obj.baz)
# Assert that the original lists are unmodified
self.assertEqual(2, len(list1.objects))
self.assertEqual(1, list1.objects[0].baz)
self.assertEqual(2, list1.objects[1].baz)
self.assertEqual(1, len(list2.objects))
self.assertEqual(42, list2.objects[0].baz)
示例9: test_list_object_concat_fails_different_objects
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import ListOfObjectsField [as 别名]
def test_list_object_concat_fails_different_objects(self):
@base.VersionedObjectRegistry.register_if(False)
class MyList(base.ObjectListBase, base.VersionedObject):
fields = {'objects': fields.ListOfObjectsField('MyOwnedObject')}
@base.VersionedObjectRegistry.register_if(False)
class MyList2(base.ObjectListBase, base.VersionedObject):
fields = {'objects': fields.ListOfObjectsField('MyOwnedObject')}
list1 = MyList(objects=[MyOwnedObject(baz=1)])
list2 = MyList2(objects=[MyOwnedObject(baz=2)])
def add(x, y):
return x + y
self.assertRaises(TypeError, add, list1, list2)
# Assert that the original lists are unmodified
self.assertEqual(1, len(list1.objects))
self.assertEqual(1, len(list2.objects))
self.assertEqual(1, list1.objects[0].baz)
self.assertEqual(2, list2.objects[0].baz)
示例10: obj_make_compatible
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import ListOfObjectsField [as 别名]
def obj_make_compatible(self, primitive, target_version):
"""Make an object representation compatible with a target version.
This is responsible for taking the primitive representation of
an object and making it suitable for the given target_version.
This may mean converting the format of object attributes, removing
attributes that have been added since the target version, etc. In
general:
- If a new version of an object adds a field, this routine
should remove it for older versions.
- If a new version changed or restricted the format of a field, this
should convert it back to something a client knowing only of the
older version will tolerate.
- If an object that this object depends on is bumped, then this
object should also take a version bump. Then, this routine should
backlevel the dependent object (by calling its obj_make_compatible())
if the requested version of this object is older than the version
where the new dependent object was added.
:param primitive: The result of :meth:`obj_to_primitive`
:param target_version: The version string requested by the recipient
of the object
:raises: :exc:`oslo_versionedobjects.exception.UnsupportedObjectError`
if conversion is not possible for some reason
"""
for key, field in self.fields.items():
if not isinstance(field, (obj_fields.ObjectField,
obj_fields.ListOfObjectsField)):
continue
if not self.obj_attr_is_set(key):
continue
self._obj_make_obj_compatible(primitive, target_version, key)
示例11: obj_tree_get_versions
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import ListOfObjectsField [as 别名]
def obj_tree_get_versions(objname, tree=None):
"""Construct a mapping of dependent object versions.
This method builds a list of dependent object versions given a top-
level object with other objects as fields. It walks the tree recursively
to determine all the objects (by symbolic name) that could be contained
within the top-level object, and the maximum versions of each. The result
is a dict like::
{'MyObject': '1.23', ... }
:param objname: The top-level object at which to start
:param tree: Used internally, pass None here.
:returns: A dictionary of object names and versions
"""
if tree is None:
tree = {}
if objname in tree:
return tree
objclass = VersionedObjectRegistry.obj_classes()[objname][0]
tree[objname] = objclass.VERSION
for field_name in objclass.fields:
field = objclass.fields[field_name]
if isinstance(field, obj_fields.ObjectField):
child_cls = field._type._obj_name
elif isinstance(field, obj_fields.ListOfObjectsField):
child_cls = field._type._element_type._type._obj_name
else:
continue
try:
obj_tree_get_versions(child_cls, tree=tree)
except IndexError:
raise exception.UnregisteredSubobject(
child_objname=child_cls, parent_objname=objname)
return tree
示例12: test_obj_make_compatible_on_list_base
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import ListOfObjectsField [as 别名]
def test_obj_make_compatible_on_list_base(self):
@base.VersionedObjectRegistry.register_if(False)
class MyList(base.ObjectListBase, base.VersionedObject):
VERSION = '1.1'
fields = {'objects': fields.ListOfObjectsField('MyObj')}
childobj = MyObj(foo=1)
listobj = MyList(objects=[childobj])
compat_func = 'obj_make_compatible_from_manifest'
with mock.patch.object(childobj, compat_func) as mock_compat:
listobj.obj_to_primitive(target_version='1.0')
mock_compat.assert_called_once_with({'foo': 1}, '1.0',
version_manifest=None)
示例13: test_obj_repr
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import ListOfObjectsField [as 别名]
def test_obj_repr(self):
@base.VersionedObjectRegistry.register
class Foo(base.ObjectListBase, base.VersionedObject):
fields = {'objects': fields.ListOfObjectsField('Bar')}
@base.VersionedObjectRegistry.register
class Bar(base.VersionedObject):
fields = {'uuid': fields.StringField()}
obj = Foo(objects=[Bar(uuid='fake-uuid')])
self.assertEqual('Foo(objects=[Bar(fake-uuid)])', repr(obj))
示例14: test_complex_loopy
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import ListOfObjectsField [as 别名]
def test_complex_loopy(self):
@base.VersionedObjectRegistry.register
class TestChild(base.VersionedObject):
VERSION = '2.34'
fields = {
'sibling': fields.ObjectField('TestChildTwo'),
}
@base.VersionedObjectRegistry.register
class TestChildTwo(base.VersionedObject):
VERSION = '4.56'
fields = {
'sibling': fields.ObjectField('TestChild'),
'parents': fields.ListOfObjectsField('TestObject'),
}
@base.VersionedObjectRegistry.register
class TestObject(base.VersionedObject):
VERSION = '1.23'
fields = {
'child': fields.ObjectField('TestChild'),
'childtwo': fields.ListOfObjectsField('TestChildTwo'),
}
tree = base.obj_tree_get_versions('TestObject')
self.assertEqual({'TestObject': '1.23',
'TestChild': '2.34',
'TestChildTwo': '4.56'},
tree)
示例15: test_builtin_list_add_fails
# 需要导入模块: from oslo_versionedobjects import fields [as 别名]
# 或者: from oslo_versionedobjects.fields import ListOfObjectsField [as 别名]
def test_builtin_list_add_fails(self):
@base.VersionedObjectRegistry.register_if(False)
class MyList(base.ObjectListBase, base.VersionedObject):
fields = {'objects': fields.ListOfObjectsField('MyOwnedObject')}
list1 = MyList(objects=[MyOwnedObject(baz=1)])
def add(obj):
return obj + []
self.assertRaises(TypeError, add, list1)