yaml_configobj_tests.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """Tests for cement.ext.ext_yaml_configobj."""
  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. if sys.version_info[0] < 3:
  9. import configobj
  10. else:
  11. raise test.SkipTest(
  12. 'ConfigObj does not support Python 3') # pragma: no cover
  13. class YamlConfigObjExtTestCase(test.CementTestCase):
  14. CONFIG = '''
  15. section:
  16. subsection:
  17. list:
  18. - item1
  19. - item2
  20. - item3
  21. - item4
  22. key: value
  23. key1: ok1
  24. key2: ok2
  25. '''
  26. CONFIG_PARSED = dict(
  27. section=dict(
  28. subsection=dict(
  29. list=['item1', 'item2', 'item3', 'item4'],
  30. key='value'),
  31. key1='ok1',
  32. key2='ok2',
  33. ),
  34. )
  35. def setUp(self):
  36. _, self.tmppath = mkstemp()
  37. f = open(self.tmppath, 'w+')
  38. f.write(self.CONFIG)
  39. f.close()
  40. self.app = self.make_app('tests',
  41. extensions=['yaml_configobj'],
  42. config_handler='yaml_configobj',
  43. config_files=[self.tmppath],
  44. )
  45. def tearDown(self):
  46. if os.path.exists(self.tmppath):
  47. os.remove(self.tmppath)
  48. def test_has_section(self):
  49. self.app.setup()
  50. self.ok(self.app.config.has_section('section'))
  51. def test_keys(self):
  52. self.app.setup()
  53. res = 'subsection' in self.app.config.keys('section')
  54. self.ok(res)
  55. def test_parse_file_bad_path(self):
  56. self.app._meta.config_files = ['./some_bogus_path']
  57. self.app.setup()
  58. def test_parse_file(self):
  59. self.app.setup()
  60. self.eq(self.app.config.get('section', 'key1'), 'ok1')
  61. self.eq(self.app.config.get_section_dict('section'),
  62. self.CONFIG_PARSED['section'])