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
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
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
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
def _section_tpl_2(): section = Section() section['b'] = {} section['b']['bb'] = {} section['b']['bb']['bbb'] = {} tpl = () return section, tpl
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
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
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"
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
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
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
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)
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')
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)
def test_Section_create(generic_dictionary, string_io): section = Section(dictionary=generic_dictionary) section.dump(stream=string_io) assert string_io.getvalue() == ""
def test_Section_update1(): upd1 = {"a": 1} flatmap = Section(dictionary=collections.OrderedDict()) flatmap.update(upd1) assert flatmap == upd1
def test_setdefault(): section = Section() val = {} inserted_val = section.setdefault("alpha", val) assert val is not inserted_val assert isinstance(inserted_val, Section)
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
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
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
def simple_section(dictionary, simple_config_content): section = Section(dictionary=dictionary, init=simple_config_content) return section
def _section_tpl_1(): section = Section() section['b'] = {} tpl = () return section, tpl
def test_Section_eq_fast(simple_section): assert simple_section == Section(dictionary=simple_section.dictionary)
'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)
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"
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
def test_Section_update2(): upd1 = {'a': 1} flatmap = Section(dictionary=collections.OrderedDict()) flatmap.update(upd1.items()) assert flatmap == upd1
def test_Section_eq_slow(simple_section): s0 = Section(init=simple_section) assert s0 == simple_section