Expose object context thru a public property
This is useful for consuming projects to cary on the context in base class without duplicating it in their own base classes. Change-Id: I28f49740b82a3063709c3c239b5199e901ad898e
This commit is contained in:
parent
c459d5e9a9
commit
bb1fe93ece
|
@ -664,6 +664,10 @@ class VersionedObject(object):
|
|||
def obj_fields(self):
|
||||
return list(self.fields.keys()) + self.obj_extra_fields
|
||||
|
||||
@property
|
||||
def obj_context(self):
|
||||
return self._context
|
||||
|
||||
|
||||
class ComparableVersionedObject(object):
|
||||
"""Mix-in to provide comparison methods
|
||||
|
|
|
@ -1083,6 +1083,21 @@ class _TestObject(object):
|
|||
obj = TestObj()
|
||||
self.assertEqual(['foo', 'bar'], obj.obj_fields)
|
||||
|
||||
def test_obj_context(self):
|
||||
class TestObj(base.VersionedObject):
|
||||
pass
|
||||
|
||||
# context is available through the public property
|
||||
context = mock.Mock()
|
||||
obj = TestObj(context)
|
||||
self.assertEqual(context, obj.obj_context)
|
||||
|
||||
# ..but it's not available for update
|
||||
new_context = mock.Mock()
|
||||
self.assertRaises(
|
||||
AttributeError,
|
||||
setattr, obj, 'obj_context', new_context)
|
||||
|
||||
def test_obj_constructor(self):
|
||||
obj = MyObj(context=self.context, foo=123, bar='abc')
|
||||
self.assertEqual(123, obj.foo)
|
||||
|
|
Loading…
Reference in New Issue