def test_decorator_context(self): """Verify that the notify decorator can extract the 'context' arg.""" self.notify_called = False self.context_arg = None def example_api(arg1, arg2, context): return arg1 + arg2 def example_api2(arg1, arg2, **kw): return arg1 + arg2 example_api = notifier_api.notify_decorator( 'example_api', example_api) example_api2 = notifier_api.notify_decorator( 'example_api2', example_api2) def mock_notify(context, cls, _type, _priority, _payload): self.notify_called = True self.context_arg = context self.stubs.Set(notifier_api, 'notify', mock_notify) # Test positional context self.assertEqual(3, example_api(1, 2, ctxt)) self.assertEqual(self.notify_called, True) self.assertEqual(self.context_arg, ctxt) self.notify_called = False self.context_arg = None # Test named context self.assertEqual(3, example_api2(1, 2, context=ctxt2)) self.assertEqual(self.notify_called, True) self.assertEqual(self.context_arg, ctxt2) # Test missing context self.assertEqual(3, example_api2(1, 2, bananas="delicious")) self.assertEqual(self.notify_called, True) self.assertIsNone(self.context_arg)
def test_decorator_context(self): """Verify that the notify decorator can extract the 'context' arg.""" self.notify_called = False self.context_arg = None def example_api(arg1, arg2, context): return arg1 + arg2 def example_api2(arg1, arg2, **kw): return arg1 + arg2 example_api = notifier_api.notify_decorator('example_api', example_api) example_api2 = notifier_api.notify_decorator('example_api2', example_api2) def mock_notify(context, cls, _type, _priority, _payload): self.notify_called = True self.context_arg = context self.stubs.Set(notifier_api, 'notify', mock_notify) # Test positional context self.assertEqual(3, example_api(1, 2, ctxt)) self.assertEqual(self.notify_called, True) self.assertEqual(self.context_arg, ctxt) self.notify_called = False self.context_arg = None # Test named context self.assertEqual(3, example_api2(1, 2, context=ctxt2)) self.assertEqual(self.notify_called, True) self.assertEqual(self.context_arg, ctxt2) # Test missing context self.assertEqual(3, example_api2(1, 2, bananas="delicious")) self.assertEqual(self.notify_called, True) self.assertEqual(self.context_arg, None)
def test_send_notification_by_decorator(self): self.notify_called = False def example_api(arg1, arg2): return arg1 + arg2 example_api = notifier_api.notify_decorator("example_api", example_api) def mock_notify(cls, *args): self.notify_called = True self.stubs.Set(no_op_notifier, "notify", mock_notify) self.assertEqual(3, example_api(1, 2)) self.assertEqual(self.notify_called, True)
def test_send_notification_by_decorator(self): self.notify_called = False def example_api(arg1, arg2): return arg1 + arg2 example_api = notifier_api.notify_decorator('example_api', example_api) def mock_notify(cls, *args): self.notify_called = True self.stubs.Set(no_op_notifier, 'notify', mock_notify) self.assertEqual(3, example_api(1, 2)) self.assertEqual(self.notify_called, True)