Exemplo n.º 1
0
def test_merge():
    section0 = Section()
    section0["a"] = 10
    section0["b"] = 20
    section0["sub0"] = {"x0": 10, "y0": 20}
    section0["sub0"]["subsub"] = {"wa": 100, "wb": 200}
    section0["sub1"] = {"x1": 11, "y1": 21}
    section1 = Section()
    section1["a"] = 1010
    section1["c"] = 1030
    section1["sub0"] = {"x0": 1010, "z0": 1030}
    section1["sub0"]["subsub"] = {"wa": 1100, "wc": 1300}
    section1["sub2"] = {"x2": 1012, "y2": 1022, "z2": 1032}
    # print(section0['sub0']['subsub'])
    section0.merge(section1)
    # print(section0['sub0']['subsub'])
    dct = section0.as_dict(dict_class=dict)
    # print(dct['sub0']['subsub'])
    expected = {
        "a": 1010,
        "b": 20,
        "c": 1030,
        "sub0": {"x0": 1010, "y0": 20, "z0": 1030, "subsub": {"wa": 1100, "wb": 200, "wc": 1300}},
        "sub1": {"x1": 11, "y1": 21},
        "sub2": {"x2": 1012, "y2": 1022, "z2": 1032},
    }
    # assert dct['sub0'] == expected['sub0']
    # assert dct['sub0']['subsub'] == expected['sub0']['subsub']
    assert dct == expected
Exemplo n.º 2
0
def test_Section_update3():
    upd1 = {'a': 1}
    upd2 = {'b': 2}
    flatmap = Section(dictionary=collections.OrderedDict())
    flatmap.update(upd1, **upd2)
    upd = {}
    upd.update(upd1)
    upd.update(upd2)
    assert flatmap == upd
Exemplo n.º 3
0
def test_Section_update3():
    upd1 = {'a': 1}
    upd2 = {'b': 2}
    flatmap = Section(dictionary=collections.OrderedDict())
    flatmap.update(upd1, **upd2)
    upd = {}
    upd.update(upd1)
    upd.update(upd2)
    assert flatmap == upd
Exemplo n.º 4
0
def test_Section_ne_1(simple_section):
    s0 = Section(init=simple_section)
    simple_section['options']['f'] = 34
    print("s0:")
    s0.dump()
    print()
    print("simple_section:")
    simple_section.dump()
    assert simple_section != s0
    assert s0 != simple_section
Exemplo n.º 5
0
def test_Section_ne_1(simple_section):
    s0 = Section(init=simple_section)
    simple_section['options']['f'] = 34
    print("s0:")
    s0.dump()
    print()
    print("simple_section:")
    simple_section.dump()
    assert simple_section != s0
    assert s0 != simple_section
Exemplo n.º 6
0
def test_Section_2(content):
    section = Section(dictionary=FlatMap(collections.OrderedDict()),
                      init=content)
    section2 = Section(dictionary=FlatMap(collections.OrderedDict()),
                       init=content)
    s_io = string_io()
    section.dump(stream=s_io)
    assert s_io.getvalue() == SECTION_DUMP
    section['sub']['y']['yfilename'] = 'y.dat'
    s_io1 = string_io()
    section.dump(stream=s_io1)
    assert s_io1.getvalue() != SECTION_DUMP
    s_io2 = string_io()
    section2.dump(stream=s_io2)
    assert s_io2.getvalue() == SECTION_DUMP
Exemplo n.º 7
0
def _section_tpl_2():
    section = Section()
    section['b'] = {}
    section['b']['bb'] = {}
    section['b']['bb']['bbb'] = {}
    tpl = ()
    return section, tpl
Exemplo n.º 8
0
def test_deepcopy():
    section1 = Section()
    section1["a"] = 0
    section1["b"] = {}
    section1["b"]["x"] = 1
    section1["b"]["y"] = 2
    section1["b"]["z"] = {"w": 3, "t": 4}
    section2 = section1.copy()
    del section2["a"]
    del section2["b"]["y"]
    del section2["b"]["z"]
    assert section1["a"] == 0
    assert section1["b"]["x"] == 1
    assert section1["b"]["y"] == 2
    assert section1["b"]["z"]["w"] == 3
    assert section1["b"]["z"]["t"] == 4
