Skip to content

Commit

Permalink
pygmt.set_display: Improve the test by checking if the preview image …
Browse files Browse the repository at this point in the history
…is opened in the expected way (#3548)
  • Loading branch information
seisman authored Oct 31, 2024
1 parent e5ecee9 commit 82b0c73
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
25 changes: 13 additions & 12 deletions pygmt/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,27 +453,28 @@ def set_display(method: Literal["external", "notebook", "none", None] = None):
>>> import pygmt
>>> fig = pygmt.Figure()
>>> fig.basemap(region=[0, 10, 0, 10], projection="X10c/5c", frame=True)
>>> fig.show() # will display a PNG image in the current notebook
>>> fig.show() # Will display a PNG image in the current notebook
>>>
>>> # set the display method to "external"
>>> # Set the display method to "external"
>>> pygmt.set_display(method="external") # doctest: +SKIP
>>> fig.show() # will display a PDF image using the default PDF viewer
>>> fig.show() # Will display a PDF image using the default PDF viewer
>>>
>>> # set the display method to "none"
>>> # Set the display method to "none"
>>> pygmt.set_display(method="none")
>>> fig.show() # will not show any image
>>> fig.show() # Will not show any image
>>>
>>> # reset to the default display method
>>> # Reset to the default display method
>>> pygmt.set_display(method=None)
>>> fig.show() # again, will show a PNG image in the current notebook
>>> fig.show() # Again, will show a PNG image in the current notebook
"""
match method:
case "external" | "notebook" | "none":
SHOW_CONFIG["method"] = method # type: ignore[assignment]
SHOW_CONFIG["method"] = method
case None:
SHOW_CONFIG["method"] = _get_default_display_method() # type: ignore[assignment]
SHOW_CONFIG["method"] = _get_default_display_method()
case _:
raise GMTInvalidInput(
f"Invalid display method '{method}'. Valid values are 'external',"
"'notebook', 'none' or None."
msg = (
f"Invalid display method '{method}'. "
"Valid values are 'external', 'notebook', 'none' or None."
)
raise GMTInvalidInput(msg)
49 changes: 43 additions & 6 deletions pygmt/tests/test_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,50 @@ class TestSetDisplay:

def test_set_display(self):
"""
Test if pygmt.set_display updates the SHOW_CONFIG variable correctly.
Test if pygmt.set_display updates the SHOW_CONFIG variable correctly and
Figure.show opens the preview image in the correct way.
"""
default_method = SHOW_CONFIG["method"] # Current default method

for method in ("notebook", "external", "none"):
set_display(method=method)
assert SHOW_CONFIG["method"] == method
default_method = SHOW_CONFIG["method"] # Store the current default method.

fig = Figure()
fig.basemap(region=[0, 3, 6, 9], projection="X1c", frame=True)

# Test the "notebook" display method.
set_display(method="notebook")
assert SHOW_CONFIG["method"] == "notebook"
if _HAS_IPYTHON:
with (
patch("IPython.display.display") as mock_display,
patch("pygmt.figure.launch_external_viewer") as mock_viewer,
):
fig.show()
assert mock_viewer.call_count == 0
assert mock_display.call_count == 1
else:
with pytest.raises(GMTError):
fig.show()

# Test the "external" display method
set_display(method="external")
assert SHOW_CONFIG["method"] == "external"
with patch("pygmt.figure.launch_external_viewer") as mock_viewer:
fig.show()
assert mock_viewer.call_count == 1
if _HAS_IPYTHON:
with patch("IPython.display.display") as mock_display:
fig.show()
assert mock_display.call_count == 0

# Test the "none" display method.
set_display(method="none")
assert SHOW_CONFIG["method"] == "none"
with patch("pygmt.figure.launch_external_viewer") as mock_viewer:
fig.show()
assert mock_viewer.call_count == 0
if _HAS_IPYTHON:
with patch("IPython.display.display") as mock_display:
fig.show()
assert mock_display.call_count == 0

# Setting method to None should revert it to the default method.
set_display(method=None)
Expand Down

0 comments on commit 82b0c73

Please sign in to comment.