Skip to content
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

use decorators to delay the application of given #7

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
11 changes: 5 additions & 6 deletions xarray_array_testing/creation.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import hypothesis.strategies as st
from functools import partial

import xarray.testing.strategies as xrst
from hypothesis import given

from xarray_array_testing.base import DuckArrayTestMixin
from xarray_array_testing.decorator import delayed_given


class CreationTests(DuckArrayTestMixin):
@given(st.data())
def test_create_variable(self, data):
variable = data.draw(xrst.variables(array_strategy_fn=self.array_strategy_fn))

@delayed_given(partial(xrst.variables))
def test_create_variable(self, variable):
assert isinstance(variable.data, self.array_type)
51 changes: 51 additions & 0 deletions xarray_array_testing/decorator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from functools import partial

from hypothesis import given


def instantiate_given(params, **kwargs):
def maybe_apply_kwargs(param, **kwargs):
if not isinstance(param, partial):
return param
else:
return param(**kwargs)

given_args, given_kwargs = params
instantiated_args = tuple(
maybe_apply_kwargs(param, **kwargs) for param in given_args
)
instantiated_kwargs = {
name: maybe_apply_kwargs(param, **kwargs)
for name, param in given_kwargs.items()
}

return instantiated_args, instantiated_kwargs


def initialize_tests(cls):
for name in dir(cls):
if not name.startswith("test_"):
continue

method = getattr(cls, name)

if not hasattr(method, "__hypothesis_given__"):
continue
params = method.__hypothesis_given__
args, kwargs = instantiate_given(
params, array_strategy_fn=cls.array_strategy_fn
)
decorated = given(*args, **kwargs)(method)

setattr(cls, name, decorated)

return cls


def delayed_given(*_given_args, **_given_kwargs):
def wrapper(f):
f.__hypothesis_given__ = (_given_args, _given_kwargs)

return f

return wrapper
10 changes: 4 additions & 6 deletions xarray_array_testing/reduction.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from contextlib import nullcontext
from functools import partial

import hypothesis.strategies as st
import pytest
import xarray.testing.strategies as xrst
from hypothesis import given

from xarray_array_testing.base import DuckArrayTestMixin
from xarray_array_testing.decorator import delayed_given


class ReductionTests(DuckArrayTestMixin):
Expand All @@ -14,10 +14,8 @@ def expected_errors(op, **parameters):
return nullcontext()

@pytest.mark.parametrize("op", ["mean", "sum", "prod", "std", "var"])
@given(st.data())
def test_variable_mean(self, op, data):
variable = data.draw(xrst.variables(array_strategy_fn=self.array_strategy_fn))

@delayed_given(partial(xrst.variables))
def test_variable(self, op, variable):
with self.expected_errors(op, variable=variable):
# compute using xr.Variable.<OP>()
actual = getattr(variable, op)().data
Expand Down
3 changes: 3 additions & 0 deletions xarray_array_testing/tests/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from xarray_array_testing.base import DuckArrayTestMixin
from xarray_array_testing.creation import CreationTests
from xarray_array_testing.decorator import initialize_tests
from xarray_array_testing.reduction import ReductionTests


Expand All @@ -26,9 +27,11 @@ def array_strategy_fn(*, shape, dtype):
return create_numpy_array(shape=shape, dtype=dtype)


@initialize_tests
class TestCreationNumpy(CreationTests, NumpyTestMixin):
pass


@initialize_tests
class TestReductionNumpy(ReductionTests, NumpyTestMixin):
pass