Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various test improvements #1887

Merged
merged 8 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
# Check that the downloader tool at least knows where to get the data from (but don't actually download it)
python tools/cartopy_feature_download.py gshhs physical --dry-run
CARTOPY_GIT_DIR=$PWD
PYPROJ_GLOBAL_CONTEXT=ON pytest -n 4 --doctest-modules --pyargs cartopy ${EXTRA_TEST_ARGS}
PYPROJ_GLOBAL_CONTEXT=ON pytest -ra -n 4 --doctest-modules --pyargs cartopy ${EXTRA_TEST_ARGS}

- name: Coveralls
if: steps.coverage.conclusion == 'success'
Expand Down
34 changes: 16 additions & 18 deletions lib/cartopy/tests/io/test_downloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@

import contextlib
import os
import warnings
from unittest import mock

import pytest

import cartopy
import cartopy.io as cio
from cartopy.io.shapereader import NEShpDownloader
from cartopy.tests.mpl.test_caching import CallCounter


def test_Downloader_data():
Expand Down Expand Up @@ -110,21 +109,18 @@ def test_downloading_simple_ascii(download_to_temp):

assert dnld_item.target_path(format_dict) == tmp_fname

with warnings.catch_warnings(record=True) as w:
with pytest.warns(cio.DownloadWarning):
assert dnld_item.path(format_dict) == tmp_fname

assert len(w) == 1, \
f'Expected a single download warning to be raised. Got {len(w)}.'
assert issubclass(w[0].category, cio.DownloadWarning)

with open(tmp_fname) as fh:
fh.readline()
assert fh.readline() == " * jQuery JavaScript Library v1.8.2\n"

# check that calling path again doesn't try re-downloading
with CallCounter(dnld_item, 'acquire_resource') as counter:
with mock.patch.object(dnld_item, 'acquire_resource',
wraps=dnld_item.acquire_resource) as counter:
assert dnld_item.path(format_dict) == tmp_fname
assert counter.count == 0, 'Item was re-downloaded.'
counter.assert_not_called()


@pytest.mark.network
Expand All @@ -147,17 +143,19 @@ def test_natural_earth_downloader(tmp_path):
dnld_item = NEShpDownloader(target_path_template=shp_path_template)

# check that the file gets downloaded the first time path is called
with CallCounter(dnld_item, 'acquire_resource') as counter:
with mock.patch.object(dnld_item, 'acquire_resource',
wraps=dnld_item.acquire_resource) as counter:
with pytest.warns(cartopy.io.DownloadWarning, match="Downloading:"):
shp_path = dnld_item.path(format_dict)
assert counter.count == 1, 'Item not downloaded.'
counter.assert_called_once()

assert shp_path_template.format(**format_dict) == shp_path

# check that calling path again doesn't try re-downloading
with CallCounter(dnld_item, 'acquire_resource') as counter:
with mock.patch.object(dnld_item, 'acquire_resource',
wraps=dnld_item.acquire_resource) as counter:
assert dnld_item.path(format_dict) == shp_path
assert counter.count == 0, 'Item was re-downloaded.'
counter.assert_not_called()

# check that we have the shp and the shx
exts = ['.shp', '.shx']
Expand All @@ -168,11 +166,11 @@ def test_natural_earth_downloader(tmp_path):

# check that providing a pre downloaded path actually works
pre_dnld = NEShpDownloader(target_path_template='/not/a/real/file.txt',
pre_downloaded_path_template=shp_path
)
pre_downloaded_path_template=shp_path)

# check that the pre_dnld downloader doesn't re-download, but instead
# uses the path of the previously downloaded item

with CallCounter(pre_dnld, 'acquire_resource') as counter:
with mock.patch.object(pre_dnld, 'acquire_resource',
wraps=pre_dnld.acquire_resource) as counter:
assert pre_dnld.path(format_dict) == shp_path
assert counter.count == 0, 'Aquire resource called more than once.'
counter.assert_not_called()
6 changes: 1 addition & 5 deletions lib/cartopy/tests/io/test_srtm.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
# See COPYING and COPYING.LESSER in the root of the repository for full
# licensing details.

import warnings

import numpy as np
from numpy.testing import assert_array_equal
import pytest
Expand Down Expand Up @@ -65,10 +63,8 @@ class TestRetrieve:
def test_srtm_retrieve(self, Source, read_SRTM, max_, min_, pt,
download_to_temp): # noqa: F811
# test that the download mechanism for SRTM works
with warnings.catch_warnings(record=True) as w:
with pytest.warns(cartopy.io.DownloadWarning):
r = Source().srtm_fname(-4, 50)
assert len(w) == 1
assert issubclass(w[0].category, cartopy.io.DownloadWarning)

assert r.startswith(str(download_to_temp)), \
'File not downloaded to tmp dir'
Expand Down
24 changes: 9 additions & 15 deletions lib/cartopy/tests/mpl/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,13 @@
import cartopy.crs as ccrs
from cartopy.mpl.geoaxes import InterProjectionTransform, GeoAxes
from cartopy.tests.mpl import ImageTesting
from cartopy.tests.mpl.test_caching import CallCounter


class TestNoSpherical:
def setup_method(self):
self.ax = plt.axes(projection=ccrs.PlateCarree())
self.data = np.arange(12).reshape((3, 4))

def teardown_method(self):
plt.clf()
plt.close()

def test_contour(self):
with pytest.raises(ValueError):
self.ax.contour(self.data, transform=ccrs.Geodetic())
Expand Down Expand Up @@ -55,20 +50,21 @@ def test_transform_PlateCarree_shortcut():

trans = InterProjectionTransform(src, target)

counter = CallCounter(target, 'project_geometry')

with counter:
with mock.patch.object(target, 'project_geometry',
wraps=target.project_geometry) as counter:
trans.transform_path(pth1)
# pth1 should allow a short-cut.
assert counter.count == 0
counter.assert_not_called()

with counter:
with mock.patch.object(target, 'project_geometry',
wraps=target.project_geometry) as counter:
trans.transform_path(pth2)
assert counter.count == 1
counter.assert_called_once()

with counter:
with mock.patch.object(target, 'project_geometry',
wraps=target.project_geometry) as counter:
trans.transform_path(pth3)
assert counter.count == 2
counter.assert_called_once()


class Test_InterProjectionTransform:
Expand Down Expand Up @@ -98,8 +94,6 @@ def test_ne(self):


class Test_Axes_add_geometries:
def teardown_method(self):
plt.close()

@mock.patch('cartopy.mpl.geoaxes.GeoAxes.add_feature')
@mock.patch('cartopy.feature.ShapelyFeature')
Expand Down
Loading