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

Mark tests which require network access #144

Merged
merged 8 commits into from
Jun 17, 2024
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/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:

- name: Running Tests
run: |
python -m pytest --cov=./ --cov-report=xml --verbose
python -m pytest ./virtualizarr --run-network-tests --cov=./ --cov-report=xml --verbose

- name: Upload code coverage to Codecov
uses: codecov/[email protected]
Expand Down
3 changes: 3 additions & 0 deletions docs/releases.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ Documentation
Internal Changes
~~~~~~~~~~~~~~~~

- Mark tests which require network access so that they are only run when `--run-network-tests` is passed a command-line argument to pytest.
(:pull:`144`) By `Tom Nicholas <https://github.com/TomNicholas>`_.

.. _v0.1:

v0.1 (17th June 2024)
Expand Down
28 changes: 28 additions & 0 deletions virtualizarr/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
import importlib
import itertools

import numpy as np
import pytest
from packaging.version import Version

from virtualizarr.manifests import ChunkManifest, ManifestArray
from virtualizarr.manifests.manifest import join
from virtualizarr.zarr import ZArray, ceildiv

network = pytest.mark.network


def _importorskip(
modname: str, minversion: str | None = None
) -> tuple[bool, pytest.MarkDecorator]:
try:
mod = importlib.import_module(modname)
has = True
if minversion is not None:
v = getattr(mod, "__version__", "999")
if Version(v) < Version(minversion):
raise ImportError("Minimum version not satisfied")
except ImportError:
has = False

reason = f"requires {modname}"
if minversion is not None:
reason += f">={minversion}"
func = pytest.mark.skipif(not has, reason=reason)
return has, func


has_s3fs, requires_s3fs = _importorskip("s3fs")


def create_manifestarray(
shape: tuple[int, ...], chunks: tuple[int, ...]
Expand Down
18 changes: 18 additions & 0 deletions virtualizarr/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@
import xarray as xr


def pytest_addoption(parser):
"""Add command-line flags for pytest."""
parser.addoption("--run-flaky", action="store_true", help="runs flaky tests")
parser.addoption(
"--run-network-tests",
action="store_true",
help="runs tests requiring a network connection",
)


def pytest_runtest_setup(item):
# based on https://stackoverflow.com/questions/47559524
if "network" in item.keywords and not item.config.getoption("--run-network-tests"):
pytest.skip(
"set --run-network-tests to run tests requiring an internet connection"
)


@pytest.fixture
def netcdf4_file(tmpdir):
# Set up example xarray dataset
Expand Down
35 changes: 19 additions & 16 deletions virtualizarr/tests/test_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from virtualizarr import open_virtual_dataset
from virtualizarr.manifests import ChunkManifest, ManifestArray
from virtualizarr.tests import network, requires_s3fs
from virtualizarr.zarr import ZArray


Expand Down Expand Up @@ -270,22 +271,24 @@ def test_combine_by_coords(self, netcdf4_files):
assert combined_vds.xindexes["time"].to_pandas_index().is_monotonic_increasing


pytest.importorskip("s3fs")


@pytest.mark.parametrize(
"filetype", ["netcdf4", None], ids=["netcdf4 filetype", "None filetype"]
)
@pytest.mark.parametrize("indexes", [None, {}], ids=["None index", "empty dict index"])
def test_anon_read_s3(filetype, indexes):
"""Parameterized tests for empty vs supplied indexes and filetypes."""
# TODO: Switch away from this s3 url after minIO is implemented.
fpath = "s3://carbonplan-share/virtualizarr/local.nc"
vds = open_virtual_dataset(fpath, filetype=filetype, indexes=indexes)

assert vds.dims == {"time": 2920, "lat": 25, "lon": 53}
for var in vds.variables:
assert isinstance(vds[var].data, ManifestArray), var
@network
@requires_s3fs
class TestReadFromS3:
@pytest.mark.parametrize(
"filetype", ["netcdf4", None], ids=["netcdf4 filetype", "None filetype"]
)
@pytest.mark.parametrize(
"indexes", [None, {}], ids=["None index", "empty dict index"]
)
def test_anon_read_s3(self, filetype, indexes):
"""Parameterized tests for empty vs supplied indexes and filetypes."""
# TODO: Switch away from this s3 url after minIO is implemented.
fpath = "s3://carbonplan-share/virtualizarr/local.nc"
vds = open_virtual_dataset(fpath, filetype=filetype, indexes=indexes)

assert vds.dims == {"time": 2920, "lat": 25, "lon": 53}
for var in vds.variables:
assert isinstance(vds[var].data, ManifestArray), var


class TestLoadVirtualDataset:
Expand Down
Loading