コード例 #1
0
ファイル: test_frontend.py プロジェクト: jtux270/translate
 def test_init(self):
     """
     Test the `ipalib.frontend.Attribute.__init__` method.
     """
     class user_add(self.cls):
         pass
     o = user_add()
     assert read_only(o, 'obj') is None
     assert read_only(o, 'obj_name') == 'user'
     assert read_only(o, 'attr_name') == 'add'
コード例 #2
0
    def test_init(self):
        """
        Test the `ipalib.frontend.Attribute.__init__` method.
        """
        class user_add(self.cls):
            pass

        o = user_add()
        assert read_only(o, 'obj') is None
        assert read_only(o, 'obj_name') == 'user'
        assert read_only(o, 'attr_name') == 'add'
コード例 #3
0
ファイル: test_frontend.py プロジェクト: jtux270/translate
 def test_set_api(self):
     """
     Test the `ipalib.frontend.Attribute.set_api` method.
     """
     user_obj = 'The user frontend.Object instance'
     class api(object):
         Object = dict(user=user_obj)
     class user_add(self.cls):
         pass
     o = user_add()
     assert read_only(o, 'api') is None
     assert read_only(o, 'obj') is None
     o.set_api(api)
     assert read_only(o, 'api') is api
     assert read_only(o, 'obj') is user_obj
コード例 #4
0
    def test_set_api(self):
        """
        Test the `ipalib.frontend.Attribute.set_api` method.
        """
        user_obj = 'The user frontend.Object instance'

        class api(object):
            Object = dict(user=user_obj)

        class user_add(self.cls):
            pass

        o = user_add()
        assert read_only(o, 'api') is None
        assert read_only(o, 'obj') is None
        o.set_api(api)
        assert read_only(o, 'api') is api
        assert read_only(o, 'obj') is user_obj
コード例 #5
0
    def test_MagicDict(self):
        """
        Test container emulation of `ipalib.plugable.MagicDict` class.
        """
        cnt = 10
        keys = []
        d = dict()
        dictproxy = self.cls(d)
        for i in xrange(cnt):
            key = 'key_%d' % i
            val = 'val_%d' % i
            keys.append(key)

            # Test thet key does not yet exist
            assert len(dictproxy) == i
            assert key not in dictproxy
            assert not hasattr(dictproxy, key)
            raises(KeyError, getitem, dictproxy, key)
            raises(AttributeError, getattr, dictproxy, key)

            # Test that items/attributes cannot be set on dictproxy:
            raises(TypeError, setitem, dictproxy, key, val)
            raises(AttributeError, setattr, dictproxy, key, val)

            # Test that additions in d are reflected in dictproxy:
            d[key] = val
            assert len(dictproxy) == i + 1
            assert key in dictproxy
            assert hasattr(dictproxy, key)
            assert dictproxy[key] is val
            assert read_only(dictproxy, key) is val

        # Test __iter__
        assert list(dictproxy) == keys

        for key in keys:
            # Test that items cannot be deleted through dictproxy:
            raises(TypeError, delitem, dictproxy, key)
            raises(AttributeError, delattr, dictproxy, key)

            # Test that deletions in d are reflected in dictproxy
            del d[key]
            assert len(dictproxy) == len(d)
            assert key not in dictproxy
            raises(KeyError, getitem, dictproxy, key)
            raises(AttributeError, getattr, dictproxy, key)
コード例 #6
0
    def test_set_api(self):
        """
        Test the `ipalib.frontend.Object.set_api` method.
        """

        # Setup for test:
        class DummyAttribute(object):
            def __init__(self, obj_name, attr_name, name=None):
                self.obj_name = obj_name
                self.attr_name = attr_name
                if name is None:
                    self.name = '%s_%s' % (obj_name, attr_name)
                else:
                    self.name = name
                self.param = frontend.create_param(attr_name)

            def __clone__(self, attr_name):
                return self.__class__(self.obj_name, self.attr_name,
                                      getattr(self, attr_name))

        def get_attributes(cnt, format):
            for name in ['other', 'user', 'another']:
                for i in xrange(cnt):
                    yield DummyAttribute(name, format % i)

        cnt = 10
        formats = dict(
            methods='method_%d',
            properties='property_%d',
        )

        _d = dict(
            Method=plugable.NameSpace(get_attributes(cnt, formats['methods'])),
            Property=plugable.NameSpace(
                get_attributes(cnt, formats['properties'])),
        )
        api = plugable.MagicDict(_d)
        assert len(api.Method) == cnt * 3
        assert len(api.Property) == cnt * 3

        class user(self.cls):
            pass

        # Actually perform test:
        o = user()
        o.set_api(api)
        assert read_only(o, 'api') is api
        for name in ['methods', 'properties']:
            namespace = getattr(o, name)
            assert isinstance(namespace, plugable.NameSpace)
            assert len(namespace) == cnt
            f = formats[name]
            for i in xrange(cnt):
                attr_name = f % i
                attr = namespace[attr_name]
                assert isinstance(attr, DummyAttribute)
                assert attr is getattr(namespace, attr_name)
                assert attr.obj_name == 'user'
                assert attr.attr_name == attr_name
                assert attr.name == '%s_%s' % ('user', attr_name)

        # Test params instance attribute
        o = self.cls()
        o.set_api(api)
        ns = o.params
        assert type(ns) is plugable.NameSpace
        assert len(ns) == 0

        class example(self.cls):
            takes_params = ('banana', 'apple')

        o = example()
        o.set_api(api)
        ns = o.params
        assert type(ns) is plugable.NameSpace
        assert len(ns) == 2, repr(ns)
        assert list(ns) == ['banana', 'apple']
        for p in ns():
            assert type(p) is parameters.Str
            assert p.required is True
            assert p.multivalue is False
