本文整理汇总了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)
#.........这里部分代码省略.........