def test_file_mode_creation(tmp_path): test_file = tmp_path / "test.roff" with open(test_file, "bw") as f: f.write(b"roff-bin\0tag\0a\0int\0b\0\0\0\0\0endtag\0") assert read(test_file) == {"a": {"b": 0}} with open(test_file, "w") as f: f.write("roff-asc tag a int σ 0 endtag") assert read(test_file) == {"a": {"σ": 0}}
def test_binary_write_read_is_ascii_write_read(roff_contents): bf = io.BytesIO() af = io.StringIO() roffio.write(bf, roff_contents, roff_format=roffio.Format.BINARY) roffio.write(af, roff_contents, roff_format=roffio.Format.ASCII) bf.seek(0) af.seek(0) read_binary_contents = roffio.read(bf) read_ascii_contents = roffio.read(af) read_binary_contents.pop("filedata") read_ascii_contents.pop("filedata") assert read_binary_contents == read_ascii_contents
def test_default_values(roff_grid): buff = io.BytesIO() roff_grid.to_file(buff) buff.seek(0) values = roffio.read(buff) del values["translate"] del values["scale"] if "subgrids" in values: del values["subgrids"] del values["active"] buff2 = io.BytesIO() roffio.write(buff2, values) buff2.seek(0) roff_grid2 = RoffGrid.from_file(buff2) assert roff_grid2.xoffset == 0.0 assert roff_grid2.yoffset == 0.0 assert roff_grid2.zoffset == 0.0 assert roff_grid2.xscale == 1.0 assert roff_grid2.yscale == 1.0 assert roff_grid2.zscale == -1.0 assert roff_grid2.subgrids is None assert np.array_equal( roff_grid2.active, np.ones(roff_grid.nx * roff_grid.ny * roff_grid.nz, dtype=np.bool_), )
def test_read_write_warn_cast(): buff = io.BytesIO() contents = {"t": {"a": np.array([1, 2], dtype=np.int64)}} with pytest.warns(UserWarning, match="cast"): roffio.write(buff, contents) buff.seek(0) assert np.array_equal(roffio.read(buff)["t"]["a"], np.array([1, 2], dtype=np.int32))
def test_write_adds_metadata(): f = io.BytesIO() roffio.write(f, {}) f.seek(0) read_contents = roffio.read(f) assert read_contents["version"]["major"] == 2 assert read_contents["version"]["minor"] == 0 assert read_contents["filedata"]["byteswaptest"] == 1
def test_not_a_grid(roff_grid): buff = io.BytesIO() roff_grid.to_file(buff) buff.seek(0) values = roffio.read(buff) values["filedata"]["filetype"] = "notgrid" buff.seek(0) roffio.write(buff, values) buff.seek(0) with pytest.raises(ValueError, match="did not have filetype set to grid"): RoffGrid.from_file(buff)
def test_read_write_multitag(roff_format, buffer): contents = [ ("tagname", {"keyname": 1.0}), ("tagname", {"keyname": 2.0}), ] roffio.write(buffer, contents, roff_format=roff_format) buffer.seek(0) values = roffio.read(buffer) assert values["tagname"] == [{"keyname": 1.0}, {"keyname": 2.0}]
def test_read_write_list(roff_format, filelike): data = {"t": {"k": ["a", "b"]}} roffio.write(filelike, data, roff_format=roff_format) filelike.seek(0) read_contents = roffio.read(filelike) read_contents.pop("version") read_contents.pop("filedata") read_contents.pop("eof") read_contents["t"]["k"] = list(read_contents["t"]["k"]) assert read_contents == data
def test_read_write_multikey(roff_format, buffer): contents = { "tagname": [ ("keyname", 1.0), ("keyname", 2.0), ], } roffio.write(buffer, contents, roff_format=roff_format) buffer.seek(0) values = roffio.read(buffer) assert values["tagname"] == {"keyname": [1.0, 2.0]}
def test_read_write_is_identity(roff_data): f = io.BytesIO() roffio.write(f, roff_data) f.seek(0) read_contents = roffio.read(f) read_contents.pop("version") read_contents.pop("filedata") read_contents.pop("eof") roff_data.pop("version", None) roff_data.pop("filedata", None) roff_data.pop("eof", None) assert read_contents == roff_data
def test_read_write_filestr(tmpdir, roff_data): filepath = os.path.join(tmpdir, "data.roff") roffio.write(filepath, roff_data) read_contents = roffio.read(filepath) read_contents.pop("version") read_contents.pop("filedata") read_contents.pop("eof") roff_data.pop("version", None) roff_data.pop("filedata", None) roff_data.pop("eof", None) assert read_contents == roff_data
def test_read_write_pathlib(tmp_path, roff_data): filepath = tmp_path / "data.roff" roffio.write(filepath, roff_data) read_contents = roffio.read(filepath) read_contents.pop("version") read_contents.pop("filedata") read_contents.pop("eof") roff_data.pop("version", None) roff_data.pop("filedata", None) roff_data.pop("eof", None) assert read_contents == roff_data
def test_to_file_codes(): buff = io.BytesIO() roff_param = RoffParameter( 1, 1, 2, "a", b"\x01\xFF", code_names=["a", "b"], code_values=np.array([1, 2], dtype=np.int32), ) roff_param.to_file(buff) buff.seek(0) vals = roffio.read(buff) assert np.array_equal(vals["parameter"]["codeNames"], np.array(["a", "b"])) assert np.array_equal(vals["parameter"]["codeValues"], np.array([1, 2])) assert vals["dimensions"] == {"nX": 1, "nY": 1, "nZ": 2}
def test_just_one_eof(): f = io.BytesIO() roffio.write(f, {"eof": {}}) f.seek(0) assert roffio.read(f)["eof"] == {}
def test_overwrite_creation_date(): f = io.BytesIO() roffio.write(f, {"filedata": {"creationDate": "today"}}) f.seek(0) assert roffio.read(f)["filedata"]["creationDate"] == "today"
def test_overwrite_filetype(): f = io.BytesIO() roffio.write(f, {"filedata": {"filetype": "surface"}}) f.seek(0) assert roffio.read(f)["filedata"]["filetype"] == "surface"
def test_to_file(tmp_path): roff_param = RoffParameter(1, 1, 2, "", b"\x01\xFF") roff_param.to_file(tmp_path / "param.roff") vals = roffio.read(tmp_path / "param.roff") assert vals["parameter"] == {"name": "", "data": b"\x01\xff"} assert vals["dimensions"] == {"nX": 1, "nY": 1, "nZ": 2}