From 17c0a84c3896bcaed873fc92dce8582a8627f94f Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sat, 19 Oct 2024 08:38:32 +0800 Subject: [PATCH] Add two tests for the deprecated Session.open_virtual_file and Session.virtualfile_from_data methods (#3527) --- pygmt/clib/session.py | 2 +- pygmt/tests/test_clib_virtualfile_in.py | 30 +++++++++++++++++++++++++ pygmt/tests/test_clib_virtualfiles.py | 30 +++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index 043f2715620..5d3c7f9406b 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -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." ) diff --git a/pygmt/tests/test_clib_virtualfile_in.py b/pygmt/tests/test_clib_virtualfile_in.py index d854d556050..aac8e4af772 100644 --- a/pygmt/tests/test_clib_virtualfile_in.py +++ b/pygmt/tests/test_clib_virtualfile_in.py @@ -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": N = {shape[0]}\t{bounds}\n" + assert output == expected diff --git a/pygmt/tests/test_clib_virtualfiles.py b/pygmt/tests/test_clib_virtualfiles.py index f913d7e5472..7119691455b 100644 --- a/pygmt/tests/test_clib_virtualfiles.py +++ b/pygmt/tests/test_clib_virtualfiles.py @@ -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": N = {shape[0]}\t{bounds}\n" + assert output == expected