Skip to content

Commit 82b0c73

Browse files
authored
pygmt.set_display: Improve the test by checking if the preview image is opened in the expected way (#3548)
1 parent e5ecee9 commit 82b0c73

File tree

2 files changed

+56
-18
lines changed

2 files changed

+56
-18
lines changed

pygmt/figure.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -453,27 +453,28 @@ def set_display(method: Literal["external", "notebook", "none", None] = None):
453453
>>> import pygmt
454454
>>> fig = pygmt.Figure()
455455
>>> fig.basemap(region=[0, 10, 0, 10], projection="X10c/5c", frame=True)
456-
>>> fig.show() # will display a PNG image in the current notebook
456+
>>> fig.show() # Will display a PNG image in the current notebook
457457
>>>
458-
>>> # set the display method to "external"
458+
>>> # Set the display method to "external"
459459
>>> pygmt.set_display(method="external") # doctest: +SKIP
460-
>>> fig.show() # will display a PDF image using the default PDF viewer
460+
>>> fig.show() # Will display a PDF image using the default PDF viewer
461461
>>>
462-
>>> # set the display method to "none"
462+
>>> # Set the display method to "none"
463463
>>> pygmt.set_display(method="none")
464-
>>> fig.show() # will not show any image
464+
>>> fig.show() # Will not show any image
465465
>>>
466-
>>> # reset to the default display method
466+
>>> # Reset to the default display method
467467
>>> pygmt.set_display(method=None)
468-
>>> fig.show() # again, will show a PNG image in the current notebook
468+
>>> fig.show() # Again, will show a PNG image in the current notebook
469469
"""
470470
match method:
471471
case "external" | "notebook" | "none":
472-
SHOW_CONFIG["method"] = method # type: ignore[assignment]
472+
SHOW_CONFIG["method"] = method
473473
case None:
474-
SHOW_CONFIG["method"] = _get_default_display_method() # type: ignore[assignment]
474+
SHOW_CONFIG["method"] = _get_default_display_method()
475475
case _:
476-
raise GMTInvalidInput(
477-
f"Invalid display method '{method}'. Valid values are 'external',"
478-
"'notebook', 'none' or None."
476+
msg = (
477+
f"Invalid display method '{method}'. "
478+
"Valid values are 'external', 'notebook', 'none' or None."
479479
)
480+
raise GMTInvalidInput(msg)

pygmt/tests/test_figure.py

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,50 @@ class TestSetDisplay:
377377

378378
def test_set_display(self):
379379
"""
380-
Test if pygmt.set_display updates the SHOW_CONFIG variable correctly.
380+
Test if pygmt.set_display updates the SHOW_CONFIG variable correctly and
381+
Figure.show opens the preview image in the correct way.
381382
"""
382-
default_method = SHOW_CONFIG["method"] # Current default method
383-
384-
for method in ("notebook", "external", "none"):
385-
set_display(method=method)
386-
assert SHOW_CONFIG["method"] == method
383+
default_method = SHOW_CONFIG["method"] # Store the current default method.
384+
385+
fig = Figure()
386+
fig.basemap(region=[0, 3, 6, 9], projection="X1c", frame=True)
387+
388+
# Test the "notebook" display method.
389+
set_display(method="notebook")
390+
assert SHOW_CONFIG["method"] == "notebook"
391+
if _HAS_IPYTHON:
392+
with (
393+
patch("IPython.display.display") as mock_display,
394+
patch("pygmt.figure.launch_external_viewer") as mock_viewer,
395+
):
396+
fig.show()
397+
assert mock_viewer.call_count == 0
398+
assert mock_display.call_count == 1
399+
else:
400+
with pytest.raises(GMTError):
401+
fig.show()
402+
403+
# Test the "external" display method
404+
set_display(method="external")
405+
assert SHOW_CONFIG["method"] == "external"
406+
with patch("pygmt.figure.launch_external_viewer") as mock_viewer:
407+
fig.show()
408+
assert mock_viewer.call_count == 1
409+
if _HAS_IPYTHON:
410+
with patch("IPython.display.display") as mock_display:
411+
fig.show()
412+
assert mock_display.call_count == 0
413+
414+
# Test the "none" display method.
415+
set_display(method="none")
416+
assert SHOW_CONFIG["method"] == "none"
417+
with patch("pygmt.figure.launch_external_viewer") as mock_viewer:
418+
fig.show()
419+
assert mock_viewer.call_count == 0
420+
if _HAS_IPYTHON:
421+
with patch("IPython.display.display") as mock_display:
422+
fig.show()
423+
assert mock_display.call_count == 0
387424

388425
# Setting method to None should revert it to the default method.
389426
set_display(method=None)

0 commit comments

Comments
 (0)