Skip to content

Commit d9c9ee7

Browse files
committed
Add basic test for Basemap.arcgisimage with cache directory
1 parent 27ffdb8 commit d9c9ee7

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

packages/basemap/src/mpl_toolkits/basemap/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4353,7 +4353,7 @@ def arcgisimage(self, server="http://server.arcgisonline.com/ArcGIS",
43534353
]) % (server, service, xmin, ymin, xmax, ymax, self.epsg, self.epsg, xpixels, ypixels, dpi)
43544354

43554355
# Print URL in verbose mode.
4356-
if verbose:
4356+
if verbose: # pragma: no cover
43574357
print(basemap_url)
43584358

43594359
# Try to return fast if cache is enabled.
@@ -4367,7 +4367,7 @@ def arcgisimage(self, server="http://server.arcgisonline.com/ArcGIS",
43674367
# Return fast if the image is already in the cache.
43684368
cache_path = os.path.join(cachedir, filename)
43694369
if os.path.isfile(cache_path):
4370-
if verbose:
4370+
if verbose: # pragma: no cover
43714371
print("Image already in cache")
43724372
img = Image.open(cache_path)
43734373
return self.imshow(img, ax=ax, origin="upper")

packages/basemap/test/mpl_toolkits/basemap/test_Basemap.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"""Import test for the :mod:`mpl_toolkits.basemap.Basemap` class."""
22

3+
import os
4+
import shutil
5+
import tempfile
36
import datetime as dt
47
try:
58
import unittest2 as unittest
@@ -145,6 +148,39 @@ def test_arcgisimage_with_cyl(self, axs=None, axslen0=10):
145148
axs_children = axs_obj.get_children()
146149
self.assertEqual(len(axs_children), axslen0 + 1)
147150

151+
@unittest.skipIf(PIL is None, reason="pillow unavailable")
152+
def test_arcgisimage_with_cyl_using_cache(self, axs=None, axslen0=10):
153+
"""Test showing an ArcGIS image as background."""
154+
155+
axs_obj = plt.gca() if axs is None else axs
156+
axs_children = axs_obj.get_children()
157+
self.assertEqual(len(axs_children), axslen0)
158+
159+
bmap = Basemap(ax=axs, projection="cyl", resolution=None,
160+
llcrnrlon=-90, llcrnrlat=30,
161+
urcrnrlon=-60, urcrnrlat=60)
162+
163+
cachedir = tempfile.mkdtemp(prefix="tmp-basemap-cachedir-")
164+
try:
165+
# Check that the cache is initially empty.
166+
self.assertEqual(len(os.listdir(cachedir)), 0)
167+
# Check that the first call populates the cache.
168+
img = bmap.arcgisimage(verbose=False, cachedir=cachedir)
169+
self.assertEqual(len(os.listdir(cachedir)), 1)
170+
# Check output properties after the first call.
171+
self.assertIsInstance(img, AxesImage)
172+
axs_children = axs_obj.get_children()
173+
self.assertEqual(len(axs_children), axslen0 + 1)
174+
# Check that the second call does not update the cache.
175+
img = bmap.arcgisimage(verbose=False, cachedir=cachedir)
176+
self.assertEqual(len(os.listdir(cachedir)), 1)
177+
# Check output properties after the second call.
178+
self.assertIsInstance(img, AxesImage)
179+
axs_children = axs_obj.get_children()
180+
self.assertEqual(len(axs_children), axslen0 + 2)
181+
finally:
182+
shutil.rmtree(cachedir)
183+
148184
def _test_basemap_data_warpimage(self, method, axs=None, axslen0=10):
149185
"""Test drawing a map background from :mod:`basemap_data`."""
150186

0 commit comments

Comments
 (0)