yaml_tests.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. """Tests for cement.ext.ext_yaml."""
  2. import os
  3. import sys
  4. import yaml
  5. from tempfile import mkstemp
  6. from cement.core import handler, hook
  7. from cement.utils import test
  8. from cement.utils.misc import rando
  9. APP = rando()[:12]
  10. class YamlExtTestCase(test.CementTestCase):
  11. CONFIG = '''
  12. section:
  13. subsection:
  14. list:
  15. - item1
  16. - item2
  17. - item3
  18. - item4
  19. key: value
  20. key1: ok1
  21. key2: ok2
  22. '''
  23. CONFIG_PARSED = dict(
  24. section=dict(
  25. subsection=dict(
  26. list=['item1', 'item2', 'item3', 'item4'],
  27. key='value'),
  28. key1='ok1',
  29. key2='ok2',
  30. ),
  31. )
  32. def setUp(self):
  33. _, self.tmppath = mkstemp()
  34. f = open(self.tmppath, 'w+')
  35. f.write(self.CONFIG)
  36. f.close()
  37. self.app = self.make_app('tests',
  38. extensions=['yaml'],
  39. config_handler='yaml',
  40. output_handler='yaml',
  41. config_files=[self.tmppath],
  42. argv=['-o', 'yaml']
  43. )
  44. def tearDown(self):
  45. if os.path.exists(self.tmppath):
  46. os.remove(self.tmppath)
  47. def test_yaml(self):
  48. self.app.setup()
  49. self.app.run()
  50. res = self.app.render(dict(foo='bar'))
  51. yaml_res = yaml.dump(dict(foo='bar'))
  52. self.eq(res, yaml_res)
  53. def test_has_section(self):
  54. self.app.setup()
  55. self.ok(self.app.config.has_section('section'))
  56. def test_keys(self):
  57. self.app.setup()
  58. res = 'subsection' in self.app.config.keys('section')
  59. self.ok(res)
  60. def test_parse_file_bad_path(self):
  61. self.app._meta.config_files = ['./some_bogus_path']
  62. self.app.setup()
  63. def test_parse_file(self):
  64. self.app.setup()
  65. self.eq(self.app.config.get('section', 'key1'), 'ok1')
  66. self.eq(self.app.config.get_section_dict('section'),
  67. self.CONFIG_PARSED['section'])
  68. def test_handler_override_options_is_none(self):
  69. app = self.make_app(APP,
  70. extensions=['yaml'],
  71. core_handler_override_options=None,
  72. handler_override_options=None
  73. )
  74. app.setup()
  75. app.run()
  76. app.render(dict(foo='bar'))