Пример #1
0
    def test_getattr_forwards_to_logger(self, create_from_instance):
        logger_cls = Mock()
        logger_args = (Mock(), )
        logger_kwargs = {'mock': Mock()}
        bundle = Mock()
        event = Mock()
        create_from_instance.return_value = bundle
        logger = Mock()
        logger_cls.return_value = logger

        proxy = LoggerProxy(logger_cls, logger_args, logger_kwargs)
        proxy._setup_proxy(event)

        self.assertEqual(proxy.some_attr, logger.some_attr)
Пример #2
0
    def test_getattr_with_no_logger_raises(self, create_from_instance):
        bundle = Mock()
        create_from_instance.return_value = bundle

        proxy = LoggerProxy(Mock(), Mock(), Mock())

        self.assertRaises(ValueError, lambda: proxy.some_attr)
Пример #3
0
    def test_setup_proxy(self, create_from_instance):
        logger_cls = Mock()
        logger_args = (Mock(), )
        logger_kwargs = {'mock': Mock()}
        bundle = Mock()
        event = Mock()
        create_from_instance.return_value = bundle
        logger = Mock()
        logger_cls.return_value = logger

        proxy = LoggerProxy(logger_cls, logger_args, logger_kwargs)
        proxy._setup_proxy(event)

        logger_cls.assert_called_once_with(event=event,
                                           *logger_args,
                                           **logger_kwargs)
        logger.start.assert_called_once_with(event)
Пример #4
0
    def test_setattr_with_no_logger_raises(self, create_from_instance):
        bundle = Mock()
        create_from_instance.return_value = bundle
        value = Mock()

        proxy = LoggerProxy(Mock(), Mock(), Mock())

        def try_set():
            proxy.some_attr = value

        self.assertRaises(ValueError, try_set)
Пример #5
0
    def test_init(self, create_from_instance):
        logger_cls = Mock()
        logger_args = Mock()
        logger_kwargs = Mock()
        bundle = Mock()
        create_from_instance.return_value = bundle
        proxy = LoggerProxy(logger_cls, logger_args, logger_kwargs)

        self.assertEqual(proxy._logger_cls, logger_cls)
        self.assertEqual(proxy._logger_args, logger_args)
        self.assertEqual(proxy._logger_kwargs, logger_kwargs)
        self.assertIsNone(proxy._logger)
        create_from_instance.assert_called_once_with(proxy)
        bundle.register.assert_called_once_with()
Пример #6
0
    def test_teardown_proxy(self, create_from_instance):
        logger_cls = Mock()
        logger_args = (Mock(), )
        logger_kwargs = {'mock': Mock()}
        bundle = Mock()
        event = Mock()
        create_from_instance.return_value = bundle
        logger = Mock()
        logger_cls.return_value = logger

        proxy = LoggerProxy(logger_cls, logger_args, logger_kwargs)
        proxy._setup_proxy(event)
        proxy._teardown_proxy(event)

        logger.end.assert_called_once_with(event)
        self.assertIsNone(proxy._logger)
Пример #7
0
    def test_teardown_proxy_logs_upon_exception(self, create_from_instance,
                                                logging_error, logging_debug):
        logger_cls = Mock()
        logger_args = (Mock(), )
        logger_kwargs = {'mock': Mock()}
        bundle = Mock()
        event = Mock()
        create_from_instance.return_value = bundle
        logger = Mock()
        logger.end.side_effect = ValueError('test')
        logger_cls.return_value = logger

        proxy = LoggerProxy(logger_cls, logger_args, logger_kwargs)
        proxy._setup_proxy(event)
        proxy._teardown_proxy(event)

        self.assertTrue(logging_error.called)
        self.assertTrue(logging_debug.called)
        self.assertIsNone(proxy._logger)