|
5 | 5 | """
|
6 | 6 | import os
|
7 | 7 |
|
| 8 | +try: |
| 9 | + import IPython |
| 10 | +except ModuleNotFoundError: |
| 11 | + IPython = None # pylint: disable=invalid-name |
| 12 | + |
| 13 | + |
8 | 14 | import numpy as np
|
9 | 15 | import numpy.testing as npt
|
10 | 16 | import pytest
|
11 | 17 | from pygmt import Figure, set_display
|
12 |
| -from pygmt.exceptions import GMTInvalidInput |
| 18 | +from pygmt.exceptions import GMTError, GMTInvalidInput |
13 | 19 | from pygmt.helpers import GMTTempFile
|
14 | 20 |
|
15 | 21 |
|
@@ -52,6 +58,23 @@ def test_figure_region_country_codes():
|
52 | 58 | npt.assert_allclose(fig.region, np.array([0.0, 360.0, -90.0, 90.0]))
|
53 | 59 |
|
54 | 60 |
|
| 61 | +def test_figure_repr(): |
| 62 | + """ |
| 63 | + Make sure that figure output's PNG and HTML printable representations look |
| 64 | + ok. |
| 65 | + """ |
| 66 | + fig = Figure() |
| 67 | + fig.basemap(region=[0, 1, 2, 3], frame=True) |
| 68 | + # Check that correct PNG 8-byte file header is produced |
| 69 | + # https://en.wikipedia.org/wiki/Portable_Network_Graphics#File_header |
| 70 | + repr_png = fig._repr_png_() # pylint: disable=protected-access |
| 71 | + assert repr_png.hex().startswith("89504e470d0a1a0a") |
| 72 | + # Check that correct HTML image tags are produced |
| 73 | + repr_html = fig._repr_html_() # pylint: disable=protected-access |
| 74 | + assert repr_html.startswith('<img src="data:image/png;base64,') |
| 75 | + assert repr_html.endswith('" width="500px">') |
| 76 | + |
| 77 | + |
55 | 78 | def test_figure_savefig_exists():
|
56 | 79 | """
|
57 | 80 | Make sure the saved figure has the right name.
|
@@ -159,6 +182,7 @@ def mock_psconvert(*args, **kwargs): # pylint: disable=unused-argument
|
159 | 182 | )
|
160 | 183 |
|
161 | 184 |
|
| 185 | +@pytest.mark.skipif(IPython is None, reason="run when IPython is installed") |
162 | 186 | def test_figure_show():
|
163 | 187 | """
|
164 | 188 | Test that show creates the correct file name and deletes the temp dir.
|
@@ -199,6 +223,27 @@ def test_figure_show_invalid_method():
|
199 | 223 | fig.show(method="test")
|
200 | 224 |
|
201 | 225 |
|
| 226 | +@pytest.mark.skipif(IPython is not None, reason="run without IPython installed") |
| 227 | +def test_figure_show_notebook_error_without_ipython(): |
| 228 | + """ |
| 229 | + Test to check if an error is raised when display method is 'notebook', but |
| 230 | + IPython is not installed. |
| 231 | + """ |
| 232 | + fig = Figure() |
| 233 | + fig.basemap(region=[0, 1, 2, 3], frame=True) |
| 234 | + with pytest.raises(GMTError): |
| 235 | + fig.show(method="notebook") |
| 236 | + |
| 237 | + |
| 238 | +def test_figure_display_external(): |
| 239 | + """ |
| 240 | + Test to check that a figure can be displayed in an external window. |
| 241 | + """ |
| 242 | + fig = Figure() |
| 243 | + fig.basemap(region=[0, 3, 6, 9], projection="X1c", frame=True) |
| 244 | + fig.show(method="external") |
| 245 | + |
| 246 | + |
202 | 247 | def test_figure_set_display_invalid():
|
203 | 248 | """
|
204 | 249 | Test to check if an error is raised when an invalid method is passed to
|
|
0 commit comments