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 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #-*- coding: utf-8 -*-
  2. import unittest
  3. from lodel.settings.utils import *
  4. from lodel.settings.settings_loader import SettingsLoader
  5. #A dummy validator that only returns the value
  6. def dummy_validator(value): return value
  7. #A dummy validator that always fails
  8. def dummy_validator_fails(value): raise ValueError("Fake validaion error")
  9. class SettingsLoaderTestCase(unittest.TestCase):
  10. def test_merge_getsection(self):
  11. """Tests merge and getSection functions """
  12. settings = SettingsLoader('tests/settings/conf.d')
  13. def maFonction(a):
  14. return a
  15. e=settings.getoption('A','a',maFonction)
  16. self.assertEqual(e,'a1')
  17. f=settings.getoption('B','bb',maFonction)
  18. self.assertEqual(f,"bj,kl,mn")
  19. g=settings.getremains()
  20. self.assertIsNotNone(g)
  21. e=settings.getoption('A','b',maFonction)
  22. e=settings.getoption('A','c',maFonction)
  23. e=settings.getoption('A','fhui',maFonction)
  24. f=settings.getoption('B','ab',maFonction)
  25. f=settings.getoption('B','cb',maFonction)
  26. f=settings.getoption('C','cb',maFonction)
  27. f=settings.getoption('C','ca',maFonction)
  28. f=settings.getoption('C','cc',maFonction)
  29. g=settings.getremains()
  30. self.assertEqual(g,[])
  31. def test_merge(self):
  32. """ Test merge of multiple configuration files """
  33. loader = SettingsLoader('tests/settings/settings_examples/merge.conf.d')
  34. for value in ('a','b','c','d','e','f'):
  35. self.assertEqual(loader.getoption('lodel2', value, dummy_validator), value)
  36. self.assertEqual(loader.getoption('lodel2.othersection', value, dummy_validator), value)
  37. def test_merge_conflict(self):
  38. """ Test merge fails because of duplicated keys """
  39. with self.assertRaises(SettingsError):
  40. loader = SettingsLoader('tests/settings/settings_examples/bad_merge.conf.d')
  41. def test_getoption_simple(self):
  42. """ Testing behavior of getoption """
  43. loader = SettingsLoader('tests/settings/settings_examples/simple.conf.d')
  44. value = loader.getoption('lodel2.foo.bar', 'foo', dummy_validator)
  45. self.assertEqual(value, "42")
  46. value = loader.getoption('lodel2.foo.bar', 'foobar', dummy_validator)
  47. self.assertEqual(value, "hello world")
  48. def test_variable_sections(self):
  49. """ Testing variable section recognition """
  50. loader = SettingsLoader('tests/settings/settings_examples/var_sections.conf.d')
  51. sections = loader.getsection('lodel2.tests')
  52. self.assertEqual( set(sections),
  53. set(( 'lodel2.tests.section1',
  54. 'lodel2.tests.section2')))
  55. def test_variable_sections_default(self):
  56. """ Testing variable section default value handling """
  57. loader = SettingsLoader('tests/settings/settings_examples/var_sections.conf.d')
  58. sections = loader.getsection('lodel2.notexisting', 'foobar')
  59. self.assertEqual(set(sections), set(('lodel2.notexisting.foobar',)))
  60. def test_variable_sections_fails(self):
  61. """ Testing behavior when no default section given for a non existing variable section """
  62. loader = SettingsLoader('tests/settings/settings_examples/var_sections.conf.d')
  63. with self.assertRaises(NameError):
  64. sections = loader.getsection('lodel2.notexisting')
  65. @unittest.skip("Waiting implementation")
  66. def test_remains(self):
  67. """ Testing the remains method of SettingsLoader """
  68. loader = SettingsLoader('tests/settings/settings_examples/remains.conf.d')
  69. pass #TO BE DONE LATER