Skip to content

Commit

Permalink
test testing (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathause committed Jan 18, 2023
1 parent 84d7e3c commit d9f7c21
Showing 1 changed file with 131 additions and 1 deletion.
132 changes: 131 additions & 1 deletion tests/unit/test_testing.py
Expand Up @@ -2,7 +2,13 @@
import pytest
import xarray as xr

from mesmer.testing import assert_dict_allclose
from mesmer.core.utils import _check_dataarray_form
from mesmer.testing import (
assert_dict_allclose,
trend_data_1D,
trend_data_2D,
trend_data_3D,
)


def test_assert_dict_allclose_type_key_errors():
Expand Down Expand Up @@ -105,3 +111,127 @@ def test_assert_dict_allclose_nested():

assert_dict_allclose(a, c)
assert_dict_allclose(c, a)


@pytest.mark.parametrize("n_timesteps", [2, 3])
@pytest.mark.parametrize("intercept", [0, 3.14])
@pytest.mark.parametrize("slope", [0, 2.72])
def test_trend_data_1D_no_scale(n_timesteps, intercept, slope):

result = trend_data_1D(
n_timesteps=n_timesteps, intercept=intercept, slope=slope, scale=0
)

_check_dataarray_form(
result, "trend_data_1D", ndim=1, required_dims="time", shape=(n_timesteps,)
)

assert result[0] == intercept
assert result[1] == intercept + slope


def test_trend_data_1D_scale():

n_timesteps = 5
intercept, slope = 3.14, 2.72

result = trend_data_1D(n_timesteps=n_timesteps, intercept=intercept, slope=slope)

_check_dataarray_form(
result, "trend_data_1D", ndim=1, required_dims="time", shape=(n_timesteps,)
)

assert result[0] != intercept
assert result[1] != intercept + slope


@pytest.mark.parametrize("n_timesteps", [2, 3])
@pytest.mark.parametrize("n_lat", [2, 3])
@pytest.mark.parametrize("n_lon", [2, 3])
@pytest.mark.parametrize("intercept", [0, 1, 3.14])
@pytest.mark.parametrize("slope", [0, 2.72])
def test_trend_data_2D_3D_no_scale(n_timesteps, n_lat, n_lon, intercept, slope):

result = trend_data_2D(
n_timesteps=n_timesteps,
n_lat=n_lat,
n_lon=n_lon,
intercept=intercept,
slope=slope,
scale=0,
)

_check_dataarray_form(
result,
"trend_data_2D",
ndim=2,
required_dims={"time", "cells"},
shape=(n_lat * n_lon, n_timesteps),
)

np.testing.assert_allclose(result[..., 0], intercept)
np.testing.assert_allclose(result[..., 1], intercept + slope)

result = trend_data_3D(
n_timesteps=n_timesteps,
n_lat=n_lat,
n_lon=n_lon,
intercept=intercept,
slope=slope,
scale=0,
)

_check_dataarray_form(
result,
"trend_data_3D",
ndim=3,
required_dims={"time", "lat", "lon"},
shape=(n_timesteps, n_lat, n_lon),
)

np.testing.assert_allclose(result[0, ...], intercept)
np.testing.assert_allclose(result[1, ...], intercept + slope)


def test_trend_data_2D_3D_scale():

n_timesteps, n_lat, n_lon = 5, 2, 3
intercept, slope = 3.14, 2.72

result = trend_data_2D(
n_timesteps=n_timesteps,
n_lat=n_lat,
n_lon=n_lon,
intercept=intercept,
slope=slope,
)

_check_dataarray_form(
result,
"trend_data_2D",
ndim=2,
required_dims={"time", "cells"},
shape=(n_lat * n_lon, n_timesteps),
)

assert not np.allclose(result[..., 0], intercept)
assert not np.allclose(result[..., 1], intercept + slope)

result = trend_data_3D(
n_timesteps=n_timesteps,
n_lat=n_lat,
n_lon=n_lon,
intercept=intercept,
slope=slope,
)

_check_dataarray_form(
result,
"trend_data_3D",
ndim=3,
required_dims={"time", "lat", "lon"},
shape=(n_timesteps, n_lat, n_lon),
)

assert not np.allclose(result[0, ...], intercept)
assert not np.allclose(result[1, ...], intercept + slope)

0 comments on commit d9f7c21

Please sign in to comment.