def test_read_write(fmt, parameters, tmp_text_file): config_class = parameters.config_class config_content = parameters.config_content cs = config_class(config_content) # print("::: filename:", tmp_text_file.name) print(":::", config_class) cs.dump() cs.write(tmp_text_file.name, fmt) # with open(tmp_text_file.name, "r") as f_in: # if fmt == "toml": # print("::: ---begin") # print(f_in.read() + '\n') # print("::: ---end") cs2 = config_class() cs2.read(tmp_text_file.name, fmt) print("::: reloaded:") cs2.dump() if config_class == Validation: tcs = transform(cs, value_transform=str, dict_class=dict) tcs2 = transform(cs2, value_transform=str, dict_class=dict) print("===", type(tcs), type(tcs2)) assert tcs == tcs2 else: assert compare_dicts(cs, cs2)
def test_IoManager_write(io_manager, filetype_obj, tmpdir, overwrite, exists, with_filetype): filetype, obj = filetype_obj tmpfile = tmpdir.join("read", filetype.filepath).strpath if exists: if not os.path.isdir(os.path.dirname(tmpfile)): os.makedirs(os.path.dirname(tmpfile)) with open(tmpfile, "w") as f_out: f_out.write("\n") else: if os.path.exists(tmpfile): os.remove(tmpfile) filetype = filetype._replace(filepath=tmpfile) if with_filetype: if exists and not overwrite: with pytest.raises(SystemExit): io_manager.write_obj(obj, filetype, overwrite=overwrite) else: io_manager.write_obj(obj, filetype, overwrite=overwrite) obj2 = filetype.config_class.from_file(filetype.filepath) # print("obj:") # obj.dump() # print("obj2:") # obj2.dump() dct = dictutils.transform(obj, dict_class=dict, value_transform=str) dct2 = dictutils.transform(obj2, dict_class=dict, value_transform=str) assert dct == dct2 else: filetype = filetype._replace(filepath=None) io_manager.printer.stream.truncate() io_manager.write_obj(obj, filetype, overwrite=overwrite) output = "\n".join( "print:{}".format(line) for line in obj.to_string(fmt=filetype.fmt).split("\n")) + '\n' assert io_manager.printer.stream.getvalue() == output
def test_IoManager_read(io_manager, filetype_obj, tmpdir): filetype, obj = filetype_obj tmpfile = tmpdir.join("read", filetype.filepath).strpath filetype = filetype._replace(filepath=tmpfile) obj.to_file(filetype.filepath) obj2 = filetype.config_class() io_manager.read_obj(obj2, filetype) # print("obj:") # obj.dump() # print("obj2:") # obj2.dump() dct = dictutils.transform(obj, dict_class=dict, value_transform=str) dct2 = dictutils.transform(obj2, dict_class=dict, value_transform=str) assert dct == dct2
def test_transform_0(): dct0 = {'a': 10, 'b': collections.OrderedDict([('x', 1), ('z', 3)]), 'c': 3} dctr = dictutils.transform(dct0, value_transform=str, key_transform=lambda x: '_' + x) assert type(dct0) is type(dctr) assert type(dct0['b']) is type(dctr['_b']) assert str(dct0['a']) == dctr['_a'] assert str(dct0['b']['x']) == dctr['_b']['_x']
def test_read_write(protocol, parameters, tmp_text_file): config_class = parameters.config_class config_content = parameters.config_content cs = config_class(config_content) print(":::", config_class) cs.dump() cs.write(tmp_text_file.name, protocol) cs2 = config_class() cs2.read(tmp_text_file.name, protocol) print("::: reloaded:") cs2.dump() if config_class == Validation: tcs = transform(cs, value_transform=str, dict_class=dict) tcs2 = transform(cs2, value_transform=str, dict_class=dict) print("===", type(tcs), type(tcs2)) assert tcs == tcs2 else: assert compare_dicts(cs, cs2)
def test_transform_none(): dct0 = {'a': 10, 'b': 20} dct1 = dictutils.transform(dct0) assert dct0 == dct1