Skip to content

Commit

Permalink
Test formatting platform (#8719)
Browse files Browse the repository at this point in the history
* Test formatting platform

* Default is float64

* Try to differentiate between OS-dependant and independant dtypes for CI safety

* wip

* fixed tests

* forgot one test

---------

Co-authored-by: Maximilian Roos <5635139+max-sixty@users.noreply.github.com>
  • Loading branch information
etienneschalk and max-sixty authored Feb 9, 2024
1 parent 97c8c18 commit 0ad7fa7
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 138 deletions.
96 changes: 14 additions & 82 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@
pytest.mark.filterwarnings("error:All-NaN (slice|axis) encountered"),
]

ON_WINDOWS = sys.platform == "win32"


class TestDataArray:
@pytest.fixture(autouse=True)
Expand All @@ -87,121 +85,55 @@ def setup(self):
self.mindex = pd.MultiIndex.from_product(
[["a", "b"], [1, 2]], names=("level_1", "level_2")
)
self.mda = DataArray([0, 1, 2, 3], coords={"x": self.mindex}, dims="x")
self.mda = DataArray([0, 1, 2, 3], coords={"x": self.mindex}, dims="x").astype(
np.uint64
)

@pytest.mark.skipif(
ON_WINDOWS,
reason="Default numpy's dtypes vary according to OS",
)
def test_repr(self) -> None:
v = Variable(["time", "x"], [[1, 2, 3], [4, 5, 6]], {"foo": "bar"})
coords = {"x": np.arange(3, dtype=np.int64), "other": np.int64(0)}
v = v.astype(np.uint64)
coords = {"x": np.arange(3, dtype=np.uint64), "other": np.uint64(0)}
data_array = DataArray(v, coords, name="my_variable")
expected = dedent(
"""\
<xarray.DataArray 'my_variable' (time: 2, x: 3)> Size: 48B
array([[1, 2, 3],
[4, 5, 6]])
Coordinates:
* x (x) int64 24B 0 1 2
other int64 8B 0
Dimensions without coordinates: time
Attributes:
foo: bar"""
)
assert expected == repr(data_array)

@pytest.mark.skipif(
not ON_WINDOWS,
reason="Default numpy's dtypes vary according to OS",
)
def test_repr_windows(self) -> None:
v = Variable(["time", "x"], [[1, 2, 3], [4, 5, 6]], {"foo": "bar"})
coords = {"x": np.arange(3, dtype=np.int64), "other": np.int64(0)}
data_array = DataArray(v, coords, name="my_variable")
expected = dedent(
"""\
<xarray.DataArray 'my_variable' (time: 2, x: 3)> Size: 24B
array([[1, 2, 3],
[4, 5, 6]])
[4, 5, 6]], dtype=uint64)
Coordinates:
* x (x) int64 24B 0 1 2
other int64 8B 0
* x (x) uint64 24B 0 1 2
other uint64 8B 0
Dimensions without coordinates: time
Attributes:
foo: bar"""
)
assert expected == repr(data_array)

@pytest.mark.skipif(
ON_WINDOWS,
reason="Default numpy's dtypes vary according to OS",
)
def test_repr_multiindex(self) -> None:
expected = dedent(
"""\
<xarray.DataArray (x: 4)> Size: 32B
array([0, 1, 2, 3])
array([0, 1, 2, 3], dtype=uint64)
Coordinates:
* x (x) object 32B MultiIndex
* level_1 (x) object 32B 'a' 'a' 'b' 'b'
* level_2 (x) int64 32B 1 2 1 2"""
)
assert expected == repr(self.mda)

@pytest.mark.skipif(
not ON_WINDOWS,
reason="Default numpy's dtypes vary according to OS",
)
def test_repr_multiindex_windows(self) -> None:
expected = dedent(
"""\
<xarray.DataArray (x: 4)> Size: 16B
array([0, 1, 2, 3])
Coordinates:
* x (x) object 32B MultiIndex
* level_1 (x) object 32B 'a' 'a' 'b' 'b'
* level_2 (x) int64 32B 1 2 1 2"""
)
assert expected == repr(self.mda)

@pytest.mark.skipif(
ON_WINDOWS,
reason="Default numpy's dtypes vary according to OS",
)
def test_repr_multiindex_long(self) -> None:
mindex_long = pd.MultiIndex.from_product(
[["a", "b", "c", "d"], [1, 2, 3, 4, 5, 6, 7, 8]],
names=("level_1", "level_2"),
)
mda_long = DataArray(list(range(32)), coords={"x": mindex_long}, dims="x")
mda_long = DataArray(
list(range(32)), coords={"x": mindex_long}, dims="x"
).astype(np.uint64)
expected = dedent(
"""\
<xarray.DataArray (x: 32)> Size: 256B
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])
Coordinates:
* x (x) object 256B MultiIndex
* level_1 (x) object 256B 'a' 'a' 'a' 'a' 'a' 'a' ... 'd' 'd' 'd' 'd' 'd' 'd'
* level_2 (x) int64 256B 1 2 3 4 5 6 7 8 1 2 3 4 ... 5 6 7 8 1 2 3 4 5 6 7 8"""
)
assert expected == repr(mda_long)

@pytest.mark.skipif(
not ON_WINDOWS,
reason="Default numpy's dtypes vary according to OS",
)
def test_repr_multiindex_long_windows(self) -> None:
mindex_long = pd.MultiIndex.from_product(
[["a", "b", "c", "d"], [1, 2, 3, 4, 5, 6, 7, 8]],
names=("level_1", "level_2"),
)
mda_long = DataArray(list(range(32)), coords={"x": mindex_long}, dims="x")
expected = dedent(
"""\
<xarray.DataArray (x: 32)> Size: 128B
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31],
dtype=uint64)
Coordinates:
* x (x) object 256B MultiIndex
* level_1 (x) object 256B 'a' 'a' 'a' 'a' 'a' 'a' ... 'd' 'd' 'd' 'd' 'd' 'd'
Expand Down
Loading

0 comments on commit 0ad7fa7

Please sign in to comment.