-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Starter property-based test suite (#1972)
- Loading branch information
Showing
5 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: test_env | ||
channels: | ||
- conda-forge | ||
dependencies: | ||
- python=3.6 | ||
- dask | ||
- distributed | ||
- h5py | ||
- h5netcdf | ||
- matplotlib | ||
- netcdf4 | ||
- pytest | ||
- flake8 | ||
- numpy | ||
- pandas | ||
- scipy | ||
- seaborn | ||
- toolz | ||
- rasterio | ||
- bottleneck | ||
- zarr | ||
- pip: | ||
- coveralls | ||
- pytest-cov | ||
- pydap | ||
- lxml | ||
- hypothesis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Property-based tests using Hypothesis | ||
|
||
This directory contains property-based tests using a library | ||
called [Hypothesis](https://github.com/HypothesisWorks/hypothesis-python). | ||
|
||
The property tests for Xarray are a work in progress - more are always welcome. | ||
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`. | ||
|
||
## Hang on, "property-based" tests? | ||
|
||
Instead of making assertions about operations on a particular piece of | ||
data, you use Hypothesis to describe a *kind* of data, then make assertions | ||
that should hold for *any* example of this kind. | ||
|
||
For example: "given a 2d ndarray of dtype uint8 `arr`, | ||
`xr.DataArray(arr).plot.imshow()` never raises an exception". | ||
|
||
Hypothesis will then try many random examples, and report a minimised | ||
failing input for each error it finds. | ||
[See the docs for more info.](https://hypothesis.readthedocs.io/en/master/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
""" | ||
Property-based tests for encoding/decoding methods. | ||
These ones pass, just as you'd hope! | ||
""" | ||
from __future__ import absolute_import, division, print_function | ||
|
||
from hypothesis import given, settings | ||
import hypothesis.strategies as st | ||
import hypothesis.extra.numpy as npst | ||
|
||
import xarray as xr | ||
|
||
# Run for a while - arrays are a bigger search space than usual | ||
settings.deadline = None | ||
|
||
|
||
an_array = npst.arrays( | ||
dtype=st.one_of( | ||
npst.unsigned_integer_dtypes(), | ||
npst.integer_dtypes(), | ||
npst.floating_dtypes(), | ||
), | ||
shape=npst.array_shapes(max_side=3), # max_side specified for performance | ||
) | ||
|
||
|
||
@given(st.data(), an_array) | ||
def test_CFMask_coder_roundtrip(data, arr): | ||
names = data.draw(st.lists(st.text(), min_size=arr.ndim, | ||
max_size=arr.ndim, unique=True).map(tuple)) | ||
original = xr.Variable(names, arr) | ||
coder = xr.coding.variables.CFMaskCoder() | ||
roundtripped = coder.decode(coder.encode(original)) | ||
xr.testing.assert_identical(original, roundtripped) | ||
|
||
|
||
@given(st.data(), an_array) | ||
def test_CFScaleOffset_coder_roundtrip(data, arr): | ||
names = data.draw(st.lists(st.text(), min_size=arr.ndim, | ||
max_size=arr.ndim, unique=True).map(tuple)) | ||
original = xr.Variable(names, arr) | ||
coder = xr.coding.variables.CFScaleOffsetCoder() | ||
roundtripped = coder.decode(coder.encode(original)) | ||
xr.testing.assert_identical(original, roundtripped) |