Merge "Expose object context thru a public property"

This commit is contained in:
Jenkins 2016-04-18 10:18:55 +00:00 committed by Gerrit Code Review
commit c82c899c2f
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)