当前位置: 首页>>代码示例>>Python>>正文


Python Components.unregisterUtility方法代码示例

本文整理汇总了Python中zope.interface.registry.Components.unregisterUtility方法的典型用法代码示例。如果您正苦于以下问题:Python Components.unregisterUtility方法的具体用法?Python Components.unregisterUtility怎么用?Python Components.unregisterUtility使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在zope.interface.registry.Components的用法示例。


在下文中一共展示了Components.unregisterUtility方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestUtility

# 需要导入模块: from zope.interface.registry import Components [as 别名]
# 或者: from zope.interface.registry.Components import unregisterUtility [as 别名]
class TestUtility(unittest.TestCase):

    def setUp(self):
        self.components = Components('comps')

    def test_register_utility(self):
        test_object = U1(1)
        self.components.registerUtility(test_object)
        self.assertEqual(self.components.getUtility(I1), test_object)

    def test_register_utility_with_factory(self):
        test_object = U1(1)
        def factory():
           return test_object
        self.components.registerUtility(factory=factory)
        self.assertEqual(self.components.getUtility(I1), test_object)
        self.assertTrue(self.components.unregisterUtility(factory=factory))

    def test_register_utility_with_component_and_factory(self):
        def factory():
            return U1(1)
        self.assertRaises(
            TypeError,
            self.components.registerUtility, U1(1), factory=factory)

    def test_unregister_utility_with_and_without_component_and_factory(self):
        def factory():
            return U1(1)
        self.assertRaises(
            TypeError,
            self.components.unregisterUtility, U1(1), factory=factory)
        self.assertRaises(TypeError, self.components.unregisterUtility)

    def test_register_utility_with_no_interfaces(self):
        self.assertRaises(TypeError, self.components.registerUtility, A)

    def test_register_utility_with_two_interfaces(self):
        self.assertRaises(TypeError, self.components.registerUtility, U12(1))

    def test_register_utility_with_arguments(self):
        test_object1 = U12(1)
        test_object2 = U12(2)
        self.components.registerUtility(test_object1, I2)
        self.components.registerUtility(test_object2, I2, 'name')
        self.assertEqual(self.components.getUtility(I2), test_object1)
        self.assertEqual(self.components.getUtility(I2, 'name'), test_object2)

    def test_get_none_existing_utility(self):
        from zope.interface.interfaces import ComponentLookupError
        self.assertRaises(ComponentLookupError, self.components.getUtility, I3)

    def test_query_none_existing_utility(self):
        self.assertTrue(self.components.queryUtility(I3) is None)
        self.assertEqual(self.components.queryUtility(I3, default=42), 42)

    def test_registered_utilities_and_sorting(self):
        test_object1 = U1(1)
        test_object2 = U12(2)
        test_object3 = U12(3)
        self.components.registerUtility(test_object1)
        self.components.registerUtility(test_object3, I2, u'name')
        self.components.registerUtility(test_object2, I2)

        sorted_utilities = sorted(self.components.registeredUtilities())
        sorted_utilities_name = map(lambda x: getattr(x, 'name'),
                                    sorted_utilities)
        sorted_utilities_component = map(lambda x: getattr(x, 'component'),
                                         sorted_utilities)
        sorted_utilities_provided = map(lambda x: getattr(x, 'provided'),
                                        sorted_utilities)

        self.assertEqual(len(sorted_utilities), 3)
        self.assertEqual(sorted_utilities_name, [u'', u'', u'name'])
        self.assertEqual(
            sorted_utilities_component,
            [test_object1, test_object2, test_object3])
        self.assertEqual(sorted_utilities_provided, [I1, I2, I2])

    def test_duplicate_utility(self):
        test_object1 = U1(1)
        test_object2 = U12(2)
        test_object3 = U12(3)
        test_object4 = U1(4)
        self.components.registerUtility(test_object1)
        self.components.registerUtility(test_object2, I2)
        self.components.registerUtility(test_object3, I2, u'name')
        self.assertEqual(self.components.getUtility(I1), test_object1)

        self.components.registerUtility(test_object4, info=u'use 4 now')
        self.assertEqual(self.components.getUtility(I1), test_object4)

    def test_unregister_utility(self):
        test_object = U1(1)
        self.components.registerUtility(test_object)
        self.assertEqual(self.components.getUtility(I1), test_object)
        self.assertTrue(self.components.unregisterUtility(provided=I1))
        self.assertFalse(self.components.unregisterUtility(provided=I1))

    def test_unregister_utility_extended(self):
        test_object = U1(1)
#.........这里部分代码省略.........
开发者ID:ChimmyTee,项目名称:oh-mainline,代码行数:103,代码来源:test_registry.py


注:本文中的zope.interface.registry.Components.unregisterUtility方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。