|
| 1 | +""" |
| 2 | +Property-based tests for encoding/decoding methods. |
| 3 | +
|
| 4 | +These ones pass, just as you'd hope! |
| 5 | +
|
| 6 | +""" |
| 7 | +from __future__ import absolute_import, division, print_function |
| 8 | + |
| 9 | +from hypothesis import given, settings |
| 10 | +import hypothesis.strategies as st |
| 11 | +import hypothesis.extra.numpy as npst |
| 12 | + |
| 13 | +import xarray as xr |
| 14 | + |
| 15 | +# Run for a while - arrays are a bigger search space than usual |
| 16 | +settings.deadline = None |
| 17 | + |
| 18 | + |
| 19 | +an_array = npst.arrays( |
| 20 | + dtype=st.one_of( |
| 21 | + npst.unsigned_integer_dtypes(), |
| 22 | + npst.integer_dtypes(), |
| 23 | + npst.floating_dtypes(), |
| 24 | + ), |
| 25 | + shape=npst.array_shapes(max_side=3), # max_side specified for performance |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +@given(st.data(), an_array) |
| 30 | +def test_CFMask_coder_roundtrip(data, arr): |
| 31 | + names = data.draw(st.lists(st.text(), min_size=arr.ndim, |
| 32 | + max_size=arr.ndim, unique=True).map(tuple)) |
| 33 | + original = xr.Variable(names, arr) |
| 34 | + coder = xr.coding.variables.CFMaskCoder() |
| 35 | + roundtripped = coder.decode(coder.encode(original)) |
| 36 | + xr.testing.assert_identical(original, roundtripped) |
| 37 | + |
| 38 | + |
| 39 | +@given(st.data(), an_array) |
| 40 | +def test_CFScaleOffset_coder_roundtrip(data, arr): |
| 41 | + names = data.draw(st.lists(st.text(), min_size=arr.ndim, |
| 42 | + max_size=arr.ndim, unique=True).map(tuple)) |
| 43 | + original = xr.Variable(names, arr) |
| 44 | + coder = xr.coding.variables.CFScaleOffsetCoder() |
| 45 | + roundtripped = coder.decode(coder.encode(original)) |
| 46 | + xr.testing.assert_identical(original, roundtripped) |
0 commit comments