Skip to content

Commit

Permalink
Add two tests for the deprecated Session.open_virtual_file and Sessio…
Browse files Browse the repository at this point in the history
…n.virtualfile_from_data methods (#3527)
  • Loading branch information
seisman authored Oct 19, 2024
1 parent dfecaa5 commit 17c0a84
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -1852,7 +1852,7 @@ def virtualfile_from_data(
instead.
"""
msg = (
"API function 'Session.virtualfile_from_datae()' has been deprecated since "
"API function 'Session.virtualfile_from_data()' has been deprecated since "
"v0.13.0 and will be removed in v0.15.0. Use 'Session.virtualfile_in()' "
"instead."
)
Expand Down
30 changes: 30 additions & 0 deletions pygmt/tests/test_clib_virtualfile_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,33 @@ def test_virtualfile_in_matrix_string_dtype():
assert output == "347.5 348.5 -30.5 -30\n"
# Should check that lib.virtualfile_from_vectors is called once,
# not lib.virtualfile_from_matrix, but it's technically complicated.


def test_virtualfile_from_data():
"""
Test the backwards compatibility of the virtualfile_from_data method.
This test is the same as test_virtualfile_in_required_z_matrix, but using the
deprecated method.
"""
shape = (5, 3)
dataframe = pd.DataFrame(
data=np.arange(shape[0] * shape[1]).reshape(shape), columns=["x", "y", "z"]
)
data = np.array(dataframe)
with clib.Session() as lib:
with pytest.warns(FutureWarning, match="virtualfile_from_data"):
with lib.virtualfile_from_data(
data=data, required_z=True, check_kind="vector"
) as vfile:
with GMTTempFile() as outfile:
lib.call_module("info", [vfile, f"->{outfile.name}"])
output = outfile.read(keep_tabs=True)
bounds = "\t".join(
[
f"<{i.min():.0f}/{i.max():.0f}>"
for i in (dataframe.x, dataframe.y, dataframe.z)
]
)
expected = f"<matrix memory>: N = {shape[0]}\t{bounds}\n"
assert output == expected
30 changes: 30 additions & 0 deletions pygmt/tests/test_clib_virtualfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,33 @@ def test_open_virtualfile_bad_direction():
with pytest.raises(GMTInvalidInput):
with lib.open_virtualfile(*vfargs):
pass


def test_open_virtual_file():
"""
Test the deprecated Session.open_virtual_file method.
This test is the same as test_open_virtualfile, but using the deprecated method.
"""
shape = (5, 3)
with clib.Session() as lib:
family = "GMT_IS_DATASET|GMT_VIA_MATRIX"
geometry = "GMT_IS_POINT"
dataset = lib.create_data(
family=family,
geometry=geometry,
mode="GMT_CONTAINER_ONLY",
dim=[shape[1], shape[0], 1, 0], # columns, rows, layers, dtype
)
data = np.arange(shape[0] * shape[1]).reshape(shape)
lib.put_matrix(dataset, matrix=data)
# Add the dataset to a virtual file and pass it along to gmt info
with pytest.warns(FutureWarning, match="open_virtual_file"):
vfargs = (family, geometry, "GMT_IN|GMT_IS_REFERENCE", dataset)
with lib.open_virtual_file(*vfargs) as vfile:
with GMTTempFile() as outfile:
lib.call_module("info", [vfile, f"->{outfile.name}"])
output = outfile.read(keep_tabs=True)
bounds = "\t".join([f"<{col.min():.0f}/{col.max():.0f}>" for col in data.T])
expected = f"<matrix memory>: N = {shape[0]}\t{bounds}\n"
assert output == expected

0 comments on commit 17c0a84

Please sign in to comment.