Exemplo 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() == """\
Exemplo n.º 2
0
def test_Config_self_validate_error_dump(string_io, config):
    del config['parameters']['frequencies'][1:]
    assert len(config['parameters']['frequencies'])
    with pytest.raises(ConfigValidationError) as exc_info:
        config.dump()
    assert str(exc_info.value) == "validation error: Validation(parameters=ValidationSection(frequencies=MinLengthError('parameters.frequencies=[5.0]: length 1 is lower than min_len 2',)))"
    exc_info.value.validation.dump(string_io)
    assert string_io.getvalue() == """\
Exemplo n.º 3
0
def test_CustomObject_config_schema_to_string(config_source, schema_source, string_io):
    with Schema.scope().registered(HOSTNAME=HOSTNAME, WORKDIR=WORKDIR), \
         Config.scope().registered(HOSTNAME=HOSTNAME, WORKDIR=WORKDIR):
        schema = Schema.from_string(schema_source)
        config = Config.from_string(config_source, defaults=False, schema=schema)
        config.to_stream(string_io)
        s = string_io.getvalue()
        assert s == """\
Exemplo n.º 4
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
Exemplo n.º 5
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() == """\
Exemplo n.º 6
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() == """\
Exemplo n.º 7
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() == """\
Exemplo 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() == """\
Exemplo n.º 9
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() == """\
Exemplo n.º 10
0
def test_Config_self_validate_error_dump(string_io, config):
    del config['parameters']['frequencies'][1:]
    assert len(config['parameters']['frequencies'])
    with pytest.raises(ConfigValidationError) as exc_info:
        config.dump()
    assert str(
        exc_info.value
    ) == "validation error: Validation(parameters=ValidationSection(frequencies=MinLengthError('parameters.frequencies=[5.0]: length 1 is lower than min_len 2',)))"
    exc_info.value.validation.dump(string_io)
    assert string_io.getvalue() == """\
Exemplo n.º 11
0
def test_Section_getitem_section(simple_section, string_io):
    epsilon = simple_section["options"]["epsilon"]
    assert isinstance(epsilon, Section)
    epsilon.dump(stream=string_io)
    assert (
        string_io.getvalue()
        == """\
epsilon_x = 10
epsilon_y = 20
epsilon_z = 30
"""
    )
Exemplo n.º 12
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() == """\
Exemplo n.º 13
0
def test_create_template_from_schema(string_io):
    schema = Schema()
    schema['m0'] = Int(default=3)
    schema['m1'] = Int()
    schema['alpha'] = Float(default=0.5)
    schema['input_file'] = Str(min_len=1)
    schema['data'] = {}
    schema['data']['i'] = Int(max=10)
    schema['data']['isub'] = {}
    schema['data']['isub']['a'] = Str()
    schema['data']['isub']['b'] = Str(default='bc')
    schema['data']['isub']['c'] = Str(default=SECTION['b'] * ROOT['m0'])
    schema['data']['isub']['d'] = Str(default=SECTION['b'] * ROOT['m1'])
    schema['data']['j'] = Int(default=2)
    config = create_template_from_schema(schema)
    config.dump(stream=string_io)
    assert string_io.getvalue() == """\
Exemplo n.º 14
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() == """\
Exemplo n.º 15
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() == """\
Exemplo n.º 16
0
def test_Section_update(dictionary, simple_config_content, string_io):
    section = Section(dictionary=dictionary)
    section.update(simple_config_content)
    section.dump(stream=string_io)
    assert string_io.getvalue() == SIMPLE_SECTION_DUMP
Exemplo n.º 17
0
def test_Section_create(generic_dictionary, string_io):
    section = Section(dictionary=generic_dictionary)
    section.dump(stream=string_io)
    assert string_io.getvalue() == ""
Exemplo n.º 18
0
def test_Section_create(content, string_io):
    section = Section(dictionary=FlatMap(collections.OrderedDict()),
                      init=content)
    section.dump(stream=string_io)
    assert string_io.getvalue() == SECTION_DUMP
Exemplo n.º 19
0
def test_SchemaSection_init(dictionary, string_io, simple_schema_content):
    schema_section = SchemaSection(dictionary=dictionary, init=simple_schema_content)
    schema_section.dump()
    schema_section.dump(stream=string_io)
    assert string_io.getvalue() == """\
