misc_tests.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. """Tests for cement.utils.misc."""
  2. from cement.utils import test, misc
  3. class BackendTestCase(test.CementCoreTestCase):
  4. def test_defaults(self):
  5. defaults = misc.init_defaults('myapp', 'section2', 'section3')
  6. defaults['myapp']['debug'] = True
  7. defaults['section2']['foo'] = 'bar'
  8. self.app = self.make_app('myapp', config_defaults=defaults)
  9. self.app.setup()
  10. self.eq(self.app.config.get('myapp', 'debug'), True)
  11. self.ok(self.app.config.get_section_dict('section2'))
  12. def test_minimal_logger(self):
  13. log = misc.minimal_logger(__name__)
  14. log = misc.minimal_logger(__name__, debug=True)
  15. log.info('info test')
  16. log.warn('warn test')
  17. log.error('error test')
  18. log.fatal('fatal test')
  19. log.debug('debug test')
  20. log.info('info test with namespce', 'test_namespace')
  21. log.info('info test with extra kwargs', extra=dict(foo='bar'))
  22. log.info('info test with extra kwargs', extra=dict(namespace='foo'))
  23. # set logging back to non-debug
  24. misc.minimal_logger(__name__, debug=False)
  25. pass
  26. def test_wrap_str(self):
  27. text = "aaaaa bbbbb ccccc"
  28. new_text = misc.wrap(text, width=5)
  29. parts = new_text.split('\n')
  30. self.eq(len(parts), 3)
  31. self.eq(parts[1], 'bbbbb')
  32. new_text = misc.wrap(text, width=5, indent='***')
  33. parts = new_text.split('\n')
  34. self.eq(parts[2], '***ccccc')
  35. @test.raises(TypeError)
  36. def test_wrap_int(self):
  37. text = int('1' * 80)
  38. try:
  39. new_text = misc.wrap(text, width=5)
  40. except TypeError as e:
  41. self.eq(e.args[0], "`text` must be a string.")
  42. raise
  43. @test.raises(TypeError)
  44. def test_wrap_none(self):
  45. text = None
  46. try:
  47. new_text = misc.wrap(text, width=5)
  48. except TypeError as e:
  49. self.eq(e.args[0], "`text` must be a string.")
  50. raise