No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_settings_loader.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #-*- coding: utf-8 -*-
  2. import unittest
  3. import os.path
  4. from lodel.settings.utils import *
  5. from lodel.settings.settings_loader import SettingsLoader
  6. #A dummy validator that only returns the value
  7. def dummy_validator(value): return value
  8. #A dummy validator that always fails
  9. def dummy_validator_fails(value): raise ValueError("Fake validation error")
  10. def write_list_validator(value):
  11. res = ''
  12. errors = list()
  13. for elt in value:
  14. res += dummy_validator(elt) + ','
  15. return res[:len(res)-1]
  16. class SettingsLoaderTestCase(unittest.TestCase):
  17. def test_merge_getsection(self):
  18. """Tests merge and getSection functions """
  19. settings = SettingsLoader('tests/settings/settings_examples/conf.d')
  20. def maFonction(a):
  21. return a
  22. e=settings.getoption('lodel2.A','a',maFonction)
  23. self.assertEqual(e,'a1')
  24. f=settings.getoption('lodel2.B','bb',maFonction)
  25. self.assertEqual(f,"bj,kl,mn")
  26. g=settings.getremains()
  27. self.assertIsNotNone(g)
  28. e=settings.getoption('lodel2.A','b',maFonction)
  29. e=settings.getoption('lodel2.A','c',maFonction)
  30. e=settings.getoption('lodel2.A','fhui',maFonction)
  31. f=settings.getoption('lodel2.B','ab',maFonction)
  32. f=settings.getoption('lodel2.B','cb',maFonction)
  33. f=settings.getoption('lodel2.C','cb',maFonction)
  34. f=settings.getoption('lodel2.C','ca',maFonction)
  35. f=settings.getoption('lodel2.C','cc',maFonction)
  36. f=settings.getoption('lodel2.C','a',maFonction)
  37. f=settings.getoption('lodel2.A.e','a',maFonction)
  38. f=settings.getoption('lodel2.A.e','titi',maFonction)
  39. g=settings.getremains()
  40. self.assertEqual(len(g),0)
  41. with self.assertRaises(SettingsError):
  42. loader = SettingsLoader('tests/settings/settings_examples/conf_raise.d')
  43. def test_merge(self):
  44. """ Test merge of multiple configuration files """
  45. loader = SettingsLoader('tests/settings/settings_examples/merge.conf.d')
  46. for value in ('a','b','c','d','e','f'):
  47. self.assertEqual(loader.getoption('lodel2', value, dummy_validator), value)
  48. self.assertEqual(loader.getoption('lodel2.othersection', value, dummy_validator), value)
  49. def test_merge_conflict(self):
  50. """ Test merge fails because of duplicated keys """
  51. with self.assertRaises(SettingsError):
  52. loader = SettingsLoader('tests/settings/settings_examples/bad_merge.conf.d')
  53. def test_getoption_simple(self):
  54. """ Testing behavior of getoption """
  55. loader = SettingsLoader('tests/settings/settings_examples/simple.conf.d')
  56. value = loader.getoption('lodel2.foo.bar', 'foo', dummy_validator)
  57. self.assertEqual(value, "42")
  58. value = loader.getoption('lodel2.foo.bar', 'foobar', dummy_validator)
  59. self.assertEqual(value, "hello world")
  60. value = loader.getoption('lodel2.foo.bar', 'foo_bar', dummy_validator)
  61. self.assertEqual(value, "foobar")
  62. value = loader.getoption('lodel2.foo.bar', 'foo.bar', dummy_validator)
  63. self.assertEqual(value, "barfoo")
  64. def test_getoption_multiple_time(self):
  65. """ Testing the behavior when doing 2 time the same call to getoption """
  66. loader = SettingsLoader('tests/settings/settings_examples/simple.conf.d')
  67. value = loader.getoption('lodel2.foo.bar', 'foo', dummy_validator)
  68. value = loader.getoption('lodel2.foo.bar', 'foo', dummy_validator)
  69. value = loader.getoption('lodel2.foo.bar', 'foo', dummy_validator)
  70. value = loader.getoption('lodel2.foo.bar', 'foo', dummy_validator)
  71. def test_geoption_default_value(self):
  72. """ Testing behavior of default value in getoption """
  73. loader = SettingsLoader('tests/settings/settings_examples/simple.conf.d')
  74. # for non existing keys in file
  75. value = loader.getoption('lodel2.foo.bar', 'foofoofoo', dummy_validator, 'hello 42', False)
  76. self.assertEqual(value, 'hello 42')
  77. # for non existing section in file
  78. value = loader.getoption('lodel2.foofoo', 'foofoofoo', dummy_validator, 'hello 42', False)
  79. self.assertEqual(value, 'hello 42')
  80. def test_getoption_complex(self):
  81. """ Testing behavior of getoption with less simple files & confs """
  82. expected = {
  83. 'lodel2.editorialmodel': {
  84. 'lib_path': '/tmp',
  85. 'foo_bar': '42',
  86. 'foo.bar': '1337',
  87. 'example': 'foobar',
  88. },
  89. 'lodel2.conf1': {
  90. 'conf_key': 'woot',
  91. 'conf_value': '42',
  92. },
  93. 'lodel2.conf2': {
  94. 'conf_foo': 'bar',
  95. },
  96. 'lodel2.foo.foo': {
  97. 'foobar': 'barfoo',
  98. },
  99. 'lodel2.bar.foo': {
  100. 'foobar': 'barfoo',
  101. 'barfoo': '42',
  102. },
  103. 'lodel2.bar.bar': {
  104. 'toto': 'tata',
  105. }
  106. }
  107. loader = SettingsLoader('tests/settings/settings_examples/complex.conf.d')
  108. for section in expected:
  109. for key, expected_value in expected[section].items():
  110. value = loader.getoption( section,
  111. key,
  112. dummy_validator)
  113. self.assertEqual(value, expected_value)
  114. def test_variable_sections(self):
  115. """ Testing variable section recognition """
  116. loader = SettingsLoader('tests/settings/settings_examples/var_sections.conf.d')
  117. sections = loader.getsection('lodel2.tests')
  118. self.assertEqual( set(sections),
  119. set(( 'lodel2.tests.section1',
  120. 'lodel2.tests.section2')))
  121. def test_variable_sections_default(self):
  122. """ Testing variable section default value handling """
  123. loader = SettingsLoader('tests/settings/settings_examples/var_sections.conf.d')
  124. sections = loader.getsection('lodel2.notexisting', 'foobar')
  125. self.assertEqual(set(sections), set(('lodel2.notexisting.foobar',)))
  126. def test_variable_sections_fails(self):
  127. """ Testing behavior when no default section given for a non existing variable section """
  128. loader = SettingsLoader('tests/settings/settings_examples/var_sections.conf.d')
  129. with self.assertRaises(NameError):
  130. sections = loader.getsection('lodel2.notexisting')
  131. def test_remains(self):
  132. """ Testing the remains method of SettingsLoader """
  133. loader = SettingsLoader('tests/settings/settings_examples/remains.conf.d')
  134. values = {
  135. 'lodel2.section': [ chr(i) for i in range(ord('a'), ord('f')) ],
  136. 'lodel2.othersection': [ chr(i) for i in range(ord('a'), ord('f')) ],
  137. }
  138. expt_rem = []
  139. for section in values:
  140. for val in values[section]:
  141. expt_rem.append('%s:%s' % (section, val))
  142. self.assertEqual(sorted(expt_rem), sorted(loader.getremains().keys()))
  143. for section in values:
  144. for val in values[section]:
  145. loader.getoption(section, val, dummy_validator)
  146. expt_rem.remove('%s:%s' % (section, val))
  147. self.assertEqual( sorted(expt_rem),
  148. sorted(loader.getremains().keys()))
  149. def test_setoption(self):
  150. loader=SettingsLoader('tests/settings/settings_examples/conf_set.d')
  151. loader.setoption('lodel2.A','fhui','test ok',dummy_validator)
  152. loader=SettingsLoader('tests/settings/settings_examples/conf_set.d')
  153. option=loader.getoption('lodel2.A','fhui',dummy_validator)
  154. self.assertEqual(option,'test ok')
  155. loader.setoption('lodel2.A','fhui','retour',dummy_validator)
  156. loader=SettingsLoader('tests/settings/settings_examples/conf_set.d')
  157. option=loader.getoption('lodel2.A','fhui',dummy_validator)
  158. self.assertEqual(option,'retour')
  159. cblist=('test ok1','test ok2','test ok3')
  160. loader.setoption('lodel2.C','cb',cblist,write_list_validator)
  161. loader=SettingsLoader('tests/settings/settings_examples/conf_set.d')
  162. option=loader.getoption('lodel2.C','cb',dummy_validator)
  163. self.assertEqual(option,'test ok1,test ok2,test ok3')
  164. cblist=('b4','b2','b3')
  165. loader.setoption('lodel2.C','cb',cblist,write_list_validator)
  166. loader=SettingsLoader('tests/settings/settings_examples/conf_set.d')
  167. option=loader.getoption('lodel2.C','cb',dummy_validator)
  168. self.assertEqual(option,'b4,b2,b3')
  169. def test_saveconf(self):
  170. loader=SettingsLoader('tests/settings/settings_examples/conf_save.d')
  171. newsec=dict()
  172. newsec['lodel2.A'] = dict()
  173. newsec['lodel2.A']['fhui'] = 'test ok'
  174. newsec['lodel2.A']['c'] = 'test ok'
  175. newsec['lodel2.A.e'] = dict()
  176. newsec['lodel2.A.e']['a'] = 'test ok'
  177. validators = dict()
  178. validators['lodel2.A'] = dict()
  179. validators['lodel2.A']['fhui'] = dummy_validator
  180. validators['lodel2.A']['c'] = dummy_validator
  181. validators['lodel2.A.e'] = dict()
  182. validators['lodel2.A.e']['a'] = dummy_validator
  183. loader.saveconf(newsec,validators)
  184. loader=SettingsLoader('tests/settings/settings_examples/conf_save.d')
  185. option=loader.getoption('lodel2.A','fhui',dummy_validator)
  186. self.assertEqual(option,'test ok')
  187. option=loader.getoption('lodel2.A','c',dummy_validator)
  188. self.assertEqual(option,'test ok')
  189. option=loader.getoption('lodel2.A.e','a',dummy_validator)
  190. self.assertEqual(option,'test ok')
  191. newsec['lodel2.A']['fhui']='retour'
  192. newsec['lodel2.A']['c']='toto'
  193. newsec['lodel2.A.e']['a']='ft'
  194. loader.saveconf(newsec,validators)
  195. loader=SettingsLoader('tests/settings/settings_examples/conf_save.d')
  196. option=loader.getoption('lodel2.A','fhui',dummy_validator)
  197. self.assertEqual(option,'retour')
  198. option=loader.getoption('lodel2.A','c',dummy_validator)
  199. self.assertEqual(option,'toto')
  200. option=loader.getoption('lodel2.A.e','a',dummy_validator)
  201. self.assertEqual(option,'ft')
  202. def test_setoption_default_value(self):
  203. loader = SettingsLoader('tests/settings/settings_examples/conf_setdef.d')
  204. # for non existing keys in file
  205. value = loader.getoption('lodel2.foo.bar', 'foofoofoo', dummy_validator, 'hello 42', False)
  206. self.assertEqual(value, 'hello 42')
  207. # for non existing section in file
  208. value = loader.getoption('lodel2.foofoo', 'foofoofoo', dummy_validator, 'hello 42', False)
  209. self.assertEqual(value, 'hello 42')
  210. loader.setoption('lodel2.foo.bar', 'foofoofoo', 'test ok', dummy_validator)
  211. loader.setoption('lodel2.foofoo', 'foofoofoo', 'test ok', dummy_validator)
  212. self.assertTrue(os.path.isfile('tests/settings/settings_examples/conf_setdef.d/generated.ini'))
  213. loader = SettingsLoader('tests/settings/settings_examples/conf_setdef.d')
  214. value = loader.getoption('lodel2.foofoo', 'foofoofoo', dummy_validator)
  215. self.assertEqual(value, 'test ok')
  216. value = loader.getoption('lodel2.foo.bar', 'foofoofoo', dummy_validator)
  217. self.assertEqual(value, 'test ok')
  218. os.remove('tests/settings/settings_examples/conf_setdef.d/generated.ini')
  219. def test_invalid_conf(self):
  220. from lodel.settings.settings import Settings
  221. Settings.stop()
  222. with self.assertRaises(SettingsErrors):
  223. Settings('tests/settings/settings_examples/bad_conf.d')