From 541df3d372c45a18fbc24069e9e425e789f793eb Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sat, 19 Oct 2024 17:30:47 +0800 Subject: [PATCH 1/4] Simplify the _get_default_display_method tests using mock --- pygmt/tests/test_figure.py | 41 ++++++++++++++------------------------ 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/pygmt/tests/test_figure.py b/pygmt/tests/test_figure.py index 4c8e184d118..7b8613d15cb 100644 --- a/pygmt/tests/test_figure.py +++ b/pygmt/tests/test_figure.py @@ -4,7 +4,9 @@ Doesn't include the plotting commands which have their own test files. """ +import importlib from pathlib import Path +from unittest import mock import numpy as np import numpy.testing as npt @@ -14,12 +16,7 @@ from pygmt.figure import SHOW_CONFIG, _get_default_display_method from pygmt.helpers import GMTTempFile -try: - import IPython - - _HAS_IPYTHON = True -except ImportError: - _HAS_IPYTHON = False +_HAS_IPYTHON = bool(importlib.util.find_spec("IPython")) def test_figure_region(): @@ -436,26 +433,18 @@ def test_disable_external_display(self, monkeypatch): assert _get_default_display_method() == "none" @pytest.mark.skipif(not _HAS_IPYTHON, reason="Run when IPython is installed") - def test_notebook_display(self, monkeypatch): + def test_notebook_display(self): """ Default display method is "notebook" when an IPython kernel is running. """ - - class MockIPython: - """ - A simple mock class to simulate an IPython instance. - """ - - def __init__(self): - self.config = {"IPKernelApp": True} - - # Mock IPython.get_ipython() to return a MockIPython instance. - mock_ipython = MockIPython() - monkeypatch.setattr(IPython, "get_ipython", lambda: mock_ipython) - - # Default display method should be "notebook" when an IPython kernel is running. - assert _get_default_display_method() == "notebook" - - # PYGMT_USE_EXTERNAL_DISPLAY should not affect notebook display. - monkeypatch.setenv("PYGMT_USE_EXTERNAL_DISPLAY", "false") - assert _get_default_display_method() == "notebook" + # Mock IPython.get_ipython() to return an object with a config attribute, + # so PyGMT can detect that an IPython kernel is running. + with mock.patch( + "IPython.get_ipython", return_value=mock.Mock(config={"IPKernelApp": True}) + ): + # Display method should be "notebook" when an IPython kernel is running. + assert _get_default_display_method() == "notebook" + + # PYGMT_USE_EXTERNAL_DISPLAY should not affect notebook display. + with mock.patch.dict("os.environ", {"PYGMT_USE_EXTERNAL_DISPLAY": "false"}): + assert _get_default_display_method() == "notebook" From cc0632661ea81b4af38bab496cca6280b133a231 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sun, 20 Oct 2024 22:56:59 +0800 Subject: [PATCH 2/4] Use patch as a decorator instead --- pygmt/tests/test_figure.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/pygmt/tests/test_figure.py b/pygmt/tests/test_figure.py index 7b8613d15cb..8e35f42cf9b 100644 --- a/pygmt/tests/test_figure.py +++ b/pygmt/tests/test_figure.py @@ -6,7 +6,7 @@ import importlib from pathlib import Path -from unittest import mock +from unittest.mock import Mock, patch import numpy as np import numpy.testing as npt @@ -433,18 +433,16 @@ def test_disable_external_display(self, monkeypatch): assert _get_default_display_method() == "none" @pytest.mark.skipif(not _HAS_IPYTHON, reason="Run when IPython is installed") + # Mock IPython.get_ipython() to return an object with a config attribute, so PyGMT + # can detect that an IPython kernel is running. + @patch("IPython.get_ipython", return_value=Mock(config={"IPKernelApp": True})) def test_notebook_display(self): """ Default display method is "notebook" when an IPython kernel is running. """ - # Mock IPython.get_ipython() to return an object with a config attribute, - # so PyGMT can detect that an IPython kernel is running. - with mock.patch( - "IPython.get_ipython", return_value=mock.Mock(config={"IPKernelApp": True}) - ): - # Display method should be "notebook" when an IPython kernel is running. - assert _get_default_display_method() == "notebook" + # Display method should be "notebook" when an IPython kernel is running. + assert _get_default_display_method() == "notebook" - # PYGMT_USE_EXTERNAL_DISPLAY should not affect notebook display. - with mock.patch.dict("os.environ", {"PYGMT_USE_EXTERNAL_DISPLAY": "false"}): - assert _get_default_display_method() == "notebook" + # PYGMT_USE_EXTERNAL_DISPLAY should not affect notebook display. + with patch.dict("os.environ", {"PYGMT_USE_EXTERNAL_DISPLAY": "false"}): + assert _get_default_display_method() == "notebook" From d08001c6e46a0a217a1e40f08e19ad2a37225249 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 21 Oct 2024 08:57:32 +0800 Subject: [PATCH 3/4] Revert "Use patch as a decorator instead" This reverts commit cc0632661ea81b4af38bab496cca6280b133a231. --- pygmt/tests/test_figure.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/pygmt/tests/test_figure.py b/pygmt/tests/test_figure.py index 8e35f42cf9b..7b8613d15cb 100644 --- a/pygmt/tests/test_figure.py +++ b/pygmt/tests/test_figure.py @@ -6,7 +6,7 @@ import importlib from pathlib import Path -from unittest.mock import Mock, patch +from unittest import mock import numpy as np import numpy.testing as npt @@ -433,16 +433,18 @@ def test_disable_external_display(self, monkeypatch): assert _get_default_display_method() == "none" @pytest.mark.skipif(not _HAS_IPYTHON, reason="Run when IPython is installed") - # Mock IPython.get_ipython() to return an object with a config attribute, so PyGMT - # can detect that an IPython kernel is running. - @patch("IPython.get_ipython", return_value=Mock(config={"IPKernelApp": True})) def test_notebook_display(self): """ Default display method is "notebook" when an IPython kernel is running. """ - # Display method should be "notebook" when an IPython kernel is running. - assert _get_default_display_method() == "notebook" - - # PYGMT_USE_EXTERNAL_DISPLAY should not affect notebook display. - with patch.dict("os.environ", {"PYGMT_USE_EXTERNAL_DISPLAY": "false"}): + # Mock IPython.get_ipython() to return an object with a config attribute, + # so PyGMT can detect that an IPython kernel is running. + with mock.patch( + "IPython.get_ipython", return_value=mock.Mock(config={"IPKernelApp": True}) + ): + # Display method should be "notebook" when an IPython kernel is running. assert _get_default_display_method() == "notebook" + + # PYGMT_USE_EXTERNAL_DISPLAY should not affect notebook display. + with mock.patch.dict("os.environ", {"PYGMT_USE_EXTERNAL_DISPLAY": "false"}): + assert _get_default_display_method() == "notebook" From e4b6b73ca3f0abdae8de39da21e6ec93a575f2db Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 21 Oct 2024 08:58:54 +0800 Subject: [PATCH 4/4] Import patch and Mock from unittest.mock --- pygmt/tests/test_figure.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pygmt/tests/test_figure.py b/pygmt/tests/test_figure.py index 7b8613d15cb..4cf4a027474 100644 --- a/pygmt/tests/test_figure.py +++ b/pygmt/tests/test_figure.py @@ -6,7 +6,7 @@ import importlib from pathlib import Path -from unittest import mock +from unittest.mock import Mock, patch import numpy as np import numpy.testing as npt @@ -439,12 +439,12 @@ def test_notebook_display(self): """ # Mock IPython.get_ipython() to return an object with a config attribute, # so PyGMT can detect that an IPython kernel is running. - with mock.patch( - "IPython.get_ipython", return_value=mock.Mock(config={"IPKernelApp": True}) + with patch( + "IPython.get_ipython", return_value=Mock(config={"IPKernelApp": True}) ): # Display method should be "notebook" when an IPython kernel is running. assert _get_default_display_method() == "notebook" # PYGMT_USE_EXTERNAL_DISPLAY should not affect notebook display. - with mock.patch.dict("os.environ", {"PYGMT_USE_EXTERNAL_DISPLAY": "false"}): + with patch.dict("os.environ", {"PYGMT_USE_EXTERNAL_DISPLAY": "false"}): assert _get_default_display_method() == "notebook"