Exemplo n.º 20
0
def test_Config_dump_evaluate_True(cfgm, string_io):
    cfgm.dump(stream=string_io, evaluate=True)
    assert string_io.getvalue() == """\
def test_ZirkonSerializer_to_stream(simple_config_content, serializer, string_io):
    serializer.to_stream(obj=simple_config_content, stream=string_io)
    assert string_io.getvalue() == SIMPLE_CONFIG_ZIRKON_SERIALIZATION
Exemplo n.º 22
0
def test_Schema_create_empty(string_io):
    schema = Schema()
    schema.dump(stream=string_io)
    assert string_io.getvalue() == ""
Exemplo n.º 23
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
Exemplo n.º 24
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() == ""
Exemplo n.º 25
0
def test_Config_create_empty(string_io, defaultsvalue):
    config = Config(defaults=defaultsvalue)
    config.dump(stream=string_io)
    assert string_io.getvalue() == ""
Exemplo n.º 26
0
def test_Schema_create_dictionary(dictionary, string_io):
    schema = Schema(dictionary=dictionary)
    schema.dump(stream=string_io)
    assert string_io.getvalue() == ""
Exemplo n.º 27
0
def test_Schema_create_dictionary_init(dictionary, simple_schema_content, string_io):
    schema = Schema(dictionary=dictionary, init=simple_schema_content)
    schema.dump(stream=string_io)
    assert string_io.getvalue() == SIMPLE_SCHEMA_DUMP
    assert len(dictionary) > 0
Exemplo n.º 28
0
def test_Config_dump_evaluate_default(cfgm, string_io):
    cfgm.dump(stream=string_io)
    assert string_io.getvalue() == """\
Exemplo n.º 29
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
Exemplo n.º 30
0
def test_Schema_create_dictionary(dictionary, string_io):
    schema = Schema(dictionary=dictionary)
    schema.dump(stream=string_io)
    assert string_io.getvalue() == ""
Exemplo n.º 31
0
def test_Section_getitem_section(simple_section, string_io):
    epsilon = simple_section['options']['epsilon']
    assert isinstance(epsilon, Section)
    epsilon.dump(stream=string_io)
    assert string_io.getvalue() == """\
Exemplo n.º 32
0
def test_Section_create(content, string_io):
    section = Section(dictionary=FlatMap(collections.OrderedDict()), init=content)
    section.dump(stream=string_io)
    assert string_io.getvalue() == SECTION_DUMP
Exemplo n.º 33
0
def test_Config(string_io, config, config_content):
    config.dump(stream=string_io)
    assert string_io.getvalue() == CONFIG_DUMP
Exemplo n.º 34
0
def test_Section_update(dictionary, simple_config_content, string_io):
    section = Section(dictionary=dictionary)
    section.update(simple_config_content)
    section.dump(stream=string_io)
    assert string_io.getvalue() == SIMPLE_SECTION_DUMP
Exemplo n.º 35
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() == ""
Exemplo n.º 36
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
Exemplo n.º 37
0
def test_Section_create(generic_dictionary, string_io):
    section = Section(dictionary=generic_dictionary)
    section.dump(stream=string_io)
    assert string_io.getvalue() == ""
Exemplo n.º 38
0
def test_ConfigObjSerializer_to_stream(simple_config_content, serializer, string_io):
    serializer.to_stream(obj=simple_config_content, stream=string_io)
    assert string_io.getvalue() == SIMPLE_CONFIG_CONFIGOBJ_SERIALIZATION
Exemplo n.º 39
0
def test_trace_errors_all_exceptions(debug_mode, string_io):
    with pytest.raises(SystemExit):
        with trace_errors(debug_mode=debug_mode, stream=string_io):
            raise KeyError("alpha")
    assert string_io.getvalue() == "ERR: KeyError: 'alpha'\n"
Exemplo n.º 40
0
def test_Config_create_empty(string_io, defaultsvalue):
    config = Config(defaults=defaultsvalue)
    config.dump(stream=string_io)
    assert string_io.getvalue() == ""
Exemplo n.º 41
0
def test_Config(string_io, config, config_content):
    config.dump(stream=string_io)
    assert string_io.getvalue() == CONFIG_DUMP
Exemplo n.º 42
0
def test_trace_errors_ValueError(debug_mode, string_io):
    with pytest.raises(KeyError):
        with trace_errors(debug_mode=debug_mode, exceptions=(ValueError,), stream=string_io):
            raise KeyError("alpha")
    assert string_io.getvalue() == ""
Exemplo n.º 43
0
def test_Schema_create_empty(string_io):
    schema = Schema()
    schema.dump(stream=string_io)
    assert string_io.getvalue() == ""
Exemplo n.º 44
0
def test_ZirkonSerializer_to_stream(simple_config_content, serializer,
                                    string_io):
    serializer.to_stream(obj=simple_config_content, stream=string_io)
    assert string_io.getvalue() == SIMPLE_CONFIG_ZIRKON_SERIALIZATION
Exemplo n.º 45
0
def test_Schema_create_dictionary_init(dictionary, simple_schema_content, string_io):
    schema = Schema(dictionary=dictionary, init=simple_schema_content)
    schema.dump(stream=string_io)
    assert string_io.getvalue() == SIMPLE_SCHEMA_DUMP
    assert len(dictionary) > 0
Exemplo n.º 46
0
def test_Section_getitem_section(simple_section, string_io):
    epsilon = simple_section['options']['epsilon']
    assert isinstance(epsilon, Section)
    epsilon.dump(stream=string_io)
    assert string_io.getvalue() == """\