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:
Ihar Hrachyshka 2016-03-10 16:51:08 +01:00
parent c459d5e9a9
commit bb1fe93ece
2 changed files with 19 additions and 0 deletions

View File

@ -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

View File

@ -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)