1
1
from contextlib import nullcontext
2
2
3
3
import hypothesis .strategies as st
4
+ import pytest
4
5
import xarray .testing .strategies as xrst
5
6
from hypothesis import HealthCheck , given , note , settings
6
7
@@ -14,27 +15,18 @@ def expected_errors(op, **parameters):
14
15
15
16
# TODO understand the differing executors health check error
16
17
@settings (suppress_health_check = [HealthCheck .differing_executors ])
18
+ @pytest .mark .parametrize ("op" , ["mean" , "sum" , "prod" , "std" , "var" ])
17
19
@given (st .data ())
18
- def test_variable_mean (self , data ):
20
+ def test_variable_numerical_reduce (self , op , data ):
19
21
variable = data .draw (xrst .variables (array_strategy_fn = self .array_strategy_fn ))
20
22
21
23
note (f"note: { variable } " )
22
24
23
25
with self .expected_errors ("mean" , variable = variable ):
24
- actual = variable .mean ().data
25
- expected = self .xp .mean (variable .data )
26
-
27
- assert isinstance (actual , self .array_type ), type (actual )
28
- self .assert_equal (actual , expected )
29
-
30
- @settings (suppress_health_check = [HealthCheck .differing_executors ])
31
- @given (st .data ())
32
- def test_variable_prod (self , data ):
33
- variable = data .draw (xrst .variables (array_strategy_fn = self .array_strategy_fn ))
34
-
35
- with self .expected_errors ("prod" , variable = variable ):
36
- actual = variable .prod ().data
37
- expected = self .xp .prod (variable .data )
26
+ # compute using xr.Variable.<OP>()
27
+ actual = getattr (variable , op )().data
28
+ # compute using xp.<OP>(array)
29
+ expected = getattr (self .xp , op )(variable .data )
38
30
39
31
assert isinstance (actual , self .array_type ), type (actual )
40
32
self .assert_equal (actual , expected )
0 commit comments