コード例 #7
0
    def test_API(self):
        """
        Test the `ipalib.plugable.API` class.
        """
        assert issubclass(plugable.API, plugable.ReadOnly)

        # Setup the test bases, create the API:
        class base0(plugable.Plugin):
            def method(self, n):
                return n

        class base1(plugable.Plugin):
            def method(self, n):
                return n + 1

        api = plugable.API(base0, base1)
        api.env.mode = 'unit_test'
        api.env.in_tree = True
        r = api.register
        assert isinstance(r, plugable.Registrar)
        assert read_only(api, 'register') is r

        class base0_plugin0(base0):
            pass
        r(base0_plugin0)

        class base0_plugin1(base0):
            pass
        r(base0_plugin1)

        class base0_plugin2(base0):
            pass
        r(base0_plugin2)

        class base1_plugin0(base1):
            pass
        r(base1_plugin0)

        class base1_plugin1(base1):
            pass
        r(base1_plugin1)

        class base1_plugin2(base1):
            pass
        r(base1_plugin2)

        # Test API instance:
        assert api.isdone('bootstrap') is False
        assert api.isdone('finalize') is False
        api.finalize()
        assert api.isdone('bootstrap') is True
        assert api.isdone('finalize') is True

        def get_base_name(b):
            return 'base%d' % b


        def get_plugin_name(b, p):
            return 'base%d_plugin%d' % (b, p)

        for b in xrange(2):
            base_name = get_base_name(b)
            base = locals()[base_name]
            ns = getattr(api, base_name)
            assert isinstance(ns, plugable.NameSpace)
            assert read_only(api, base_name) is ns
            assert len(ns) == 3
            for p in xrange(3):
                plugin_name = get_plugin_name(b, p)
                plugin = locals()[plugin_name]
                inst = ns[plugin_name]
                assert isinstance(inst, base)
                assert isinstance(inst, plugin)
                assert inst.name == plugin_name
                assert read_only(ns, plugin_name) is inst
                assert inst.method(7) == 7 + b

        # Test that calling finilize again raises AssertionError:
        e = raises(StandardError, api.finalize)
        assert str(e) == 'API.finalize() already called', str(e)
コード例 #8
0
ファイル: test_frontend.py プロジェクト: jtux270/translate
    def test_set_api(self):
        """
        Test the `ipalib.frontend.Object.set_api` method.
        """
        # Setup for test:
        class DummyAttribute(object):
            def __init__(self, obj_name, attr_name, name=None):
                self.obj_name = obj_name
                self.attr_name = attr_name
                if name is None:
                    self.name = '%s_%s' % (obj_name, attr_name)
                else:
                    self.name = name
                self.param = frontend.create_param(attr_name)

            def __clone__(self, attr_name):
                return self.__class__(
                    self.obj_name,
                    self.attr_name,
                    getattr(self, attr_name)
                )

        def get_attributes(cnt, format):
            for name in ['other', 'user', 'another']:
                for i in xrange(cnt):
                    yield DummyAttribute(name, format % i)

        cnt = 10
        formats = dict(
            methods='method_%d',
            properties='property_%d',
        )


        _d = dict(
            Method=plugable.NameSpace(
                get_attributes(cnt, formats['methods'])
            ),
            Property=plugable.NameSpace(
                get_attributes(cnt, formats['properties'])
            ),
        )
        api = plugable.MagicDict(_d)
        assert len(api.Method) == cnt * 3
        assert len(api.Property) == cnt * 3

        class user(self.cls):
            pass

        # Actually perform test:
        o = user()
        o.set_api(api)
        assert read_only(o, 'api') is api
        for name in ['methods', 'properties']:
            namespace = getattr(o, name)
            assert isinstance(namespace, plugable.NameSpace)
            assert len(namespace) == cnt
            f = formats[name]
            for i in xrange(cnt):
                attr_name = f % i
                attr = namespace[attr_name]
                assert isinstance(attr, DummyAttribute)
                assert attr is getattr(namespace, attr_name)
                assert attr.obj_name == 'user'
                assert attr.attr_name == attr_name
                assert attr.name == '%s_%s' % ('user', attr_name)

        # Test params instance attribute
        o = self.cls()
        o.set_api(api)
        ns = o.params
        assert type(ns) is plugable.NameSpace
        assert len(ns) == 0
        class example(self.cls):
            takes_params = ('banana', 'apple')
        o = example()
        o.set_api(api)
        ns = o.params
        assert type(ns) is plugable.NameSpace
        assert len(ns) == 2, repr(ns)
        assert list(ns) == ['banana', 'apple']
        for p in ns():
            assert type(p) is parameters.Str
            assert p.required is True
            assert p.multivalue is False