test_config.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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('read')
  7. parser.should_receive('sections').and_return(section_names)
  8. flexmock(module).SafeConfigParser = parser
  9. return parser
  10. def test_parse_configuration_should_return_config_data():
  11. section_names = (module.CONFIG_SECTION_LOCATION, module.CONFIG_SECTION_RETENTION)
  12. parser = insert_mock_parser(section_names)
  13. for section_name in section_names:
  14. parser.should_receive('options').with_args(section_name).and_return(
  15. module.CONFIG_FORMAT[section_name],
  16. )
  17. expected_config = (
  18. module.LocationConfig(flexmock(), flexmock()),
  19. module.RetentionConfig(flexmock(), flexmock(), flexmock()),
  20. )
  21. sections = (
  22. (module.CONFIG_SECTION_LOCATION, expected_config[0], 'get'),
  23. (module.CONFIG_SECTION_RETENTION, expected_config[1], 'getint'),
  24. )
  25. for section_name, section_config, method_name in sections:
  26. for index, option_name in enumerate(module.CONFIG_FORMAT[section_name]):
  27. (
  28. parser.should_receive(method_name).with_args(section_name, option_name)
  29. .and_return(section_config[index])
  30. )
  31. config = module.parse_configuration(flexmock())
  32. assert config == expected_config
  33. def test_parse_configuration_with_missing_section_should_raise():
  34. insert_mock_parser((module.CONFIG_SECTION_LOCATION,))
  35. with assert_raises(ValueError):
  36. module.parse_configuration(flexmock())
  37. def test_parse_configuration_with_extra_section_should_raise():
  38. insert_mock_parser((module.CONFIG_SECTION_LOCATION, module.CONFIG_SECTION_RETENTION, 'extra'))
  39. with assert_raises(ValueError):
  40. module.parse_configuration(flexmock())
  41. def test_parse_configuration_with_missing_option_should_raise():
  42. section_names = (module.CONFIG_SECTION_LOCATION, module.CONFIG_SECTION_RETENTION)
  43. parser = insert_mock_parser(section_names)
  44. for section_name in section_names:
  45. parser.should_receive('options').with_args(section_name).and_return(
  46. module.CONFIG_FORMAT[section_name][:-1],
  47. )
  48. with assert_raises(ValueError):
  49. module.parse_configuration(flexmock())
  50. def test_parse_configuration_with_extra_option_should_raise():
  51. section_names = (module.CONFIG_SECTION_LOCATION, module.CONFIG_SECTION_RETENTION)
  52. parser = insert_mock_parser(section_names)
  53. for section_name in section_names:
  54. parser.should_receive('options').with_args(section_name).and_return(
  55. module.CONFIG_FORMAT[section_name] + ('extra',),
  56. )
  57. with assert_raises(ValueError):
  58. module.parse_configuration(flexmock())