Merge "Recognize extra fields in CinderObjectDictCompat"
This commit is contained in:
commit
892de6d0a9
@ -240,6 +240,13 @@ class CinderObjectDictCompat(base.VersionedObjectDictCompat):
|
||||
else:
|
||||
return getattr(self, key)
|
||||
|
||||
def __contains__(self, name):
|
||||
try:
|
||||
# Overriding this to make extra fields pass "'foo' in obj" tests
|
||||
return name in self.obj_extra_fields or self.obj_attr_is_set(name)
|
||||
except AttributeError:
|
||||
return False
|
||||
|
||||
|
||||
class CinderPersistentObject(object):
|
||||
"""Mixin class for Persistent objects.
|
||||
|
@ -538,3 +538,31 @@ class TestCinderObjectConditionalUpdate(test.TestCase):
|
||||
|
||||
# Check that the volume in the DB has also been updated
|
||||
self._check_volume(volume, 'deleting', expected_size, True)
|
||||
|
||||
|
||||
class TestCinderDictObject(test_objects.BaseObjectsTestCase):
|
||||
@objects.base.CinderObjectRegistry.register_if(False)
|
||||
class TestDictObject(objects.base.CinderObjectDictCompat,
|
||||
objects.base.CinderObject):
|
||||
obj_extra_fields = ['foo']
|
||||
|
||||
fields = {
|
||||
'abc': fields.StringField(nullable=True),
|
||||
'def': fields.IntegerField(nullable=True),
|
||||
}
|
||||
|
||||
@property
|
||||
def foo(self):
|
||||
return 42
|
||||
|
||||
def test_dict_objects(self):
|
||||
obj = self.TestDictObject()
|
||||
self.assertIsNone(obj.get('non_existing'))
|
||||
self.assertEqual('val', obj.get('abc', 'val'))
|
||||
obj.abc = 'val2'
|
||||
self.assertEqual('val2', obj.get('abc', 'val'))
|
||||
self.assertEqual(42, obj.get('foo'))
|
||||
|
||||
self.assertTrue('foo' in obj)
|
||||
self.assertTrue('abc' in obj)
|
||||
self.assertFalse('def' in obj)
|
||||
|
Loading…
x
Reference in New Issue
Block a user