|
4 | 4 | Doesn't include the plotting commands which have their own test files.
|
5 | 5 | """
|
6 | 6 |
|
7 |
| -import importlib |
8 | 7 | from pathlib import Path
|
9 | 8 |
|
10 | 9 | import numpy as np
|
11 | 10 | import numpy.testing as npt
|
12 | 11 | import pytest
|
13 | 12 | from pygmt import Figure, set_display
|
14 | 13 | from pygmt.exceptions import GMTError, GMTInvalidInput
|
| 14 | +from pygmt.figure import _get_default_display_method |
15 | 15 | from pygmt.helpers import GMTTempFile
|
16 | 16 |
|
17 |
| -HAS_IPYTHON = bool(importlib.util.find_spec("IPython")) |
| 17 | +try: |
| 18 | + import IPython |
| 19 | + |
| 20 | + _HAS_IPYTHON = True |
| 21 | +except ImportError: |
| 22 | + _HAS_IPYTHON = False |
18 | 23 |
|
19 | 24 |
|
20 | 25 | def test_figure_region():
|
@@ -307,7 +312,7 @@ def test_figure_savefig_worldfile():
|
307 | 312 | fig.savefig(fname=imgfile.name, worldfile=True)
|
308 | 313 |
|
309 | 314 |
|
310 |
| -@pytest.mark.skipif(not HAS_IPYTHON, reason="run when IPython is installed") |
| 315 | +@pytest.mark.skipif(not _HAS_IPYTHON, reason="run when IPython is installed") |
311 | 316 | def test_figure_show():
|
312 | 317 | """
|
313 | 318 | Test that show creates the correct file name and deletes the temp dir.
|
@@ -347,7 +352,7 @@ def test_figure_show_invalid_method():
|
347 | 352 | fig.show(method="test")
|
348 | 353 |
|
349 | 354 |
|
350 |
| -@pytest.mark.skipif(HAS_IPYTHON, reason="run without IPython installed") |
| 355 | +@pytest.mark.skipif(_HAS_IPYTHON, reason="run without IPython installed") |
351 | 356 | def test_figure_show_notebook_error_without_ipython():
|
352 | 357 | """
|
353 | 358 | Test to check if an error is raised when display method is 'notebook', but IPython
|
@@ -390,3 +395,48 @@ def test_figure_unsupported_xshift_yshift():
|
390 | 395 | fig.plot(x=1, y=1, style="c3c", yshift="3c")
|
391 | 396 | with pytest.raises(GMTInvalidInput):
|
392 | 397 | fig.plot(x=1, y=1, style="c3c", Y="3c")
|
| 398 | + |
| 399 | + |
| 400 | +class TestGetDefaultDisplayMethod: |
| 401 | + """ |
| 402 | + Test the _get_default_display_method function. |
| 403 | + """ |
| 404 | + |
| 405 | + def test_default_display_method(self, monkeypatch): |
| 406 | + """ |
| 407 | + Default display method is "external" if PYGMT_USE_EXTERNAL_DISPLAY is undefined. |
| 408 | + """ |
| 409 | + monkeypatch.delenv("PYGMT_USE_EXTERNAL_DISPLAY", raising=False) |
| 410 | + assert _get_default_display_method() == "external" |
| 411 | + |
| 412 | + def test_disable_external_display(self, monkeypatch): |
| 413 | + """ |
| 414 | + Setting PYGMT_USE_EXTERNAL_DISPLAY to "false" should disable external display. |
| 415 | + """ |
| 416 | + monkeypatch.setenv("PYGMT_USE_EXTERNAL_DISPLAY", "false") |
| 417 | + assert _get_default_display_method() == "none" |
| 418 | + |
| 419 | + @pytest.mark.skipif(not _HAS_IPYTHON, reason="Run when IPython is installed") |
| 420 | + def test_notebook_display(self, monkeypatch): |
| 421 | + """ |
| 422 | + Default display method is "notebook" when an IPython kernel is running. |
| 423 | + """ |
| 424 | + |
| 425 | + class MockIPython: |
| 426 | + """ |
| 427 | + A simple mock class to simulate an IPython instance. |
| 428 | + """ |
| 429 | + |
| 430 | + def __init__(self): |
| 431 | + self.config = {"IPKernelApp": True} |
| 432 | + |
| 433 | + # Mock IPython.get_ipython() to return a MockIPython instance. |
| 434 | + mock_ipython = MockIPython() |
| 435 | + monkeypatch.setattr(IPython, "get_ipython", lambda: mock_ipython) |
| 436 | + |
| 437 | + # Default display method should be "notebook" when an IPython kernel is running. |
| 438 | + assert _get_default_display_method() == "notebook" |
| 439 | + |
| 440 | + # PYGMT_USE_EXTERNAL_DISPLAY should not affect notebook display. |
| 441 | + monkeypatch.setenv("PYGMT_USE_EXTERNAL_DISPLAY", "false") |
| 442 | + assert _get_default_display_method() == "notebook" |
0 commit comments