Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman committed Nov 27, 2024
1 parent cfb69d2 commit 00e2605
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions pygmt/tests/test_tilemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@
Test Figure.tilemap.
"""

import importlib
from unittest.mock import patch

import pytest
from pygmt import Figure

contextily = pytest.importorskip("contextily")
rioxarray = pytest.importorskip("rioxarray")
try:
import contextily

_HAS_CONTEXTILY = True
except ImportError:
_HAS_CONTEXTILY = False

_HAS_RIOXARRAY = bool(importlib.util.find_spec("rioxarray"))


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

@pytest.mark.benchmark
@pytest.mark.mpl_image_compare
@pytest.mark.skipif(
not (_HAS_CONTEXTILY and _HAS_RIOXARRAY),
reason="contextily and rioxarray are not installed",
)
def test_tilemap_ogc_wgs84():
"""
Create a tilemap plot using longitude/latitude coordinates (OGC:WGS84), centred on
Expand All @@ -45,6 +59,10 @@ def test_tilemap_ogc_wgs84():

@pytest.mark.mpl_image_compare
@pytest.mark.parametrize("no_clip", [False, True])
@pytest.mark.skipif(
not (_HAS_CONTEXTILY and _HAS_RIOXARRAY),
reason="contextily and rioxarray are not installed",
)
def test_tilemap_no_clip(no_clip):
"""
Create a tilemap plot clipped to the Southern Hemisphere when no_clip is False, but
Expand All @@ -60,3 +78,34 @@ def test_tilemap_no_clip(no_clip):
no_clip=no_clip,
)
return fig


@pytest.mark.skipif(_HAS_CONTEXTILY, reason="contextily is installed.")
def test_tilemap_no_contextily():
"""
Raise an ImportError when contextily is not installed.
"""
fig = Figure()
with pytest.raises(ImportError, match="Package `contextily` is required"):
fig.tilemap(
region=[-20000000.0, 20000000.0, -20000000.0, 20000000.0],
zoom=0,
lonlat=False,
frame="afg",
)


@pytest.mark.skipif(_HAS_RIOXARRAY, reason="rioxarray is installed.")
def test_tilemap_no_rioxarray():
"""
Raise an ImportError when rioxarray is not installed and contextily is installed.
"""
fig = Figure()
# In our CI, contextily and rioxarray are installed together, so we will see the
# error about contextily, not rioxarray. Here we mock contextily as installed, to
# make sure that we see the rioxarray error message when rioxarray is not installed.
with patch("pygmt.datasets.tile_map._HAS_CONTEXTILY", True):
with pytest.raises(ImportError, match="Package `rioxarray` is required"):
fig.tilemap(
region=[-180.0, 180.0, -90, 90], zoom=0, lonlat=True, frame="afg"
)

0 comments on commit 00e2605

Please sign in to comment.