def test_parent_fail(self, tmp_path): """Test simple write with pathlib object.""" sub1 = tmp_path / "sub1" sub2 = sub1 / "sub2" path = sub2 / "file.txt" data = "abc\n123\n" with pytest.raises(exceptions.ToolsError): tools.path_write(obj=path, data=data, make_parent=False)
def test_is_json_true_nonjson(self, tmp_path): """Test is_json=True with .json not in filename and invalid json data.""" sub1 = tmp_path / "sub1" sub2 = sub1 / "sub2" path = sub2 / "file.text" data = pytest with pytest.raises(Exception): path_write(obj=path, data=data, is_json=True)
def test_overwrite_true(self, tmp_path): """Test overwrite=True.""" sub1 = tmp_path / "sub1" sub2 = sub1 / "sub2" path = sub2 / "file.txt" data = "abc\n123\n" path_write(obj=path, data=data) path_write(obj=path, data=data, overwrite=True) assert path.is_file()
def test_overwrite_false(self, tmp_path): """Test overwrite=False.""" sub1 = tmp_path / "sub1" sub2 = sub1 / "sub2" path = sub2 / "file.txt" data = "abc\n123\n" path_write(obj=path, data=data) with pytest.raises(ToolsError): path_write(obj=path, data=data, overwrite=False)
def test_noperm_parent(self): """Pass.""" tmpdir = tools.pathlib.Path(tempfile.gettempdir()) path = tmpdir / "file.txt" data = "abc\n123\n" ret_path, ret_write = tools.path_write(obj=path, data=data, overwrite=True) assert ret_path.read_text() == data
def test_is_json_true_json(self, tmp_path): """Test is_json=True with .json not in filename and valid json data.""" sub1 = tmp_path / "sub1" sub2 = sub1 / "sub2" path = sub2 / "file.text" data = {"x": 2} ret_path, ret_write = path_write(obj=path, data=data, is_json=True) assert ret_path.read_text() == '{\n "x": 2\n}'
def test_is_json_false_dotjson_json(self, tmp_path): """Test is_json=False with .json in filename and valid json data.""" sub1 = tmp_path / "sub1" sub2 = sub1 / "sub2" path = sub2 / "file.json" data = {"x": 2} ret_path, ret_write = tools.path_write(obj=path, data=data, is_json=False) assert ret_path.read_text() == '{\n "x": 2\n}'
def test_is_json_false_dotjson_nonjson(self, tmp_path): """Test is_json=False with .json in filename and invalid json data.""" sub1 = tmp_path / "sub1" sub2 = sub1 / "sub2" path = sub2 / "file.json" data = b"" ret_path, ret_write = path_write(obj=path, data=data, is_json=False) assert ret_path.read_text() == data.decode()
def test_is_json_true_nonjson(self, tmp_path): """Test is_json=True with .json not in filename and invalid json data.""" sub1 = tmp_path / "sub1" sub2 = sub1 / "sub2" path = sub2 / "file.text" data = "abc\n123\n" wret_path, ret_write = tools.path_write(obj=path, data=data) with pytest.raises(Exception): tools.path_read(obj=path, is_json=True)
def test_binary_false_binary(self, tmp_path): """Test binary=False with binary data.""" sub1 = tmp_path / "sub1" sub2 = sub1 / "sub2" path = sub2 / "file.txt" data = b"abc\n123\n" ret_path, ret_write = tools.path_write(obj=path, data=data, binary=False) assert ret_path.read_text() == data.decode() assert ret_path.read_bytes() == data
def test_binary_true_nonbinary(self, tmp_path): """Test binary=True with nonbinary data.""" sub1 = tmp_path / "sub1" sub2 = sub1 / "sub2" path = sub2 / "file.txt" data = "abc\n123\n" ret_path, ret_write = tools.path_write(obj=path, data=data, binary=True) assert ret_path.read_text() == data assert ret_path.read_bytes() == data.encode()
def test_simple_str(self, tmp_path): """Test simple write with path as str.""" sub1 = tmp_path / "sub1" sub2 = sub1 / "sub2" path = sub2 / "file.txt" data = "abc\n123\n" ret_path, ret_write = path_write(obj=format(path), data=data) assert ret_path.read_text() == data assert format(ret_path) == format(path) assert ret_write == len(data)
def test_is_json_false_dotjson_nonjson(self, tmp_path): """Test is_json=False with .json in filename and invalid json data.""" sub1 = tmp_path / "sub1" sub2 = sub1 / "sub2" path = sub2 / "file.json" data = "abc\n123\n" wret_path, ret_write = tools.path_write(obj=path, data=data) rret_path, ret_read = tools.path_read(obj=path, is_json=False) assert wret_path == rret_path assert ret_read == data
def test_binary_true(self, tmp_path): """Test binary=True.""" sub1 = tmp_path / "sub1" sub2 = sub1 / "sub2" path = sub2 / "file.txt" data = "abc\n123\n" wret_path, ret_write = tools.path_write(obj=path, data=data) rret_path, ret_read = tools.path_read(obj=format(path), binary=True) assert wret_path == rret_path assert ret_read == data.encode()
def test_pathlib(self, tmp_path): """Test simple write with pathlib object.""" sub1 = tmp_path / "sub1" sub2 = sub1 / "sub2" path = sub2 / "file.txt" data = "abc\n123\n" wret_path, ret_write = tools.path_write(obj=path, data=data) rret_path, ret_read = tools.path_read(obj=path) assert wret_path == rret_path assert ret_read == data
def test_is_json_false_dotjson_json(self, tmp_path): """Test is_json=False with .json in filename and valid json data.""" sub1 = tmp_path / "sub1" sub2 = sub1 / "sub2" path = sub2 / "file.json" data = {"x": 2} wret_path, ret_write = path_write(obj=path, data=data) rret_path, ret_read = path_read(obj=path, is_json=False) assert wret_path == rret_path assert ret_read == data
def test_binary_false(self, tmp_path): """Test binary=False.""" sub1 = tmp_path / "sub1" sub2 = sub1 / "sub2" path = sub2 / "file.txt" data = "abc\n123\n" wret_path, ret_write = path_write(obj=path, data=data) rret_path, ret_read = path_read(obj=format(path), binary=False) assert wret_path == rret_path assert ret_read == data
def test_str(self, tmp_path): """Test simple write with str.""" sub1 = tmp_path / "sub1" sub2 = sub1 / "sub2" path = sub2 / "file.txt" data = "abc\n123\n" wret_path, ret_write = path_write(obj=path, data=data) rret_path, ret_read = path_read(obj=format(path)) assert wret_path == rret_path assert ret_read == data
def test_binary_false_binary(self, tmp_path): """Test binary=False with binary data.""" sub1 = tmp_path / "sub1" sub2 = sub1 / "sub2" path = sub2 / "file.txt" data = b"abc\n123\n" ret_path, ret_write = path_write(obj=path, data=data, binary=False) assert ret_path.read_text() == "abc\n123\n" if IS_WINDOWS: assert ret_path.read_bytes() == b"abc\r\n123\r\n" else: assert ret_path.read_bytes() == b"abc\n123\n"
def test_binary_true(self, tmp_path): """Test binary=True.""" sub1 = tmp_path / "sub1" sub2 = sub1 / "sub2" path = sub2 / "file.txt" data = "abc\n123\n" wret_path, ret_write = path_write(obj=path, data=data) rret_path, ret_read = path_read(obj=format(path), binary=True) assert wret_path == rret_path if IS_WINDOWS: assert ret_read == b"abc\r\n123\r\n" else: assert ret_read == b"abc\n123\n"
def test_simple_pathlib(self, tmp_path): """Test simple write with pathlib object.""" sub1 = tmp_path / "sub1" sub2 = sub1 / "sub2" path = sub2 / "file.txt" data = "abc\n123\n" ret_path, ret_write = tools.path_write(obj=path, data=data) assert ret_path.read_text() == data assert format(ret_path) == format(path) assert ret_write == len(data) # FUTURE: not the same octal on windows assert ret_path.stat().st_mode == 33152 assert ret_path.parent.stat().st_mode == 16832
def test_simple_pathlib(self, tmp_path): """Test simple write with pathlib object.""" sub1 = tmp_path / "sub1" sub2 = sub1 / "sub2" path = sub2 / "file.txt" data = "abc\n123\n" ret_path, ret_write = path_write(obj=path, data=data) assert ret_path.read_text() == data assert format(ret_path) == format(path) assert ret_write == len(data) if IS_WINDOWS: assert ret_path.stat().st_mode == 33206 assert ret_path.parent.stat().st_mode == 16895 else: assert ret_path.stat().st_mode == 33152 assert ret_path.parent.stat().st_mode == 16832