Exemplo n.º 9
0
def test_SchemaSection_validate_unexpected_ignored(dictionary, generic_dictionary, string_io, simple_schema_content, simple_section_content):
    simple_section_content['sub']['abc'] = 10
    schema_section = SchemaSection(dictionary=generic_dictionary, init=simple_schema_content, unexpected_option_validator=Ignore())
    section = Section(dictionary=dictionary, init=simple_section_content)
    validation = schema_section.validate(section)
    assert not validation
    assert section['sub'].has_option('abc')
    assert section['sub']['abc'] == 10
Exemplo n.º 10
0
def test_Section_invalid_dictionary_content():
    dictionary = {'a': (1, 23, {}), 'b': 8}
    section = Section(dictionary=dictionary)
    assert section['b'] == 8
    with pytest.raises(TypeError) as exc_info:
        a = section['a']
    assert str(exc_info.value
               ) == "option a: invalid tuple: item #2 {} has invalid type dict"
Exemplo n.º 11
0
def _section_tpl_0():
    section = Section()
    section['a'] = 1
    section['b'] = {'bx': 2}
    section['b']['by'] = 3
    section['c'] = 4
    tpl = (((), 'a', 1), ((), 'c', 4), (('b', ), 'bx', 2), (('b', ), 'by', 3))
    return section, tpl
Exemplo n.º 12
0
def test_Section_len_empty(generic_dictionary):
    section = Section(dictionary=generic_dictionary)
    assert len(section) == 0
    section['a'] = 10
    assert len(section) == 1
    section['b'] = {'x': 0, 'y': 1, 'z': 2}
    assert len(section) == 2
    assert len(section['b']) == 3
Exemplo n.º 13
0
def _section_tpl_3():
    section = Section()
    section['b'] = {}
    section['b']['bb'] = {}
    section['b']['bb']['bbb'] = {}
    section['b']['bb']['bbb']['x'] = 0
    tpl = ((('b', 'bb', 'bbb'), 'x', 0), )
    return section, tpl
Exemplo n.º 14
0
def test_SchemaSection_validate_unexpected(dictionary, generic_dictionary, string_io, simple_schema_content, simple_section_content):
    simple_section_content['sub']['abc'] = 10
    schema_section = SchemaSection(dictionary=generic_dictionary, init=simple_schema_content)
    section = Section(dictionary=dictionary, init=simple_section_content)
    validation = schema_section.validate(section)
    assert isinstance(validation, Validation)
    assert validation
    assert len(validation) == 1
    assert len(validation['sub']) == 1
    assert isinstance(validation['sub']['abc'], UnexpectedOptionError)
Exemplo n.º 15
0
def test_Section_2(content):
    section = Section(dictionary=FlatMap(collections.OrderedDict()), init=content)
    section2 = Section(dictionary=FlatMap(collections.OrderedDict()), init=content)
    s_io = string_io()
    section.dump(stream=s_io)
    assert s_io.getvalue() == SECTION_DUMP
    section['sub']['y']['yfilename'] = 'y.dat'
    s_io1 = string_io()
    section.dump(stream=s_io1)
    assert s_io1.getvalue() != SECTION_DUMP
    s_io2 = string_io()
    section2.dump(stream=s_io2)
    assert s_io2.getvalue() == SECTION_DUMP
Exemplo n.º 16
0
def test_SchemaSection_validate_unexpected_sub_removed(dictionary, generic_dictionary, string_io, simple_schema_content, simple_section_content):
    simple_section_content['sub']['ssub'] = {'abc': 10}
    schema_section = SchemaSection(dictionary=generic_dictionary, init=simple_schema_content, unexpected_option_validator=Remove())
    section = Section(dictionary=dictionary, init=simple_section_content)
    assert section['sub'].has_section('ssub')
    assert section['sub']['ssub'].has_option('abc')
    assert section['sub']['ssub']['abc'] == 10
    validation = schema_section.validate(section)
    assert not validation
    assert section['sub'].has_section('ssub')
    assert not section['sub']['ssub'].has_option('abc')
