Skip to content

Commit bd5d79f

Browse files
Add tests
1 parent 6dc0664 commit bd5d79f

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ min-file-size = 1024
264264
"TID252", # ban relative imports
265265
"PTH118", # `os.path.join()` should be replaced by `Path` with `/` operator,
266266
"PTH120", # `os.path.dirname()` should be replaced by `Path.parent`
267+
"PLR2004", # magic value in comparison
268+
"PLC2701" # Private name import
267269
]
268270

269271
[tool.ruff.lint.flake8-quotes]

tests/test_misc.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515

1616
import numpy as np
1717
import pytest
18+
import xarray as xr
1819

1920
from cmethods import adjust
21+
from cmethods.core import _add_cmethods_metadata
2022
from cmethods.distribution import (
2123
detrended_quantile_mapping,
2224
quantile_delta_mapping,
@@ -130,3 +132,70 @@ def test_adjust_failing_no_group_for_distribution(datasets: dict) -> None:
130132
n_quantiles=100,
131133
group="time.month",
132134
)
135+
136+
137+
def test_add_cmethods_metadata_with_dataarray() -> None:
138+
"""Test that _add_cmethods_metadata adds correct attributes to a DataArray"""
139+
data = xr.DataArray(
140+
np.array([1, 2, 3, 4, 5]),
141+
dims=["time"],
142+
coords={"time": np.arange(5)},
143+
)
144+
145+
result = _add_cmethods_metadata(
146+
data,
147+
method="linear_scaling",
148+
kind="+",
149+
n_quantiles=100,
150+
group="time.month",
151+
)
152+
153+
assert "cmethods_version" in result.attrs
154+
assert "cmethods_method" in result.attrs
155+
assert "cmethods_timestamp" in result.attrs
156+
assert "cmethods_source" in result.attrs
157+
158+
assert result.attrs["cmethods_method"] == "linear_scaling"
159+
assert result.attrs["cmethods_kind"] == "+"
160+
assert result.attrs["cmethods_n_quantiles"] == "100"
161+
assert result.attrs["cmethods_group"] == "time.month"
162+
assert result.attrs["cmethods_source"] == "https://github.com/btschwertfeger/python-cmethods"
163+
assert "UTC" in result.attrs["cmethods_timestamp"]
164+
165+
166+
def test_add_cmethods_metadata_with_dataset() -> None:
167+
"""Test that _add_cmethods_metadata adds correct attributes to a Dataset"""
168+
data = xr.Dataset(
169+
{
170+
"temperature": xr.DataArray(
171+
np.array([1, 2, 3, 4, 5]),
172+
dims=["time"],
173+
coords={"time": np.arange(5)},
174+
),
175+
},
176+
)
177+
178+
result = _add_cmethods_metadata(data, method="quantile_mapping")
179+
180+
assert "cmethods_version" in result.attrs
181+
assert "cmethods_method" in result.attrs
182+
assert "cmethods_timestamp" in result.attrs
183+
assert "cmethods_source" in result.attrs
184+
assert result.attrs["cmethods_method"] == "quantile_mapping"
185+
186+
187+
def test_add_cmethods_metadata_optional_params() -> None:
188+
"""Test that _add_cmethods_metadata handles optional parameters correctly"""
189+
data = xr.DataArray(
190+
np.array([1, 2, 3]),
191+
dims=["time"],
192+
coords={"time": np.arange(3)},
193+
)
194+
195+
result = _add_cmethods_metadata(data, method="variance_scaling")
196+
197+
assert "cmethods_method" in result.attrs
198+
assert result.attrs["cmethods_method"] == "variance_scaling"
199+
assert "cmethods_kind" not in result.attrs
200+
assert "cmethods_n_quantiles" not in result.attrs
201+
assert "cmethods_group" not in result.attrs

0 commit comments

Comments
 (0)