diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 0000000..44ab445 --- /dev/null +++ b/tests/test_config.py @@ -0,0 +1,210 @@ +# pylint: disable=redefined-outer-name,missing-function-docstring,protected-access,global-statement +""" Tests on config lib """ + +from mylib.config import Config, ConfigSection +from mylib.config import StringOption + +runned = dict() + + +def test_config_init_default_args(): + appname = 'Test app' + config = Config(appname) + assert config.appname == appname + assert config.version == '0.0' + assert config.encoding == 'utf-8' + + +def test_config_init_custom_args(): + appname = 'Test app' + version = '1.43' + encoding = 'ISO-8859-1' + config = Config(appname, version=version, encoding=encoding) + assert config.appname == appname + assert config.version == version + assert config.encoding == encoding + + +def test_add_section_default_args(): + config = Config('Test app') + name = 'test_section' + section = config.add_section(name) + assert isinstance(section, ConfigSection) + assert config.sections[name] == section + assert section.name == name + assert section.comment is None + assert section.order == 10 + + +def test_add_section_custom_args(): + config = Config('Test app') + name = 'test_section' + comment = 'Test' + order = 20 + section = config.add_section(name, comment=comment, order=order) + assert isinstance(section, ConfigSection) + assert section.name == name + assert section.comment == comment + assert section.order == order + + +def test_add_section_with_callback(): + config = Config('Test app') + name = 'test_section' + + global runned + runned['test_add_section_with_callback'] = False + + def test_callback(loaded_config): + global runned + assert loaded_config == config + assert runned['test_add_section_with_callback'] is False + runned['test_add_section_with_callback'] = True + + section = config.add_section(name, loaded_callback=test_callback) + assert isinstance(section, ConfigSection) + assert test_callback in config._loaded_callbacks + assert runned['test_add_section_with_callback'] is False + + config.parse_arguments_options(argv=[], create=False) + assert runned['test_add_section_with_callback'] is True + assert test_callback in config._loaded_callbacks_executed + # Try to execute again to verify callback is not runned again + config._loaded() + + +def test_add_section_with_callback_already_loaded(): + config = Config('Test app') + name = 'test_section' + config.parse_arguments_options(argv=[], create=False) + + global runned + runned['test_add_section_with_callback_already_loaded'] = False + + def test_callback(loaded_config): + global runned + assert loaded_config == config + assert runned['test_add_section_with_callback_already_loaded'] is False + runned['test_add_section_with_callback_already_loaded'] = True + + section = config.add_section(name, loaded_callback=test_callback) + assert isinstance(section, ConfigSection) + assert runned['test_add_section_with_callback_already_loaded'] is True + assert test_callback in config._loaded_callbacks + assert test_callback in config._loaded_callbacks_executed + # Try to execute again to verify callback is not runned again + config._loaded() + + +def test_add_option_default_args(): + config = Config('Test app') + section = config.add_section('my_section') + assert isinstance(section, ConfigSection) + name = 'my_option' + option = section.add_option(StringOption, name) + assert isinstance(option, StringOption) + assert name in section.options and section.options[name] == option + assert option.config == config + assert option.section == section + assert option.name == name + assert option.default is None + assert option.comment is None + assert option.no_arg is False + assert option.arg is None + assert option.short_arg is None + assert option.arg_help is None + + +def test_add_option_custom_args(): + config = Config('Test app') + section = config.add_section('my_section') + assert isinstance(section, ConfigSection) + name = 'my_option' + kwargs = dict( + default='default value', + comment='my comment', + no_arg=True, + arg='--my-option', + short_arg='-M', + arg_help='My help' + ) + option = section.add_option(StringOption, name, **kwargs) + assert isinstance(option, StringOption) + assert name in section.options and section.options[name] == option + + assert option.config == config + assert option.section == section + assert option.name == name + for arg, value in kwargs.items(): + assert getattr(option, arg) == value + + +def test_defined(): + config = Config('Test app') + section_name = 'my_section' + opt_name = 'my_option' + assert not config.defined(section_name, opt_name) + + section = config.add_section('my_section') + assert isinstance(section, ConfigSection) + section.add_option(StringOption, opt_name) + + assert config.defined(section_name, opt_name) + + +def test_isset(): + config = Config('Test app') + section_name = 'my_section' + opt_name = 'my_option' + assert not config.isset(section_name, opt_name) + + section = config.add_section('my_section') + assert isinstance(section, ConfigSection) + option = section.add_option(StringOption, opt_name) + + assert not config.isset(section_name, opt_name) + + config.parse_arguments_options(argv=[option.parser_argument_name, 'value'], create=False) + + assert config.isset(section_name, opt_name) + + +def test_not_isset(): + config = Config('Test app') + section_name = 'my_section' + opt_name = 'my_option' + assert not config.isset(section_name, opt_name) + + section = config.add_section('my_section') + assert isinstance(section, ConfigSection) + option = section.add_option(StringOption, opt_name) + + assert not config.isset(section_name, opt_name) + + config.parse_arguments_options(argv=[], create=False) + + assert not config.isset(section_name, opt_name) + + +def test_get(): + config = Config('Test app') + section_name = 'my_section' + opt_name = 'my_option' + opt_value = 'value' + section = config.add_section('my_section') + option = section.add_option(StringOption, opt_name) + config.parse_arguments_options(argv=[option.parser_argument_name, opt_value], create=False) + + assert config.get(section_name, opt_name) == opt_value + + +def test_get_default(): + config = Config('Test app') + section_name = 'my_section' + opt_name = 'my_option' + opt_default_value = 'value' + section = config.add_section('my_section') + option = section.add_option(StringOption, opt_name, default=opt_default_value) + config.parse_arguments_options(argv=[], create=False) + + assert config.get(section_name, opt_name) == opt_default_value