From 85f3e43fe5f40b151e14726fff03e67aba397c3f Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Fri, 17 May 2024 18:51:46 +0800 Subject: [PATCH] Refactor tests to pass list of arguments to the Session.call_module method --- pygmt/tests/test_clib.py | 12 ++++++------ pygmt/tests/test_clib_virtualfiles.py | 20 ++++++++++---------- pygmt/tests/test_datatypes_dataset.py | 2 +- pygmt/tests/test_session_management.py | 6 +++--- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/pygmt/tests/test_clib.py b/pygmt/tests/test_clib.py index bc3d294b96b..8f7c10c8728 100644 --- a/pygmt/tests/test_clib.py +++ b/pygmt/tests/test_clib.py @@ -181,7 +181,7 @@ def test_call_module_invalid_arguments(): """ with clib.Session() as lib: with pytest.raises(GMTCLibError): - lib.call_module("info", "bogus-data.bla") + lib.call_module("info", ["bogus-data.bla"]) def test_call_module_invalid_name(): @@ -190,7 +190,7 @@ def test_call_module_invalid_name(): """ with clib.Session() as lib: with pytest.raises(GMTCLibError): - lib.call_module("meh", "") + lib.call_module("meh", []) def test_call_module_error_message(): @@ -199,7 +199,7 @@ def test_call_module_error_message(): """ with clib.Session() as lib: with pytest.raises(GMTCLibError) as exc_info: - lib.call_module("info", "bogus-data.bla") + lib.call_module("info", ["bogus-data.bla"]) assert "Module 'info' failed with status code" in exc_info.value.args[0] assert ( "gmtinfo [ERROR]: Cannot find file bogus-data.bla" in exc_info.value.args[0] @@ -213,7 +213,7 @@ def test_method_no_session(): # Create an instance of Session without "with" so no session is created. lib = clib.Session() with pytest.raises(GMTCLibNoSessionError): - lib.call_module("gmtdefaults", "") + lib.call_module("gmtdefaults", []) with pytest.raises(GMTCLibNoSessionError): _ = lib.session_pointer @@ -385,14 +385,14 @@ def test_extract_region_two_figures(): # Activate the first figure and extract the region from it # Use in a different session to avoid any memory problems. with clib.Session() as lib: - lib.call_module("figure", f"{fig1._name} -") + lib.call_module("figure", [fig1._name, "-"]) with clib.Session() as lib: wesn1 = lib.extract_region() npt.assert_allclose(wesn1, region1) # Now try it with the second one with clib.Session() as lib: - lib.call_module("figure", f"{fig2._name} -") + lib.call_module("figure", [fig2._name, "-"]) with clib.Session() as lib: wesn2 = lib.extract_region() npt.assert_allclose(wesn2, np.array([-165.0, -150.0, 15.0, 25.0])) diff --git a/pygmt/tests/test_clib_virtualfiles.py b/pygmt/tests/test_clib_virtualfiles.py index 26ebfc5d379..b8b5ee0500d 100644 --- a/pygmt/tests/test_clib_virtualfiles.py +++ b/pygmt/tests/test_clib_virtualfiles.py @@ -69,7 +69,7 @@ def test_virtual_file(dtypes): vfargs = (family, geometry, "GMT_IN|GMT_IS_REFERENCE", dataset) with lib.open_virtualfile(*vfargs) as vfile: with GMTTempFile() as outfile: - lib.call_module("info", f"{vfile} ->{outfile.name}") + 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" @@ -144,7 +144,7 @@ def test_virtualfile_in_required_z_matrix(array_func, kind): data=data, required_z=True, check_kind="vector" ) as vfile: with GMTTempFile() as outfile: - lib.call_module("info", f"{vfile} ->{outfile.name}") + lib.call_module("info", [vfile, f"->{outfile.name}"]) output = outfile.read(keep_tabs=True) bounds = "\t".join( [ @@ -217,7 +217,7 @@ def test_virtualfile_from_vectors(dtypes): with clib.Session() as lib: with lib.virtualfile_from_vectors(x, y, z) as vfile: with GMTTempFile() as outfile: - lib.call_module("info", f"{vfile} ->{outfile.name}") + 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 (x, y, z)]) expected = f": N = {size}\t{bounds}\n" @@ -237,7 +237,7 @@ def test_virtualfile_from_vectors_one_string_or_object_column(dtype): with clib.Session() as lib: with lib.virtualfile_from_vectors(x, y, strings) as vfile: with GMTTempFile() as outfile: - lib.call_module("convert", f"{vfile} ->{outfile.name}") + lib.call_module("convert", [vfile, f"->{outfile.name}"]) output = outfile.read(keep_tabs=True) expected = "".join( f"{i}\t{j}\t{k}\n" for i, j, k in zip(x, y, strings, strict=True) @@ -259,7 +259,7 @@ def test_virtualfile_from_vectors_two_string_or_object_columns(dtype): with clib.Session() as lib: with lib.virtualfile_from_vectors(x, y, strings1, strings2) as vfile: with GMTTempFile() as outfile: - lib.call_module("convert", f"{vfile} ->{outfile.name}") + lib.call_module("convert", [vfile, f"->{outfile.name}"]) output = outfile.read(keep_tabs=True) expected = "".join( f"{h}\t{i}\t{j} {k}\n" @@ -278,7 +278,7 @@ def test_virtualfile_from_vectors_transpose(dtypes): with clib.Session() as lib: with lib.virtualfile_from_vectors(*data.T) as vfile: with GMTTempFile() as outfile: - lib.call_module("info", f"{vfile} -C ->{outfile.name}") + lib.call_module("info", [vfile, "-C", f"->{outfile.name}"]) output = outfile.read(keep_tabs=True) bounds = "\t".join([f"{col.min():.0f}\t{col.max():.0f}" for col in data.T]) expected = f"{bounds}\n" @@ -308,7 +308,7 @@ def test_virtualfile_from_matrix(dtypes): with clib.Session() as lib: with lib.virtualfile_from_matrix(data) as vfile: with GMTTempFile() as outfile: - lib.call_module("info", f"{vfile} ->{outfile.name}") + 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" @@ -328,7 +328,7 @@ def test_virtualfile_from_matrix_slice(dtypes): with clib.Session() as lib: with lib.virtualfile_from_matrix(data) as vfile: with GMTTempFile() as outfile: - lib.call_module("info", f"{vfile} ->{outfile.name}") + 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 = {rows}\t{bounds}\n" @@ -354,7 +354,7 @@ def test_virtualfile_from_vectors_pandas(dtypes_pandas): with clib.Session() as lib: with lib.virtualfile_from_vectors(data.x, data.y, data.z) as vfile: with GMTTempFile() as outfile: - lib.call_module("info", f"{vfile} ->{outfile.name}") + 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 (data.x, data.y, data.z)] @@ -374,7 +374,7 @@ def test_virtualfile_from_vectors_arraylike(): with clib.Session() as lib: with lib.virtualfile_from_vectors(x, y, z) as vfile: with GMTTempFile() as outfile: - lib.call_module("info", f"{vfile} ->{outfile.name}") + lib.call_module("info", [vfile, f"->{outfile.name}"]) output = outfile.read(keep_tabs=True) bounds = "\t".join([f"<{min(i):.0f}/{max(i):.0f}>" for i in (x, y, z)]) expected = f": N = {size}\t{bounds}\n" diff --git a/pygmt/tests/test_datatypes_dataset.py b/pygmt/tests/test_datatypes_dataset.py index dd7e4073852..aa261c74a62 100644 --- a/pygmt/tests/test_datatypes_dataset.py +++ b/pygmt/tests/test_datatypes_dataset.py @@ -46,7 +46,7 @@ def dataframe_from_gmt(fname, **kwargs): """ with Session() as lib: with lib.virtualfile_out(kind="dataset") as vouttbl: - lib.call_module("read", f"{fname} {vouttbl} -Td") + lib.call_module("read", [fname, vouttbl, "-Td"]) df = lib.virtualfile_to_dataset(vfname=vouttbl, **kwargs) return df diff --git a/pygmt/tests/test_session_management.py b/pygmt/tests/test_session_management.py index 77a3970787f..d949f1a51c0 100644 --- a/pygmt/tests/test_session_management.py +++ b/pygmt/tests/test_session_management.py @@ -21,7 +21,7 @@ def test_begin_end(): end() # Kill the global session begin() with Session() as lib: - lib.call_module("basemap", "-R10/70/-3/8 -JX4i/3i -Ba") + lib.call_module("basemap", ["-R10/70/-3/8", "-JX4i/3i", "-Ba"]) end() begin() # Restart the global session assert Path("pygmt-session.pdf").exists() @@ -39,10 +39,10 @@ def test_gmt_compat_6_is_applied(capsys): # Generate a gmt.conf file in the current directory # with GMT_COMPATIBILITY = 5 with Session() as lib: - lib.call_module("gmtset", "GMT_COMPATIBILITY 5") + lib.call_module("gmtset", ["GMT_COMPATIBILITY=5"]) begin() with Session() as lib: - lib.call_module("basemap", "-R10/70/-3/8 -JX4i/3i -Ba") + lib.call_module("basemap", ["-R10/70/-3/8", "-JX4i/3i", "-Ba"]) out, err = capsys.readouterr() # capture stdout and stderr assert out == "" assert err != (