Ejemplo n.º 1
0
def test_main_create_template_evaluate(files, string_io, evaluate):
    o_name = "xe-template.zirkon"
    s_name = "xe.zirkon-schema"
    with files.tmp(o_name):
        o_file = files[o_name]
        s_file = files[s_name]

        assert not os.path.exists(files[o_name])

        args = ["create", "-s", s_file, "-o", o_file]
        if evaluate:
            args.append("-e")
        log_stream, out_stream = run(args)

        config = Config()
        config.read(o_file, fmt="zirkon")
        config.dump(string_io)
        if evaluate:
            assert string_io.getvalue() == """\
a = 10
b = 20
c = 200
"""
        else:
            assert string_io.getvalue() == """\
Ejemplo n.º 2
0
def test_Config_on_shelf(tmpdir):
    with shelve.open(os.path.join(tmpdir.strpath, 'x.shelve')) as shelf:
        flatshelf = FlatMap(dictionary=shelf)
        config = Config(dictionary=flatshelf)
        config['x'] = 10
        config['y'] = {}
        config['y']['a'] = 'aaa'
        config['y']['b'] = 111
        config['y']['c'] = {'v': 10}
        config['y']['c']['w'] = 20
        config['y']['d'] = 888
        config['z'] = 999
        config_stream = string_io()
        config.dump(stream=config_stream)
        config_dump = config_stream.getvalue()
        assert config_dump == """\
z = 999
x = 10
[y]
    b = 111
    [c]
        v = 10
        w = 20
    a = 'aaa'
    d = 888
"""
    with shelve.open(os.path.join(tmpdir.strpath, 'x.shelve')) as shelf2:
        flatshelf2 = FlatMap(dictionary=shelf2)
        config2 = Config(dictionary=flatshelf2)
        config2_stream = string_io()
        config2.dump(stream=config2_stream)
        config2_dump = config2_stream.getvalue()
        assert config2_dump == config_dump
Ejemplo n.º 3
0
def test_Config_create_dictionary_init(dictionary, simple_config_content,
                                       string_io, defaultsvalue):
    config = Config(dictionary=dictionary,
                    init=simple_config_content,
                    defaults=defaultsvalue)
    config.dump(stream=string_io)
    assert string_io.getvalue() == SIMPLE_SECTION_DUMP
    assert len(dictionary) > 0
Ejemplo n.º 4
0
def test_create_template_from_schema(string_io):
    schema = Schema()
    schema['m0'] = Int(default=3)
    schema['m1'] = Int(default=ROOT['m0'] + 1)
    config = Config()
    create_template_from_schema(schema, config=config)
    
    config.dump(stream=string_io)
    assert string_io.getvalue() == """\
Ejemplo n.º 5
0
def test_Config_defaults_dump(string_io):
    config = Config(defaults=True)
    config['x'] = 1
    config.set_defaults(alpha=10)
    config['sub'] = {'w': 2}
    config['sub'].set_defaults(beta=20, gamma={'x': 11}, delta={})
    assert config['sub']['beta'] == 20
    assert config['sub']['gamma']['x'] == 11
    config.dump(string_io)
    assert string_io.getvalue() == """\
Ejemplo n.º 6
0
def test_Config_defaults_dump(string_io):
    config = Config(defaults=True)
    config['x'] = 1
    config.set_defaults(alpha=10)
    config['sub'] = {'w': 2}
    config['sub'].set_defaults(beta=20, gamma={'x': 11}, delta={})
    assert config['sub']['beta'] == 20
    assert config['sub']['gamma']['x'] == 11
    config.dump(string_io)
    assert string_io.getvalue() == """\
Ejemplo n.º 7
0
def test_main_create_template(files, string_io):
    o_name = "x-template.zirkon"
    s_name = "x.zirkon-schema"
    with files.tmp(o_name):
        o_file = files[o_name]
        s_file = files[s_name]

        assert not os.path.exists(files[o_name])

        args = ["create", "-s", s_file, "-o", o_file]
        log_stream, out_stream = run(args)

        config = Config()
        config.read(o_file, protocol="zirkon")
        config.dump(string_io)
        assert string_io.getvalue() == """\
Ejemplo n.º 8
0
def test_Config_use_defaults_False(string_io, use_defaults):
    config = Config()
    schema = Schema(use_defaults=use_defaults)
    schema['x'] = Int(default=10)
    schema['sub'] = {'y': Int(default=20)}
    schema['sub']['sub'] = {'z': Int(default=30)}
    validation = schema.validate(config)
    assert config['x'] == 10
    assert config['sub']['y'] == 20
    assert config['sub']['sub']['z'] == 30
    assert not validation
    config.dump(string_io)
    if use_defaults:
        assert string_io.getvalue() == """\
[sub]
    [sub]
"""
    else:
        assert string_io.getvalue() == """\
