From 508a28e5c89ae08229f35c8f1bcfa7e02311d4e0 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Mon, 9 Jan 2023 13:33:14 +0100 Subject: [PATCH] tests: add some tests on BooleanOption --- tests/test_config.py | 119 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/tests/test_config.py b/tests/test_config.py index 804cfb8..326eb96 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -3,8 +3,13 @@ """ Tests on config lib """ import logging +import os + +import configparser +import pytest from mylib.config import Config, ConfigSection +from mylib.config import BooleanOption from mylib.config import StringOption runned = {} @@ -225,3 +230,117 @@ def test_logging_splited_stdout_stderr(capsys): assert info_msg not in captured.err assert err_msg in captured.err assert err_msg not in captured.out + + +# +# Test option types +# + + +@pytest.fixture() +def config_with_file(tmpdir): + config = Config('Test app') + config_dir = tmpdir.mkdir('config') + config_file = config_dir.join('config.ini') + config.save(os.path.join(config_file.dirname, config_file.basename)) + return config + + +def generate_mock_input(expected_prompt, input_value): + def mock_input(self, prompt): # pylint: disable=unused-argument + assert prompt == expected_prompt + return input_value + return mock_input + + +# Boolean option + + +def test_boolean_option_from_config(config_with_file): + section = config_with_file.add_section('test') + default = True + option = section.add_option( + BooleanOption, 'test_bool', default=default) + config_with_file.save() + + option.set(not default) + assert option._from_config is not default + + option.set(default) + assert not option._isset_in_config_file + with pytest.raises(configparser.NoOptionError): + assert option._from_config is default + + +def test_boolean_option_ask_value(mocker): + config = Config('Test app') + section = config.add_section('test') + name = 'test_bool' + option = section.add_option( + BooleanOption, name, default=True) + + mocker.patch( + 'mylib.config.BooleanOption._get_user_input', + generate_mock_input(f'{name}: [Y/n] ', 'y') + ) + assert option.ask_value(set_it=False) is True + + mocker.patch( + 'mylib.config.BooleanOption._get_user_input', + generate_mock_input(f'{name}: [Y/n] ', 'Y') + ) + assert option.ask_value(set_it=False) is True + + mocker.patch( + 'mylib.config.BooleanOption._get_user_input', + generate_mock_input(f'{name}: [Y/n] ', '') + ) + assert option.ask_value(set_it=False) is True + + mocker.patch( + 'mylib.config.BooleanOption._get_user_input', + generate_mock_input(f'{name}: [Y/n] ', 'n') + ) + assert option.ask_value(set_it=False) is False + + mocker.patch( + 'mylib.config.BooleanOption._get_user_input', + generate_mock_input(f'{name}: [Y/n] ', 'N') + ) + assert option.ask_value(set_it=False) is False + + +def test_boolean_option_to_config(): + config = Config('Test app') + section = config.add_section('test') + default = True + option = section.add_option(BooleanOption, 'test_bool', default=default) + assert option.to_config(True) == 'true' + assert option.to_config(False) == 'false' + + +def test_boolean_option_export_to_config(config_with_file): + section = config_with_file.add_section('test') + name = 'test_bool' + comment = 'Test boolean' + default = True + + option = section.add_option( + BooleanOption, name, default=default, comment=comment) + + assert option.export_to_config() == f"""# {comment} +# Default: {str(default).lower()} +# {name} = +""" + + option.set(not default) + assert option.export_to_config() == f"""# {comment} +# Default: {str(default).lower()} +{name} = {str(not default).lower()} +""" + + option.set(default) + assert option.export_to_config() == f"""# {comment} +# Default: {str(default).lower()} +# {name} = +"""