From db9c946f85a9ed64390b3a6fa5906a80c12a3698 Mon Sep 17 00:00:00 2001 From: voith Date: Wed, 21 Sep 2016 00:14:39 +0530 Subject: [PATCH] Added tests for filters --- tests/test_filters.py | 122 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 tests/test_filters.py diff --git a/tests/test_filters.py b/tests/test_filters.py new file mode 100644 index 000000000..796faecdb --- /dev/null +++ b/tests/test_filters.py @@ -0,0 +1,122 @@ +import unittest + +from frontera.logger.filters import PLAINVALUES, INCLUDEFIELDS, EXCLUDEFIELDS +from tests.utils import LoggingCaptureMixin, SetupDefaultLoggingMixin + + +class BaseTestFilters(SetupDefaultLoggingMixin, LoggingCaptureMixin, unittest.TestCase): + def tearDown(self): + super(BaseTestFilters, self).setUp() + self.logger.handlers[0].filters = [] + + def addFilter(self, filter): + self.logger.handlers[0].addFilter(filter) + + +class TestFilterPlainValues(BaseTestFilters): + def test_plain_values_exclude_fields(self): + filter = PLAINVALUES(excluded_fields=['event']) + self.addFilter(filter) + self.logger.debug({'message1': 'logging', 'message2': 'debug', 'event': 'value'}) + log_msg = self.logger_output.getvalue() + assert log_msg == 'logging debug\n' or log_msg == 'debug logging\n' + + def test_plain_values_separator(self): + filter = PLAINVALUES(separator=',') + self.addFilter(filter) + self.logger.debug({'message1': 'logging', 'message2': 'debug'}) + log_msg = self.logger_output.getvalue() + assert log_msg == 'logging,debug\n' or log_msg == 'debug,logging\n' + + def test_plain_values_msg_max_length(self): + filter = PLAINVALUES(msg_max_length=10) + self.addFilter(filter) + self.logger.debug({'message1': '1' * 10, 'message2': '2' * 10}) + log_msg = self.logger_output.getvalue() + assert log_msg == '%s...\n' % ('1' * 7) or log_msg == '%s...\n' % ('2' * 7) + + def test_plain_values_str_msg(self): + filter = PLAINVALUES(msg_max_length=10) + self.addFilter(filter) + self.logger.debug('debug message') + self.assertEqual(self.logger_output.getvalue(), 'debug message\n') + + +class TestIncludeFields(BaseTestFilters): + def test_include_fields_matching_values(self): + filter = INCLUDEFIELDS(field_name='event', included_values=['page_crawled']) + self.addFilter(filter) + self.logger.debug('crawled page P', extra={'event': 'page_crawled'}) + self.assertEqual(self.logger_output.getvalue(), 'crawled page P\n') + + def test_include_fields_non_matching_values(self): + filter = INCLUDEFIELDS(field_name='event', included_values=['links_extracted']) + self.addFilter(filter) + self.logger.debug('crawled page P', extra={'event': 'page_crawled'}) + self.assertEqual(self.logger_output.getvalue(), '') + + def test_include_fields_dict_msg_matching_values(self): + filter = INCLUDEFIELDS(field_name='event', included_values=['page_crawled']) + self.addFilter(filter) + self.logger.debug({'message': 'debug message', 'event': 'page_crawled'}) + log_msg = self.logger_output.getvalue() + assert log_msg == "{'event': 'page_crawled', 'message': 'debug message'}\n" or \ + log_msg == "{'message': 'debug message', 'event': 'page_crawled'}\n" + + def test_include_fields_dict_msg_non_matching_values(self): + filter = INCLUDEFIELDS(field_name='event', included_values=['links_extracted']) + self.addFilter(filter) + self.logger.debug({'message': 'debug message', 'event': 'page_crawled'}) + self.assertEqual(self.logger_output.getvalue(), '') + + def test_include_fields_field_name_none(self): + filter = INCLUDEFIELDS(field_name=None, included_values=[]) + self.addFilter(filter) + self.logger.debug('debug message') + self.assertEqual(self.logger_output.getvalue(), 'debug message\n') + + def test_include_fields_list_message(self): + filter = INCLUDEFIELDS(field_name='event', included_values=['page_crawled']) + self.addFilter(filter) + self.logger.debug(['debug message']) + self.assertEqual(self.logger_output.getvalue(), "['debug message']\n") + + +class TestExcludeFields(BaseTestFilters): + def test_exclude_fields_matching_values(self): + filter = EXCLUDEFIELDS(field_name='event', excluded_fields=['page_crawled']) + self.addFilter(filter) + self.logger.debug('crawled page P', extra={'event': 'page_crawled'}) + self.assertEqual(self.logger_output.getvalue(), '') + + def test_exclude_fields_non_matching_values(self): + filter = EXCLUDEFIELDS(field_name='event', excluded_fields=['links_extracted']) + self.addFilter(filter) + self.logger.debug('crawled page P', extra={'event': 'page_crawled'}) + self.assertEqual(self.logger_output.getvalue(), 'crawled page P\n') + + def test_exclude_fields_dict_msg_matching_values(self): + filter = EXCLUDEFIELDS(field_name='event', excluded_fields='page_crawled') + self.addFilter(filter) + self.logger.debug({'message': 'debug message', 'event': 'page_crawled'}) + self.assertEqual(self.logger_output.getvalue(), '') + + def test_exclude_fields_dict_msg_non_matching_values(self): + filter = EXCLUDEFIELDS(field_name='event', excluded_fields='links_extracted') + self.addFilter(filter) + self.logger.debug({'message': 'debug message', 'event': 'page_crawled'}) + log_msg = self.logger_output.getvalue() + assert log_msg == "{'event': 'page_crawled', 'message': 'debug message'}\n" or \ + log_msg == "{'message': 'debug message', 'event': 'page_crawled'}\n" + + def test_include_fields_field_name_none(self): + filter = EXCLUDEFIELDS(field_name=None, excluded_fields=[]) + self.addFilter(filter) + self.logger.debug('debug message') + self.assertEqual(self.logger_output.getvalue(), 'debug message\n') + + def test_include_fields_list_message(self): + filter = EXCLUDEFIELDS(field_name='event', excluded_fields=['page_crawled']) + self.addFilter(filter) + self.logger.debug(['debug message']) + self.assertEqual(self.logger_output.getvalue(), "['debug message']\n")