Skip to content

Commit

Permalink
pytest-ification (pydata#1828)
Browse files Browse the repository at this point in the history
* deprecation notes in testing functions

* gitignore additions

* self_assert from unittest2pytest

* pytest.warns needs a warning

* assert_equal

* future warnings

* assert identical

* pytest warning fix

* revert assert_equal, as assert_array_equal needs to be separate

* assert dataset & dataarray equal

* imports

* assertItemsEqual (but may need coercion to sets)

* Revert "assertItemsEqual (but may need coercion to sets)"

This reverts commit fa8f89a.

* add back in assertitemsequal

* self.assert_equal -> assert_equal

* pytest.warns message matching

* undo backend changes - too many errors

* a few triages

* another import

* can't deprecate importorskip yet

* assert_array_equal

* assert_identical half way in ufuncs

* backends merge

* lint v1

* lint v2

* autopep8 all the things

* lint final

* undo some of the yapf crazyness

* @Zac-HD dtype

* remove more yapf overeagerness

* test_ufuncs normal again

* final lint normalize

* a few left overs

* xarray-specific functions changed in backends

* undo self.AllClose

* remove some old xarray funcs

* assert_array_equal complete

* remove assertVariable*

* more parentheses please

* install pytest through pip in 3.4 CI

* flake8 fails on 3.4 only without this?
  • Loading branch information
max-sixty authored Jan 15, 2018
1 parent 0d69bf9 commit f3deb2f
Show file tree
Hide file tree
Showing 28 changed files with 2,471 additions and 2,500 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pip-log.txt
.tox
nosetests.xml
.cache
.ropeproject/

# Translations
*.mo
Expand All @@ -51,4 +52,7 @@ doc/_build
doc/generated
xarray/version.py

# Sync tools
Icon*

.ipynb_checkpoints
2 changes: 1 addition & 1 deletion ci/requirements-py34.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: test_env
dependencies:
- python=3.4
- bottleneck
- pytest
- flake8
- pandas
- pip:
- coveralls
- pytest-cov
- pytest
2 changes: 1 addition & 1 deletion xarray/conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def ensure_dtype_not_object(var, name=None):
fill_value = u''
else:
# insist on using float for numeric values
if not np.issubdtype(inferred_dtype, float):
if not np.issubdtype(inferred_dtype, np.floating):
inferred_dtype = np.dtype(float)
fill_value = inferred_dtype.type(np.nan)

Expand Down
6 changes: 4 additions & 2 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1573,7 +1573,8 @@ def relevant_keys(mapping):
dim_name = dim
dim_coord = None

reordered = self.transpose(*(list(indexer_dims) + list(non_indexed_dims)))
reordered = self.transpose(
*(list(indexer_dims) + list(non_indexed_dims)))

variables = OrderedDict()

Expand Down Expand Up @@ -3383,7 +3384,8 @@ def rank(self, dim, pct=False, keep_attrs=False):
Variables that do not depend on `dim` are dropped.
"""
if dim not in self.dims:
raise ValueError('Dataset does not contain the dimension: %s' % dim)
raise ValueError(
'Dataset does not contain the dimension: %s' % dim)

variables = OrderedDict()
for name, var in iteritems(self.variables):
Expand Down
12 changes: 7 additions & 5 deletions xarray/core/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ def maybe_promote(dtype):
fill_value : Valid missing value for the promoted dtype.
"""
# N.B. these casting rules should match pandas
if np.issubdtype(dtype, float):
if np.issubdtype(dtype, np.floating):
fill_value = np.nan
elif np.issubdtype(dtype, int):
# convert to floating point so NaN is valid
dtype = float
elif np.issubdtype(dtype, np.integer):
if dtype.itemsize <= 2:
dtype = np.float32
else:
dtype = np.float64
fill_value = np.nan
elif np.issubdtype(dtype, complex):
elif np.issubdtype(dtype, np.complexfloating):
fill_value = np.nan + np.nan * 1j
elif np.issubdtype(dtype, np.datetime64):
fill_value = np.datetime64('NaT')
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ def __array__(self, dtype=None):
if isinstance(array, pd.PeriodIndex):
with suppress(AttributeError):
# this might not be public API
array = array.asobject
array = array.astype('object')
return np.asarray(array.values, dtype=dtype)

@property
Expand Down
3 changes: 3 additions & 0 deletions xarray/core/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class NumpyInterpolator(BaseInterpolator):
--------
numpy.interp
'''

def __init__(self, xi, yi, method='linear', fill_value=None, **kwargs):

if method != 'linear':
Expand Down Expand Up @@ -83,6 +84,7 @@ class ScipyInterpolator(BaseInterpolator):
--------
scipy.interpolate.interp1d
'''

def __init__(self, xi, yi, method=None, fill_value=None,
assume_sorted=True, copy=False, bounds_error=False, **kwargs):
from scipy.interpolate import interp1d
Expand Down Expand Up @@ -118,6 +120,7 @@ class SplineInterpolator(BaseInterpolator):
--------
scipy.interpolate.UnivariateSpline
'''

def __init__(self, xi, yi, method='spline', fill_value=None, order=3,
**kwargs):
from scipy.interpolate import UnivariateSpline
Expand Down
1 change: 1 addition & 0 deletions xarray/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ class HiddenKeyDict(MutableMapping):
Acts like a normal dictionary, but hides certain keys.
'''
# ``__init__`` method required to create instance from class.

def __init__(self, data, hidden_keys):
self._data = data
if type(hidden_keys) not in (list, tuple):
Expand Down
56 changes: 6 additions & 50 deletions xarray/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
import importlib

import numpy as np
from numpy.testing import assert_array_equal
from numpy.testing import assert_array_equal # noqa: F401
from xarray.core.duck_array_ops import allclose_or_equiv
import pytest

from xarray.core import utils
from xarray.core.pycompat import PY3
from xarray.core.indexing import ExplicitlyIndexed
from xarray.testing import assert_equal, assert_identical, assert_allclose
from xarray.testing import (assert_equal, assert_identical, # noqa: F401
assert_allclose)
from xarray.plot.utils import import_seaborn

try:
Expand Down Expand Up @@ -109,6 +110,9 @@ def _importorskip(modname, minversion=None):


class TestCase(unittest.TestCase):
"""
These functions are all deprecated. Instead, use functions in xr.testing
"""
if PY3:
# Python 3 assertCountEqual is roughly equivalent to Python 2
# assertItemsEqual
Expand All @@ -125,26 +129,10 @@ def assertWarns(self, message):
assert len(w) > 0
assert any(message in str(wi.message) for wi in w)

def assertVariableEqual(self, v1, v2):
__tracebackhide__ = True # noqa: F841
assert_equal(v1, v2)

def assertVariableIdentical(self, v1, v2):
__tracebackhide__ = True # noqa: F841
assert_identical(v1, v2)

def assertVariableAllClose(self, v1, v2, rtol=1e-05, atol=1e-08):
__tracebackhide__ = True # noqa: F841
assert_allclose(v1, v2, rtol=rtol, atol=atol)

def assertVariableNotEqual(self, v1, v2):
__tracebackhide__ = True # noqa: F841
assert not v1.equals(v2)

def assertArrayEqual(self, a1, a2):
__tracebackhide__ = True # noqa: F841
assert_array_equal(a1, a2)

def assertEqual(self, a1, a2):
__tracebackhide__ = True # noqa: F841
assert a1 == a2 or (a1 != a1 and a2 != a2)
Expand All @@ -153,38 +141,6 @@ def assertAllClose(self, a1, a2, rtol=1e-05, atol=1e-8):
__tracebackhide__ = True # noqa: F841
assert allclose_or_equiv(a1, a2, rtol=rtol, atol=atol)

def assertDatasetEqual(self, d1, d2):
__tracebackhide__ = True # noqa: F841
assert_equal(d1, d2)

def assertDatasetIdentical(self, d1, d2):
__tracebackhide__ = True # noqa: F841
assert_identical(d1, d2)

def assertDatasetAllClose(self, d1, d2, rtol=1e-05, atol=1e-08,
decode_bytes=True):
__tracebackhide__ = True # noqa: F841
assert_allclose(d1, d2, rtol=rtol, atol=atol,
decode_bytes=decode_bytes)

def assertCoordinatesEqual(self, d1, d2):
__tracebackhide__ = True # noqa: F841
assert_equal(d1, d2)

def assertDataArrayEqual(self, ar1, ar2):
__tracebackhide__ = True # noqa: F841
assert_equal(ar1, ar2)

def assertDataArrayIdentical(self, ar1, ar2):
__tracebackhide__ = True # noqa: F841
assert_identical(ar1, ar2)

def assertDataArrayAllClose(self, ar1, ar2, rtol=1e-05, atol=1e-08,
decode_bytes=True):
__tracebackhide__ = True # noqa: F841
assert_allclose(ar1, ar2, rtol=rtol, atol=atol,
decode_bytes=decode_bytes)


@contextmanager
def raises_regex(error, pattern):
Expand Down
29 changes: 15 additions & 14 deletions xarray/tests/test_accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import numpy as np
import pandas as pd

from . import TestCase, requires_dask, raises_regex
from . import (TestCase, requires_dask, raises_regex, assert_equal,
assert_array_equal)


class TestDatetimeAccessor(TestCase):
Expand Down Expand Up @@ -36,10 +37,10 @@ def test_field_access(self):
hours = xr.DataArray(self.times.hour, name='hour',
coords=[self.times, ], dims=['time', ])

self.assertDataArrayEqual(years, self.data.time.dt.year)
self.assertDataArrayEqual(months, self.data.time.dt.month)
self.assertDataArrayEqual(days, self.data.time.dt.day)
self.assertDataArrayEqual(hours, self.data.time.dt.hour)
assert_equal(years, self.data.time.dt.year)
assert_equal(months, self.data.time.dt.month)
assert_equal(days, self.data.time.dt.day)
assert_equal(hours, self.data.time.dt.hour)

def test_not_datetime_type(self):
nontime_data = self.data.copy()
Expand Down Expand Up @@ -75,16 +76,16 @@ def test_dask_field_access(self):

# Double check that outcome chunksize is unchanged
dask_chunks = dask_times_2d.chunks
self.assertEqual(dask_year.data.chunks, dask_chunks)
self.assertEqual(dask_month.data.chunks, dask_chunks)
self.assertEqual(dask_day.data.chunks, dask_chunks)
self.assertEqual(dask_hour.data.chunks, dask_chunks)
assert dask_year.data.chunks == dask_chunks
assert dask_month.data.chunks == dask_chunks
assert dask_day.data.chunks == dask_chunks
assert dask_hour.data.chunks == dask_chunks

# Check the actual output from the accessors
self.assertDataArrayEqual(years, dask_year.compute())
self.assertDataArrayEqual(months, dask_month.compute())
self.assertDataArrayEqual(days, dask_day.compute())
self.assertDataArrayEqual(hours, dask_hour.compute())
assert_equal(years, dask_year.compute())
assert_equal(months, dask_month.compute())
assert_equal(days, dask_day.compute())
assert_equal(hours, dask_hour.compute())

def test_seasons(self):
dates = pd.date_range(start="2000/01/01", freq="M", periods=12)
Expand All @@ -93,4 +94,4 @@ def test_seasons(self):
"SON", "SON", "SON", "DJF"]
seasons = xr.DataArray(seasons)

self.assertArrayEqual(seasons.values, dates.dt.season.values)
assert_array_equal(seasons.values, dates.dt.season.values)
Loading

0 comments on commit f3deb2f

Please sign in to comment.