Merge "Add objectify decorator for readability"

This commit is contained in:
Jenkins 2016-06-16 00:04:03 +00:00 committed by Gerrit Code Review
commit 0bb46f0a2c
2 changed files with 46 additions and 2 deletions

View File

@ -143,13 +143,16 @@ class VersionedObjectRegistry(object):
def register_if(cls, condition): def register_if(cls, condition):
def wraps(obj_cls): def wraps(obj_cls):
if condition: if condition:
registry = cls() obj_cls = cls.register(obj_cls)
registry._register_class(obj_cls)
else: else:
_make_class_properties(obj_cls) _make_class_properties(obj_cls)
return obj_cls return obj_cls
return wraps return wraps
@classmethod
def objectify(cls, obj_cls):
return cls.register_if(False)(obj_cls)
@classmethod @classmethod
def obj_classes(cls): def obj_classes(cls):
registry = cls() registry = cls()

View File

@ -324,6 +324,47 @@ class TestRegistry(test.TestCase):
self.assertEqual(AVersionedObject1.reg_to, "one") self.assertEqual(AVersionedObject1.reg_to, "one")
self.assertEqual(AVersionedObject2.reg_to, "two") self.assertEqual(AVersionedObject2.reg_to, "two")
@mock.patch.object(base.VersionedObjectRegistry, '__new__')
def test_register(self, mock_registry):
mock_reg_obj = mock.Mock()
mock_registry.return_value = mock_reg_obj
mock_reg_obj._register_class = mock.Mock()
class my_class(object):
pass
base.VersionedObjectRegistry.register(my_class)
mock_reg_obj._register_class.assert_called_once_with(my_class)
@mock.patch.object(base.VersionedObjectRegistry, 'register')
def test_register_if(self, mock_register):
class my_class(object):
pass
base.VersionedObjectRegistry.register_if(True)(my_class)
mock_register.assert_called_once_with(my_class)
@mock.patch.object(base, '_make_class_properties')
def test_register_if_false(self, mock_make_props):
class my_class(object):
pass
base.VersionedObjectRegistry.register_if(False)(my_class)
mock_make_props.assert_called_once_with(my_class)
@mock.patch.object(base.VersionedObjectRegistry, 'register_if')
def test_objectify(self, mock_register_if):
mock_reg_callable = mock.Mock()
mock_register_if.return_value = mock_reg_callable
class my_class(object):
pass
base.VersionedObjectRegistry.objectify(my_class)
mock_register_if.assert_called_once_with(False)
mock_reg_callable.assert_called_once_with(my_class)
class TestObjMakeList(test.TestCase): class TestObjMakeList(test.TestCase):