json_configobj_tests.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. """Tests for cement.ext.ext_json_configobj."""
  2. import json
  3. import sys
  4. from tempfile import mkstemp
  5. from cement.core import handler, backend, hook
  6. from cement.utils import test
  7. if sys.version_info[0] < 3:
  8. import configobj
  9. else:
  10. raise test.SkipTest(
  11. 'ConfigObj does not support Python 3') # pragma: no cover
  12. class JsonConfigObjExtTestCase(test.CementExtTestCase):
  13. CONFIG = '''{
  14. "section": {
  15. "subsection": {
  16. "list": [
  17. "item1", "item2", "item3", "item4"],
  18. "key": "value"
  19. },
  20. "key1": "ok1",
  21. "key2": "ok2"
  22. }
  23. }
  24. '''
  25. CONFIG_PARSED = dict(
  26. section=dict(
  27. subsection=dict(
  28. list=['item1', 'item2', 'item3', 'item4'],
  29. key='value'),
  30. key1='ok1',
  31. key2='ok2',
  32. ),
  33. )
  34. def setUp(self):
  35. _, self.tmppath = mkstemp()
  36. f = open(self.tmppath, 'w+')
  37. f.write(self.CONFIG)
  38. f.close()
  39. self.app = self.make_app('tests',
  40. extensions=['json_configobj'],
  41. config_handler='json_configobj',
  42. config_files=[self.tmppath],
  43. )
  44. def test_has_section(self):
  45. self.app.setup()
  46. self.ok(self.app.config.has_section('section'))
  47. def test_keys(self):
  48. self.app.setup()
  49. res = 'subsection' in self.app.config.keys('section')
  50. self.ok(res)
  51. def test_parse_file_bad_path(self):
  52. self.app._meta.config_files = ['./some_bogus_path']
  53. self.app.setup()
  54. def test_parse_file(self):
  55. self.app.setup()
  56. self.eq(self.app.config.get('section', 'key1'), 'ok1')
  57. self.eq(self.app.config.get_section_dict('section'),
  58. self.CONFIG_PARSED['section'])