Skip to content

Add hypothesis test for netCDF4 roundtrip #3283

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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: 2 additions & 0 deletions properties/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ They are stored in a separate directory because they tend to run more examples
and thus take longer, and so that local development can run a test suite
without needing to `pip install hypothesis`.

To run these tests, run `pytest` in this directory.

## Hang on, "property-based" tests?

Instead of making assertions about operations on a particular piece of
Expand Down
49 changes: 49 additions & 0 deletions properties/test_netcdf_roundtrip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
Property-based tests for round-tripping data to netCDF
"""
import hypothesis.extra.numpy as npst
import hypothesis.strategies as st
from hypothesis import given, settings

import xarray as xr

# Run for a while - arrays are a bigger search space than usual
settings.register_profile("ci", deadline=None)
settings.load_profile("ci")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd delete this part - it's at best redundant given the changes in #3285 and I'd prefer that config.



an_array = npst.arrays(
dtype=st.one_of(
npst.unsigned_integer_dtypes(),
npst.integer_dtypes(),
# NetCDF does not support float16
# https://www.unidata.ucar.edu/software/netcdf/docs/data_type.html
npst.floating_dtypes(sizes=(32, 64)),
),
shape=npst.array_shapes(max_side=3), # max_side specified for performance
)

compatible_names = st.text(
alphabet=st.characters(
whitelist_categories=("Ll", "Lu", "Nd"),
# It looks like netCDF should allow unicode names, but removing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this specified? It would be useful to provide a link and prehaps explain what's going on with the categories too 😄

# this causes a failure with 'ά'
max_codepoint=255,
),
min_size=1,
)


@given(st.data(), an_array)
def test_netcdf_roundtrip(tmp_path, data, arr):
names = data.draw(
st.lists(
compatible_names, min_size=arr.ndim, max_size=arr.ndim, unique=True
).map(tuple)
)
var = xr.Variable(names, arr)
original = xr.Dataset({"data": var})
original.to_netcdf(tmp_path / "test.nc")

roundtripped = xr.open_dataset(tmp_path / "test.nc")
xr.testing.assert_identical(original, roundtripped)