tests: add some tests on BooleanOption
This commit is contained in:
parent
6a7368deb5
commit
508a28e5c8
1 changed files with 119 additions and 0 deletions
|
@ -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} =
|
||||
"""
|
||||
|
|
Loading…
Reference in a new issue