test_config.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from flexmock import flexmock
  2. from nose.tools import assert_raises
  3. from atticmatic import config as module
  4. def insert_mock_parser(section_names):
  5. parser = flexmock()
  6. parser.should_receive('readfp')
  7. parser.should_receive('sections').and_return(section_names)
  8. flexmock(module).open = lambda filename: None
  9. flexmock(module).ConfigParser = parser
  10. return parser
  11. def test_parse_configuration_should_return_config_data():
  12. section_names = (module.CONFIG_SECTION_LOCATION, module.CONFIG_SECTION_RETENTION)
  13. parser = insert_mock_parser(section_names)
  14. for section_name in section_names:
  15. parser.should_receive('options').with_args(section_name).and_return(
  16. module.CONFIG_FORMAT[section_name],
  17. )
  18. expected_config = (
  19. module.LocationConfig(flexmock(), flexmock()),
  20. module.RetentionConfig(flexmock(), flexmock(), flexmock()),
  21. )
  22. sections = (
  23. (module.CONFIG_SECTION_LOCATION, expected_config[0], 'get'),
  24. (module.CONFIG_SECTION_RETENTION, expected_config[1], 'getint'),
  25. )
  26. for section_name, section_config, method_name in sections:
  27. for index, option_name in enumerate(module.CONFIG_FORMAT[section_name]):
  28. (
  29. parser.should_receive(method_name).with_args(section_name, option_name)
  30. .and_return(section_config[index])
  31. )
  32. config = module.parse_configuration(flexmock())
  33. assert config == expected_config
  34. def test_parse_configuration_with_missing_section_should_raise():
  35. insert_mock_parser((module.CONFIG_SECTION_LOCATION,))
  36. with assert_raises(ValueError):
  37. module.parse_configuration(flexmock())
  38. def test_parse_configuration_with_extra_section_should_raise():
  39. insert_mock_parser((module.CONFIG_SECTION_LOCATION, module.CONFIG_SECTION_RETENTION, 'extra'))
  40. with assert_raises(ValueError):
  41. module.parse_configuration(flexmock())
  42. def test_parse_configuration_with_missing_option_should_raise():
  43. section_names = (module.CONFIG_SECTION_LOCATION, module.CONFIG_SECTION_RETENTION)
  44. parser = insert_mock_parser(section_names)
  45. for section_name in section_names:
  46. parser.should_receive('options').with_args(section_name).and_return(
  47. module.CONFIG_FORMAT[section_name][:-1],
  48. )
  49. with assert_raises(ValueError):
  50. module.parse_configuration(flexmock())
  51. def test_parse_configuration_with_extra_option_should_raise():
  52. section_names = (module.CONFIG_SECTION_LOCATION, module.CONFIG_SECTION_RETENTION)
  53. parser = insert_mock_parser(section_names)
  54. for section_name in section_names:
  55. parser.should_receive('options').with_args(section_name).and_return(
  56. module.CONFIG_FORMAT[section_name] + ('extra',),
  57. )
  58. with assert_raises(ValueError):
  59. module.parse_configuration(flexmock())