formatter_test.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import logging
  2. from compose.cli import colors
  3. from compose.cli.formatter import ConsoleWarningFormatter
  4. from tests import unittest
  5. MESSAGE = 'this is the message'
  6. def make_log_record(level, message=None):
  7. return logging.LogRecord('name', level, 'pathame', 0, message or MESSAGE, (), None)
  8. class ConsoleWarningFormatterTestCase(unittest.TestCase):
  9. def setUp(self):
  10. self.formatter = ConsoleWarningFormatter()
  11. def test_format_warn(self):
  12. output = self.formatter.format(make_log_record(logging.WARN))
  13. expected = colors.yellow('WARNING') + ': '
  14. assert output == expected + MESSAGE
  15. def test_format_error(self):
  16. output = self.formatter.format(make_log_record(logging.ERROR))
  17. expected = colors.red('ERROR') + ': '
  18. assert output == expected + MESSAGE
  19. def test_format_info(self):
  20. output = self.formatter.format(make_log_record(logging.INFO))
  21. assert output == MESSAGE
  22. def test_format_unicode_info(self):
  23. message = b'\xec\xa0\x95\xec\x88\x98\xec\xa0\x95'
  24. output = self.formatter.format(make_log_record(logging.INFO, message))
  25. assert output == message.decode('utf-8')
  26. def test_format_unicode_warn(self):
  27. message = b'\xec\xa0\x95\xec\x88\x98\xec\xa0\x95'
  28. output = self.formatter.format(make_log_record(logging.WARN, message))
  29. expected = colors.yellow('WARNING') + ': '
  30. assert output == '{0}{1}'.format(expected, message.decode('utf-8'))
  31. def test_format_unicode_error(self):
  32. message = b'\xec\xa0\x95\xec\x88\x98\xec\xa0\x95'
  33. output = self.formatter.format(make_log_record(logging.ERROR, message))
  34. expected = colors.red('ERROR') + ': '
  35. assert output == '{0}{1}'.format(expected, message.decode('utf-8'))