Esempio n. 1
0
def test_config_to_dict(tmpdir, config):
    data = """
[foo]
string = new
integer = 2
real = 4.1
boolean = true

[bar]
string = new
class = test.class
"""
    conf = str(tmpdir.join("conf"))
    with open(conf, "w") as f:
        f.write(data)
    configloader.load(config, [conf])
    expected = {
        "foo": {
            "string": "new",
            "integer": 2,
            "real": 4.1,
            "boolean": True,
            "string_nonascii": "\u05d0",
            "string_null": "\u0000",
        },
        "bar": {
            "string": "new",
            "class": "test.class"
        },
    }
    assert expected == configloader.to_dict(config)
Esempio n. 2
0
def test_false(tmpdir, config, value):
    data = """
[foo]
boolean = %s
""" % value
    conf = str(tmpdir.join("conf"))
    with open(conf, "w") as f:
        f.write(data)
    configloader.load(config, [conf])
    assert config.foo.boolean is False
Esempio n. 3
0
def test_empty(tmpdir, config):
    conf = str(tmpdir.join("conf"))
    with open(conf, "w"):
        pass
    configloader.load(config, [conf])
    assert config.foo.string == "old"
    assert config.foo.string_nonascii == "\u05d0"
    assert config.foo.string_null == "\u0000"
    assert config.foo.integer == 1
    assert config.foo.real == 4.0
    assert config.foo.boolean is False
    assert config.bar.string == "old"
Esempio n. 4
0
def test_keyword_option(tmpdir, config):
    data = """
[bar]
string = new
class = test.class
"""
    conf = str(tmpdir.join("conf"))
    with open(conf, "w") as f:
        f.write(data)
    configloader.load(config, [conf])
    assert config.bar.string == "new"
    assert config.bar.keyword__class == "test.class"
Esempio n. 5
0
def test_unicode(tmpdir, config):
    data = """
[foo]
string_nonascii = \u05d0
string_null = \u0000
"""
    conf = str(tmpdir.join("conf"))
    with open(conf, "wb") as f:
        f.write(data.encode("utf-8"))
    configloader.load(config, [conf])
    assert config.foo.string_nonascii == "\u05d0"
    assert config.foo.string_null == "\u0000"
Esempio n. 6
0
def test_some(tmpdir, config):
    data = """
[foo]
string = new
boolean = true
"""
    conf = str(tmpdir.join("conf"))
    with open(conf, "w") as f:
        f.write(data)
    configloader.load(config, [conf])
    assert config.foo.string == "new"
    assert config.foo.integer == 1
    assert config.foo.real == 4.0
    assert config.foo.boolean is True
    assert config.bar.string == "old"
Esempio n. 7
0
def test_ignore_unknown_option(tmpdir, config):
    data = """
[foo]
string = new
unknown = 3
"""
    conf = str(tmpdir.join("conf"))
    with open(conf, "w") as f:
        f.write(data)
    configloader.load(config, [conf])
    assert config.foo.string == "new"
    assert config.foo.integer == 1
    assert config.foo.real == 4.0
    assert config.foo.boolean is False
    assert config.bar.string == "old"
    assert not hasattr(config.foo, "unknown")
Esempio n. 8
0
def test_unsupported_default_value(tmpdir):
    class config:
        class section:
            value = b"bytes"

    data = """
[section]
value = bar
"""
    conf = str(tmpdir.join("conf"))
    with open(conf, "w") as f:
        f.write(data)

    with pytest.raises(ValueError) as e:
        configloader.load(config, [conf])

    error = str(e.value)
    assert "section.value" in error
    assert str(type(config.section.value)) in error
Esempio n. 9
0
def test_full(tmpdir, config):
    data = """
[foo]
string = new
integer = 2
real = 4.1
boolean = true

[bar]
string = new
"""
    conf = str(tmpdir.join("conf"))
    with open(conf, "w") as f:
        f.write(data)
    configloader.load(config, [conf])
    assert config.foo.string == "new"
    assert config.foo.integer == 2
    assert config.foo.real == 4.1
    assert config.foo.boolean is True
    assert config.bar.string == "new"