def test_null_finish_template_for_context_manager(self, func_name, level): context = log_context.LogContext(self.logger, templates={ 'context.start': 'Start', 'context.finish': '', }) context_manager = getattr(context, func_name) with context_manager('context label'): pass self.logger.log.assert_called_once_with(level, "Start", stacklevel=3)
def test_null_finish_template_for_decorator(self, func_name, level): context = log_context.LogContext(self.logger, templates={ 'function.start': 'Start', 'function.finish': '', }) decorator = getattr(context, func_name) @decorator def function(): pass function() self.logger.log.assert_called_once_with(level, "Start", stacklevel=3)
def test_decorator_with_args_delegates_to_logger(self, func_name, level): context = log_context.LogContext(self.logger, templates={ 'function.start': 'Called `{label}({arguments})`', 'function.finish': 'Return from `{label}`', }) decorator = getattr(context, func_name) @decorator(show_args=True, show_kwargs=True) def function(a, b=None): pass # Decorating the function shouldn't log anything. self.logger.log.assert_not_called() function('a', b=1) self.logger.log.assert_has_calls([ mock.call(level, "Called `function('a', b=1)`", stacklevel=3), mock.call(level, 'Return from `function`', stacklevel=3), ])
def setup(self): self.logger = mock.Mock(spec=logging.Logger) self.context = log_context.LogContext(self.logger)