core/tests/components/test_logger.py

62 lines
1.9 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the Logger component."""
from collections import namedtuple
2016-01-30 18:03:46 +00:00
import logging
import unittest
2016-09-20 04:12:56 +00:00
from homeassistant.bootstrap import setup_component
2016-01-30 18:03:46 +00:00
from homeassistant.components import logger
from tests.common import get_test_home_assistant
RECORD = namedtuple('record', ('name', 'levelno'))
2016-01-30 18:03:46 +00:00
class TestUpdater(unittest.TestCase):
2016-03-09 09:25:50 +00:00
"""Test logger component."""
2016-01-30 18:03:46 +00:00
def setUp(self):
2016-03-09 09:25:50 +00:00
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.log_config = {'logger':
{'default': 'warning', 'logs': {'test': 'info'}}}
2016-01-30 18:03:46 +00:00
def tearDown(self):
2016-03-09 09:25:50 +00:00
"""Stop everything that was started."""
del logging.root.handlers[-1]
self.hass.stop()
def test_logger_setup(self):
2016-03-09 09:25:50 +00:00
"""Use logger to create a logging filter."""
setup_component(self.hass, logger.DOMAIN, self.log_config)
2016-01-30 18:03:46 +00:00
self.assertTrue(len(logging.root.handlers) > 0)
handler = logging.root.handlers[-1]
self.assertEqual(len(handler.filters), 1)
log_filter = handler.filters[0].logfilter
self.assertEqual(log_filter['default'], logging.WARNING)
self.assertEqual(log_filter['logs']['test'], logging.INFO)
def test_logger_test_filters(self):
2016-03-09 09:25:50 +00:00
"""Test resulting filter operation."""
setup_component(self.hass, logger.DOMAIN, self.log_config)
log_filter = logging.root.handlers[-1].filters[0]
2016-03-09 09:25:50 +00:00
# Blocked default record
record = RECORD('asdf', logging.DEBUG)
self.assertFalse(log_filter.filter(record))
2016-03-09 09:25:50 +00:00
# Allowed default record
record = RECORD('asdf', logging.WARNING)
self.assertTrue(log_filter.filter(record))
2016-03-09 09:25:50 +00:00
# Blocked named record
record = RECORD('test', logging.DEBUG)
self.assertFalse(log_filter.filter(record))
2016-03-09 09:25:50 +00:00
# Allowed named record
record = RECORD('test', logging.INFO)
self.assertTrue(log_filter.filter(record))