def test_dump_to_file_descriptor(inifile, tmp_path): conf = load(inifile) file = tmp_path / "save.ini" with open(file, mode="w") as fh: dump(conf, fh) # a little weak but better than just testing that the file isn't empty new_body = file.read_text() for key, val in conf.items(): if isinstance(val, dict): assert f"[{key}]\n" in new_body
def test_idempotent_io(inifile): data0 = load(inifile) with tempfile.TemporaryDirectory() as tmpdir: save1 = Path(tmpdir) / "save1" save2 = Path(tmpdir) / "save2" dump(data0, save1) data1 = load(save1) dump(data1, save2) text1 = save1.read_text().split("\n") text2 = save2.read_text().split("\n") diff = "".join(difflib.context_diff(text1, text2)) assert not diff
def test_dump_to_file_path(inifile, tmp_path): conf = load(inifile) # pathlib.Path obj file1 = tmp_path / "save1.ini" dump(conf, file1) body1 = file1.read_text() # str file2 = tmp_path / "save2.ini" sfile2 = str(file2) dump(conf, sfile2) body2 = file2.read_text() assert body1 == body2 for key, val in conf.items(): if isinstance(val, dict): assert f"[{key}]\n" in body2
def test_dump_invalid_conf(invalid_conf, tmp_path): with pytest.raises(ValueError, match=r"^(Invalid schema)"): dump(invalid_conf, tmp_path / "save.ini")
def test_dump_wrong_mode(mode, expected_err, inifile, tmp_path): conf = load(inifile) save_file = str(tmp_path / "save.ini") with pytest.raises(expected_err): with open(save_file, mode=mode) as fh: dump(conf, fh)