def test_not_found(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" with pytest.raises(ToolsError): path_read(obj=path)
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 = path_write(obj=path, data=data) with pytest.raises(Exception): path_read(obj=path, is_json=True)
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_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(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_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_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 _read_csv(self, path, required): """Pass.""" path, content = path_read(obj=path) content = bom_strip(content=content) reader = csv.DictReader(io.StringIO(content)) rows = [x for x in reader] if not rows: self.spew_error("No rows supplied in CSV!") columns = listify(reader.fieldnames) self.spew_debug(f"CSV file {path} has columns {columns}") missing = [x for x in required if x not in columns] if missing: missing = ", ".join(missing) self.spew_error( f"Missing required columns {missing} in CSV file {path}") return columns, rows