PageRenderTime 92ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/pyramid/tests/test_events.py

https://gitlab.com/goolic/pyramid
Python | 301 lines | 246 code | 55 blank | 0 comment | 3 complexity | ae00619ee14e4c0d59df5ab045fca353 MD5 | raw file
  1. import unittest
  2. from pyramid import testing
  3. class NewRequestEventTests(unittest.TestCase):
  4. def _getTargetClass(self):
  5. from pyramid.events import NewRequest
  6. return NewRequest
  7. def _makeOne(self, request):
  8. return self._getTargetClass()(request)
  9. def test_class_conforms_to_INewRequest(self):
  10. from pyramid.interfaces import INewRequest
  11. from zope.interface.verify import verifyClass
  12. klass = self._getTargetClass()
  13. verifyClass(INewRequest, klass)
  14. def test_instance_conforms_to_INewRequest(self):
  15. from pyramid.interfaces import INewRequest
  16. from zope.interface.verify import verifyObject
  17. request = DummyRequest()
  18. inst = self._makeOne(request)
  19. verifyObject(INewRequest, inst)
  20. def test_ctor(self):
  21. request = DummyRequest()
  22. inst = self._makeOne(request)
  23. self.assertEqual(inst.request, request)
  24. class NewResponseEventTests(unittest.TestCase):
  25. def _getTargetClass(self):
  26. from pyramid.events import NewResponse
  27. return NewResponse
  28. def _makeOne(self, request, response):
  29. return self._getTargetClass()(request, response)
  30. def test_class_conforms_to_INewResponse(self):
  31. from pyramid.interfaces import INewResponse
  32. from zope.interface.verify import verifyClass
  33. klass = self._getTargetClass()
  34. verifyClass(INewResponse, klass)
  35. def test_instance_conforms_to_INewResponse(self):
  36. from pyramid.interfaces import INewResponse
  37. from zope.interface.verify import verifyObject
  38. request = DummyRequest()
  39. response = DummyResponse()
  40. inst = self._makeOne(request, response)
  41. verifyObject(INewResponse, inst)
  42. def test_ctor(self):
  43. request = DummyRequest()
  44. response = DummyResponse()
  45. inst = self._makeOne(request, response)
  46. self.assertEqual(inst.request, request)
  47. self.assertEqual(inst.response, response)
  48. class ApplicationCreatedEventTests(unittest.TestCase):
  49. def _getTargetClass(self):
  50. from pyramid.events import ApplicationCreated
  51. return ApplicationCreated
  52. def _makeOne(self, context=object()):
  53. return self._getTargetClass()(context)
  54. def test_class_conforms_to_IApplicationCreated(self):
  55. from pyramid.interfaces import IApplicationCreated
  56. from zope.interface.verify import verifyClass
  57. verifyClass(IApplicationCreated, self._getTargetClass())
  58. def test_object_conforms_to_IApplicationCreated(self):
  59. from pyramid.interfaces import IApplicationCreated
  60. from zope.interface.verify import verifyObject
  61. verifyObject(IApplicationCreated, self._makeOne())
  62. class WSGIApplicationCreatedEventTests(ApplicationCreatedEventTests):
  63. def _getTargetClass(self):
  64. from pyramid.events import WSGIApplicationCreatedEvent
  65. return WSGIApplicationCreatedEvent
  66. def test_class_conforms_to_IWSGIApplicationCreatedEvent(self):
  67. from pyramid.interfaces import IWSGIApplicationCreatedEvent
  68. from zope.interface.verify import verifyClass
  69. verifyClass(IWSGIApplicationCreatedEvent, self._getTargetClass())
  70. def test_object_conforms_to_IWSGIApplicationCreatedEvent(self):
  71. from pyramid.interfaces import IWSGIApplicationCreatedEvent
  72. from zope.interface.verify import verifyObject
  73. verifyObject(IWSGIApplicationCreatedEvent, self._makeOne())
  74. class ContextFoundEventTests(unittest.TestCase):
  75. def _getTargetClass(self):
  76. from pyramid.events import ContextFound
  77. return ContextFound
  78. def _makeOne(self, request=None):
  79. if request is None:
  80. request = DummyRequest()
  81. return self._getTargetClass()(request)
  82. def test_class_conforms_to_IContextFound(self):
  83. from zope.interface.verify import verifyClass
  84. from pyramid.interfaces import IContextFound
  85. verifyClass(IContextFound, self._getTargetClass())
  86. def test_instance_conforms_to_IContextFound(self):
  87. from zope.interface.verify import verifyObject
  88. from pyramid.interfaces import IContextFound
  89. verifyObject(IContextFound, self._makeOne())
  90. class AfterTraversalEventTests(ContextFoundEventTests):
  91. def _getTargetClass(self):
  92. from pyramid.events import AfterTraversal
  93. return AfterTraversal
  94. def test_class_conforms_to_IAfterTraversal(self):
  95. from zope.interface.verify import verifyClass
  96. from pyramid.interfaces import IAfterTraversal
  97. verifyClass(IAfterTraversal, self._getTargetClass())
  98. def test_instance_conforms_to_IAfterTraversal(self):
  99. from zope.interface.verify import verifyObject
  100. from pyramid.interfaces import IAfterTraversal
  101. verifyObject(IAfterTraversal, self._makeOne())
  102. class TestSubscriber(unittest.TestCase):
  103. def setUp(self):
  104. self.config = testing.setUp()
  105. def tearDown(self):
  106. testing.tearDown()
  107. def _makeOne(self, *ifaces, **predicates):
  108. from pyramid.events import subscriber
  109. return subscriber(*ifaces, **predicates)
  110. def test_register_single(self):
  111. from zope.interface import Interface
  112. class IFoo(Interface): pass
  113. class IBar(Interface): pass
  114. dec = self._makeOne(IFoo)
  115. def foo(): pass
  116. config = DummyConfigurator()
  117. scanner = Dummy()
  118. scanner.config = config
  119. dec.register(scanner, None, foo)
  120. self.assertEqual(config.subscribed, [(foo, IFoo)])
  121. def test_register_multi(self):
  122. from zope.interface import Interface
  123. class IFoo(Interface): pass
  124. class IBar(Interface): pass
  125. dec = self._makeOne(IFoo, IBar)
  126. def foo(): pass
  127. config = DummyConfigurator()
  128. scanner = Dummy()
  129. scanner.config = config
  130. dec.register(scanner, None, foo)
  131. self.assertEqual(config.subscribed, [(foo, IFoo), (foo, IBar)])
  132. def test_register_none_means_all(self):
  133. from zope.interface import Interface
  134. dec = self._makeOne()
  135. def foo(): pass
  136. config = DummyConfigurator()
  137. scanner = Dummy()
  138. scanner.config = config
  139. dec.register(scanner, None, foo)
  140. self.assertEqual(config.subscribed, [(foo, Interface)])
  141. def test_register_objectevent(self):
  142. from zope.interface import Interface
  143. class IFoo(Interface): pass
  144. class IBar(Interface): pass
  145. dec = self._makeOne([IFoo, IBar])
  146. def foo(): pass
  147. config = DummyConfigurator()
  148. scanner = Dummy()
  149. scanner.config = config
  150. dec.register(scanner, None, foo)
  151. self.assertEqual(config.subscribed, [(foo, [IFoo, IBar])])
  152. def test___call__(self):
  153. dec = self._makeOne()
  154. dummy_venusian = DummyVenusian()
  155. dec.venusian = dummy_venusian
  156. def foo(): pass
  157. dec(foo)
  158. self.assertEqual(dummy_venusian.attached,
  159. [(foo, dec.register, 'pyramid')])
  160. def test_regsister_with_predicates(self):
  161. from zope.interface import Interface
  162. dec = self._makeOne(a=1)
  163. def foo(): pass
  164. config = DummyConfigurator()
  165. scanner = Dummy()
  166. scanner.config = config
  167. dec.register(scanner, None, foo)
  168. self.assertEqual(config.subscribed, [(foo, Interface, {'a':1})])
  169. class TestBeforeRender(unittest.TestCase):
  170. def _makeOne(self, system, val=None):
  171. from pyramid.events import BeforeRender
  172. return BeforeRender(system, val)
  173. def test_instance_conforms(self):
  174. from zope.interface.verify import verifyObject
  175. from pyramid.interfaces import IBeforeRender
  176. event = self._makeOne({})
  177. verifyObject(IBeforeRender, event)
  178. def test_setitem_success(self):
  179. event = self._makeOne({})
  180. event['a'] = 1
  181. self.assertEqual(event, {'a':1})
  182. def test_setdefault_fail(self):
  183. event = self._makeOne({})
  184. result = event.setdefault('a', 1)
  185. self.assertEqual(result, 1)
  186. self.assertEqual(event, {'a':1})
  187. def test_setdefault_success(self):
  188. event = self._makeOne({})
  189. event['a'] = 1
  190. result = event.setdefault('a', 2)
  191. self.assertEqual(result, 1)
  192. self.assertEqual(event, {'a':1})
  193. def test_update_success(self):
  194. event = self._makeOne({'a':1})
  195. event.update({'b':2})
  196. self.assertEqual(event, {'a':1, 'b':2})
  197. def test__contains__True(self):
  198. system = {'a':1}
  199. event = self._makeOne(system)
  200. self.assertTrue('a' in event)
  201. def test__contains__False(self):
  202. system = {}
  203. event = self._makeOne(system)
  204. self.assertFalse('a' in event)
  205. def test__getitem__success(self):
  206. system = {'a':1}
  207. event = self._makeOne(system)
  208. self.assertEqual(event['a'], 1)
  209. def test__getitem__fail(self):
  210. system = {}
  211. event = self._makeOne(system)
  212. self.assertRaises(KeyError, event.__getitem__, 'a')
  213. def test_get_success(self):
  214. system = {'a':1}
  215. event = self._makeOne(system)
  216. self.assertEqual(event.get('a'), 1)
  217. def test_get_fail(self):
  218. system = {}
  219. event = self._makeOne(system)
  220. self.assertEqual(event.get('a'), None)
  221. def test_rendering_val(self):
  222. system = {}
  223. val = {}
  224. event = self._makeOne(system, val)
  225. self.assertTrue(event.rendering_val is val)
  226. class DummyConfigurator(object):
  227. def __init__(self):
  228. self.subscribed = []
  229. def add_subscriber(self, wrapped, ifaces, **predicates):
  230. if not predicates:
  231. self.subscribed.append((wrapped, ifaces))
  232. else:
  233. self.subscribed.append((wrapped, ifaces, predicates))
  234. class DummyRegistry(object):
  235. pass
  236. class DummyVenusian(object):
  237. def __init__(self):
  238. self.attached = []
  239. def attach(self, wrapped, fn, category=None):
  240. self.attached.append((wrapped, fn, category))
  241. class Dummy:
  242. pass
  243. class DummyRequest:
  244. pass
  245. class DummyResponse:
  246. pass