Exemple #1
0
def test_get_values(configdir, defaults):
    """
    Test that values are returned correctly with the right type.
    """
    conf = UserConfig(NAME, defaults=defaults, load=True, path=configdir,
                      backup=True, version=CONF_VERSION, raw_mode=True)

    for section, options in defaults:
        for option, value in options.items():
            assert conf.get(section, option) == value
            assert conf.get_default(section, option) == value

    # Get a value of an option that does not exists without providing a
    # default value.
    assert not conf.has_option('main', 'dummy')
    assert conf.get_default('main', 'dummy') is NoDefault
    with pytest.raises(cp.NoOptionError):
        conf.get('main', 'dummy')

    # Get a value of an option that does not exists while providing a
    # default value.
    assert not conf.has_option('main', 'dummy')
    assert conf.get_default('main', 'dummy') is NoDefault
    assert conf.get('main', 'dummy', 8.234) == 8.234
    assert conf.get_default('main', 'dummy') == 8.234

    # Get a value from section that does not exists without providing a
    # default value.
    assert not conf.has_section('dummy')
    assert conf.get_default('dummy', 'dummy') is NoDefault
    with pytest.raises(cp.NoSectionError):
        conf.get('dummy', 'dummy')

    # Get a value from section that does not exists while providing a
    # default value.
    assert not conf.has_section('dummy')
    assert conf.get_default('dummy', 'dummy') is NoDefault
    assert conf.get('dummy', 'dummy', 'new_value') == 'new_value'
    assert conf.get_default('dummy', 'dummy') == 'new_value'
Exemple #2
0
def test_bump_version(configdir, defaults):
    """
    Test bumping the configuration version and assert that the defaults
    gets updated as expected.
    """
    conf = UserConfig(NAME, defaults=defaults, load=True, path=configdir,
                      backup=True, version='0.1.0', raw_mode=True)

    assert osp.exists(conf.get_filename())
    assert osp.exists(osp.join(conf.path, 'defaults', 'defaults-0.1.0.ini'))

    # Delete option#4 in main section.
    del defaults[0][1]['option#4']
    # Delete section#1 in defaults.
    del defaults[1]
    # Change the default value of option#5 in main.
    defaults[0][1]['option#5'] = 'main_opt5_new_default'
    # Add and option to main section.
    defaults[0][1]['option#999'] = 'main_opt999_default'
    # Add a new section to defaults.
    defaults.append(('section#999', {'option#999': 'sec999_opt999_default'}))

    del conf
    conf = UserConfig(NAME, defaults=defaults, load=True, path=configdir,
                      backup=True, version='0.2.0', raw_mode=True)

    assert osp.exists(osp.join(conf.path, 'defaults', 'defaults-0.2.0.ini'))
    assert osp.exists(conf.get_filename() + '-0.1.0.bak')

    assert not conf.has_option('main', 'option#4')
    assert not conf.has_section('section#1')
    assert conf.get('main', 'option#5') == 'main_opt5_new_default'
    assert conf.get_default('main', 'option#5') == 'main_opt5_new_default'
    assert conf.get('main', 'option#999') == 'main_opt999_default'
    assert conf.get_default('main', 'option#999') == 'main_opt999_default'
    assert conf.get('section#999', 'option#999') == 'sec999_opt999_default'
    assert conf.get_default('section#999', 'option#999') == (
        'sec999_opt999_default')