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

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