Exemplo n.º 17
0
def test_SchemaSection_validate_invalid_option(dictionary, generic_dictionary, string_io, simple_schema_content, simple_section_content):
    simple_section_content['sub']['subsub']['ssx'] = "delta"
    schema_section = SchemaSection(dictionary=generic_dictionary, init=simple_schema_content)
    section = Section(dictionary=dictionary, init=simple_section_content)
    validation = schema_section.validate(section)
    assert isinstance(validation, Validation)
    assert validation
    assert len(validation) == 1
    assert len(validation['sub']) == 1
    assert len(validation['sub']['subsub']) == 1
    assert isinstance(validation['sub']['subsub']['ssx'], InvalidChoiceError)
Exemplo n.º 18
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.º 19
0
def test_Section_update1():
    upd1 = {"a": 1}
    flatmap = Section(dictionary=collections.OrderedDict())
    flatmap.update(upd1)
    assert flatmap == upd1
Exemplo n.º 20
0
def test_setdefault():
    section = Section()
    val = {}
    inserted_val = section.setdefault("alpha", val)
    assert val is not inserted_val
    assert isinstance(inserted_val, Section)
Exemplo n.º 21
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.º 22
0
def test_SchemaSection_validate_0(dictionary, generic_dictionary, string_io, simple_schema_content, simple_section_content):
    schema_section = SchemaSection(dictionary=generic_dictionary, init=simple_schema_content)
    section = Section(dictionary=dictionary, init=simple_section_content)
    validation = schema_section.validate(section)
    assert isinstance(validation, Validation)
    assert not validation
Exemplo n.º 23
0
def test_Section_ne_0(simple_section):
    s0 = Section(init=simple_section)
    simple_section['options']['f'] = {'a': 1}
    simple_section.dump()
    assert s0 != simple_section
    assert simple_section != s0
Exemplo n.º 24
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.º 25
0
def simple_section(dictionary, simple_config_content):
    section = Section(dictionary=dictionary, init=simple_config_content)
    return section
Exemplo n.º 26
0
def _section_tpl_1():
    section = Section()
    section['b'] = {}
    tpl = ()
    return section, tpl
Exemplo n.º 27
0
def test_Section_eq_fast(simple_section):
    assert simple_section == Section(dictionary=simple_section.dictionary)
Exemplo n.º 28
0
                                'subsubsub': {
                                    'sss': 4
                                },
                                'ssb': 5
                            },
                            'sb': 6
                        },
                        b=7)
    assert config.has_section('sub')
    assert config['sub'].has_section('subsub')
    assert config['sub']['subsub'].has_section('subsubsub')
    assert not config['sub']['subsub'].has_section('empty1')
    assert not config['sub']['subsub'].has_section('empty2')


@pytest.fixture(params=[collections.OrderedDict(), Section(), Config()])
def extdefaults(request):
    defaults = request.param
    defaults['i10'] = 10
    defaults['sub'] = collections.OrderedDict()
    defaults['sub']['f11'] = 1.1
    defaults['sub']['f22'] = 2.2
    defaults['sub']['sse'] = collections.OrderedDict()
    defaults['sub']['ssf'] = collections.OrderedDict()
    defaults['sub']['ssf']['f44'] = 4.4
    defaults['xdat'] = "x.dat"
    return defaults


def test_Config_defaults_external(extdefaults):
    edcopy = as_dict(extdefaults, depth=-1, dict_class=dict)
Exemplo n.º 29
0
def test_Section_invalid_list_content():
    section = Section()
    with pytest.raises(TypeError) as exc_info:
        section['a'] = (1, 23, {})
    assert str(exc_info.value
               ) == "option a: invalid tuple: item #2 {} has invalid type dict"
Exemplo n.º 30
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.º 31
0
def test_Section_update2():
    upd1 = {'a': 1}
    flatmap = Section(dictionary=collections.OrderedDict())
    flatmap.update(upd1.items())
    assert flatmap == upd1
Exemplo n.º 32
0
def test_Section_update2():
    upd1 = {'a': 1}
    flatmap = Section(dictionary=collections.OrderedDict())
    flatmap.update(upd1.items())
    assert flatmap == upd1
Exemplo n.º 33
0
def test_Section_eq_slow(simple_section):
    s0 = Section(init=simple_section)
    assert s0 == simple_section
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_Section_create(content, string_io):
    section = Section(dictionary=FlatMap(collections.OrderedDict()),
                      init=content)
    section.dump(stream=string_io)
    assert string_io.getvalue() == SECTION_DUMP