Ejemplo n.º 1
0
def test_set_values(configdir, mocker, defaults):
    """
    Test that values are set correctly with the right type corresponding to
    that of their default values if any.
    """
    conf = UserConfig(NAME, defaults=defaults, load=True, path=configdir,
                      backup=True, version=CONF_VERSION, raw_mode=True)

    # Set the value of options with default value.
    params = [('main', 'option#1', 34.678, '34.678'),
              ('main', 'option#3', 34.678, 34.678),
              ('main', 'option#4', 34.678, 34),
              ('main', 'option#5', '', False),
              ('main', 'option#5', 34.678, True)]

    mocked_save = mocker.patch('appconfigs.user.UserConfig._save')
    for i, (section, option, new_value, expected_value) in enumerate(params):
        conf.set(section, option, new_value)
        assert conf.get(section, option) == expected_value
        assert mocked_save.call_count == i + 1

    # Set the value of new options with no default value.
    params = [('main', 'new_option', 'some_str'),
              ('new_section', 'new_option', 12.123)]

    mocked_save.reset_mock()
    for i, (section, option, new_value) in enumerate(params):
        assert conf.get_default(section, option) is NoDefault

        conf.set(section, option, new_value)
        assert conf.get(section, option) == new_value
        assert conf.get_default(section, option) is new_value
        assert mocked_save.call_count == i + 1
Ejemplo n.º 2
0
def test_reset_to_defaults(configdir, defaults, mocker, save_value):
    """
    Test resetting config values to defaults.
    """
    conf = UserConfig(NAME, defaults=defaults, load=True, path=configdir,
                      backup=True, version='0.1.0', raw_mode=True)
    conf._create_backup()

    # Change the value of option#3 in main section.
    conf.set('main', 'option#3', 65.23)
    assert conf.get('main', 'option#3') == 65.23
    assert conf.get_default('main', 'option#3') == 24.567
    assert filecmp.cmp(
        conf.get_filename(), conf.get_filename() + '.bak', shallow=False
        ) is False

    # Reset the config values to defaults.
    conf.reset_to_defaults(save=save_value)
    assert conf.get('main', 'option#3') == 24.567
    assert conf.get_default('main', 'option#3') == 24.567
    assert filecmp.cmp(
        conf.get_filename(), conf.get_filename() + '.bak', shallow=False
        ) is save_value