Ejemplo n.º 9
0
def test_Config_use_defaults_False(string_io, use_defaults):
    config = Config()
    schema = Schema(use_defaults=use_defaults)
    schema['x'] = Int(default=10)
    schema['sub'] = {'y': Int(default=20)}
    schema['sub']['sub'] = {'z': Int(default=30)}
    validation = schema.validate(config)
    assert config['x'] == 10
    assert config['sub']['y'] == 20
    assert config['sub']['sub']['z'] == 30
    assert not validation
    config.dump(string_io)
    if use_defaults:
        assert string_io.getvalue() == """\
[sub]
    [sub]
"""
    else:
        assert string_io.getvalue() == """\
Ejemplo n.º 10
0
def test_set_key(string_io):
    config = Config()
    with pytest.raises(KeyError) as exc_info:
        set_key(config, (), 3)
    assert str(exc_info.value) == "()"

    with pytest.raises(KeyError) as exc_info:
        set_key(config, "", 3)
    assert str(exc_info.value) == "''"

    set_key(config, "x", 10)
    set_key(config, "sub", {'y': 20})
    set_key(config, "sub.z", 30)
    with pytest.raises(KeyError) as exc_info:
        set_key(config, "sub.sub2.w", 40)
    assert str(exc_info.value) == "'sub2'"
    set_key(config, "sub.sub2.w", 40, parents=True)

    config.dump(string_io)
    assert string_io.getvalue() == """\
Ejemplo n.º 11
0
def test_del_key(string_io):
    config = Config()
    config['x'] = 10
    config['sub'] = {'y': 20}
    config['sub']['sub1'] = {'z': 30, 'w': 40}
    config['sub']['sub2'] = {'z': 30, 'w': 40}

    del_key(config, "x")
    with pytest.raises(KeyError) as exc_info:
        del_key(config, "x")
    assert str(exc_info.value) == "'x'"
    del_key(config, "x", ignore_errors=True)

    with pytest.raises(KeyError) as exc_info:
        del_key(config, "sub.sub3.xx")
    assert str(exc_info.value) == "'sub3'"
    
    del_key(config, "sub.sub1")
    del_key(config, "sub.sub2.z")
    config.dump(string_io)
    assert string_io.getvalue() == """\
Ejemplo n.º 12
0
def test_Config_on_shelf(tmpdir):
    with shelve.open(os.path.join(tmpdir.strpath, "x.shelve")) as shelf:
        flatshelf = FlatMap(dictionary=shelf)
        config = Config(dictionary=flatshelf)
        config["x"] = 10
        config["y"] = {}
        config["y"]["a"] = "aaa"
        config["y"]["b"] = 111
        config["y"]["c"] = {"v": 10}
        config["y"]["c"]["w"] = 20
        config["y"]["d"] = 888
        config["z"] = 999
        config_stream = string_io()
        config.dump(stream=config_stream)
        config_dump = config_stream.getvalue()
        assert (
            config_dump
            == """\
z = 999
x = 10
[y]
    b = 111
    [c]
        v = 10
        w = 20
    a = 'aaa'
    d = 888
"""
        )
    with shelve.open(os.path.join(tmpdir.strpath, "x.shelve")) as shelf2:
        flatshelf2 = FlatMap(dictionary=shelf2)
        config2 = Config(dictionary=flatshelf2)
        config2_stream = string_io()
        config2.dump(stream=config2_stream)
        config2_dump = config2_stream.getvalue()
        assert config2_dump == config_dump
Ejemplo n.º 13
0
def test_Config_create_empty(string_io, defaultsvalue):
    config = Config(defaults=defaultsvalue)
    config.dump(stream=string_io)
    assert string_io.getvalue() == ""
Ejemplo n.º 14
0
def test_Config_create_dictionary(generic_dictionary, string_io, defaultsvalue):
    config = Config(dictionary=generic_dictionary, defaults=defaultsvalue)
    config.dump(stream=string_io)
    assert string_io.getvalue() == ""
Ejemplo n.º 15
0
def test_Config_create_init(simple_config_content, string_io, defaultsvalue):
    config = Config(init=simple_config_content, defaults=defaultsvalue)
    config.dump(stream=string_io)
    assert string_io.getvalue() == SIMPLE_SECTION_DUMP
Ejemplo n.º 16
0
def test_Config_create_dictionary_init(dictionary, simple_config_content, string_io, defaultsvalue):
    config = Config(dictionary=dictionary, init=simple_config_content, defaults=defaultsvalue)
    config.dump(stream=string_io)
    assert string_io.getvalue() == SIMPLE_SECTION_DUMP
    assert len(dictionary) > 0
Ejemplo n.º 17
0
def test_Config_create_init(simple_config_content, string_io, defaultsvalue):
    config = Config(init=simple_config_content, defaults=defaultsvalue)
    config.dump(stream=string_io)
    assert string_io.getvalue() == SIMPLE_SECTION_DUMP
Ejemplo n.º 18
0
def test_Config_create_dictionary(generic_dictionary, string_io,
                                  defaultsvalue):
    config = Config(dictionary=generic_dictionary, defaults=defaultsvalue)
    config.dump(stream=string_io)
    assert string_io.getvalue() == ""
Ejemplo n.º 19
0
def test_Config_create_empty(string_io, defaultsvalue):
    config = Config(defaults=defaultsvalue)
    config.dump(stream=string_io)
    assert string_io.getvalue() == ""