Skip to content

Commit 00e2605

Browse files
committed
Improve tests
1 parent cfb69d2 commit 00e2605

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

pygmt/tests/test_tilemap.py

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@
22
Test Figure.tilemap.
33
"""
44

5+
import importlib
6+
from unittest.mock import patch
7+
58
import pytest
69
from pygmt import Figure
710

8-
contextily = pytest.importorskip("contextily")
9-
rioxarray = pytest.importorskip("rioxarray")
11+
try:
12+
import contextily
13+
14+
_HAS_CONTEXTILY = True
15+
except ImportError:
16+
_HAS_CONTEXTILY = False
17+
18+
_HAS_RIOXARRAY = bool(importlib.util.find_spec("rioxarray"))
1019

1120

1221
@pytest.mark.mpl_image_compare
22+
@pytest.mark.skipif(not _HAS_CONTEXTILY, reason="contextily is not installed")
1323
def test_tilemap_web_mercator():
1424
"""
1525
Create a tilemap plot in Spherical Mercator projection (EPSG:3857).
@@ -27,6 +37,10 @@ def test_tilemap_web_mercator():
2737

2838
@pytest.mark.benchmark
2939
@pytest.mark.mpl_image_compare
40+
@pytest.mark.skipif(
41+
not (_HAS_CONTEXTILY and _HAS_RIOXARRAY),
42+
reason="contextily and rioxarray are not installed",
43+
)
3044
def test_tilemap_ogc_wgs84():
3145
"""
3246
Create a tilemap plot using longitude/latitude coordinates (OGC:WGS84), centred on
@@ -45,6 +59,10 @@ def test_tilemap_ogc_wgs84():
4559

4660
@pytest.mark.mpl_image_compare
4761
@pytest.mark.parametrize("no_clip", [False, True])
62+
@pytest.mark.skipif(
63+
not (_HAS_CONTEXTILY and _HAS_RIOXARRAY),
64+
reason="contextily and rioxarray are not installed",
65+
)
4866
def test_tilemap_no_clip(no_clip):
4967
"""
5068
Create a tilemap plot clipped to the Southern Hemisphere when no_clip is False, but
@@ -60,3 +78,34 @@ def test_tilemap_no_clip(no_clip):
6078
no_clip=no_clip,
6179
)
6280
return fig
81+
82+
83+
@pytest.mark.skipif(_HAS_CONTEXTILY, reason="contextily is installed.")
84+
def test_tilemap_no_contextily():
85+
"""
86+
Raise an ImportError when contextily is not installed.
87+
"""
88+
fig = Figure()
89+
with pytest.raises(ImportError, match="Package `contextily` is required"):
90+
fig.tilemap(
91+
region=[-20000000.0, 20000000.0, -20000000.0, 20000000.0],
92+
zoom=0,
93+
lonlat=False,
94+
frame="afg",
95+
)
96+
97+
98+
@pytest.mark.skipif(_HAS_RIOXARRAY, reason="rioxarray is installed.")
99+
def test_tilemap_no_rioxarray():
100+
"""
101+
Raise an ImportError when rioxarray is not installed and contextily is installed.
102+
"""
103+
fig = Figure()
104+
# In our CI, contextily and rioxarray are installed together, so we will see the
105+
# error about contextily, not rioxarray. Here we mock contextily as installed, to
106+
# make sure that we see the rioxarray error message when rioxarray is not installed.
107+
with patch("pygmt.datasets.tile_map._HAS_CONTEXTILY", True):
108+
with pytest.raises(ImportError, match="Package `rioxarray` is required"):
109+
fig.tilemap(
110+
region=[-180.0, 180.0, -90, 90], zoom=0, lonlat=True, frame="afg"
111+
)

0 commit comments

Comments
 (0)