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

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