Skip to content

Commit ab93efd

Browse files
committed
add better tests
1 parent 1a5cfe7 commit ab93efd

2 files changed

Lines changed: 60 additions & 3 deletions

File tree

src/eopf_geozarr/conversion/fs_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def replace_json_invalid_floats(obj: object) -> object:
5252

5353
class NanCompatibleJSONEncoder(json.JSONEncoder):
5454
"""
55-
Custom JSON encoder that converts NaN values to the string "NaN"
55+
Custom JSON encoder that converts NaN, Inf, -Inf values to JSON-safe equivalents
5656
to ensure valid JSON output.
5757
"""
5858

tests/test_fs_utils.py

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
from unittest.mock import Mock, patch
44

5+
import numpy as np
56
import pytest
7+
import xarray as xr
68

79
from eopf_geozarr.conversion.fs_utils import (
810
get_s3_credentials_info,
@@ -14,6 +16,7 @@
1416
path_exists,
1517
read_json_metadata,
1618
replace_json_invalid_floats,
19+
sanitize_dataset_attributes,
1720
validate_s3_access,
1821
write_json_metadata,
1922
)
@@ -210,10 +213,64 @@ def test_replace_json_invalid_floats() -> None:
210213
}
211214
expected: dict[str, object] = {
212215
"nan": "NaN",
213-
"nested_nan": {"nan": "NaN", "inf": "Infinity", "-inf": "-Infinity"},
214-
"nan_in_list": ["NaN", "Infinity", "-Infinity"],
216+
"nested": {"nan": "NaN", "inf": "Infinity", "-inf": "-Infinity"},
217+
"in_list": ["NaN", "Infinity", "-Infinity"],
215218
"inf": "Infinity",
216219
"-inf": "-Infinity",
217220
}
218221
observed = replace_json_invalid_floats(data)
219222
assert observed == expected
223+
224+
225+
def test_sanitize_dataset_attributes() -> None:
226+
"""
227+
Check that sanitize_dataset_attributes removes invalid floats from the attributes of an
228+
xarray Dataset, as well as the attributes of the data variables and the coordinate variables.
229+
"""
230+
231+
# Create a dataset with NaN and Infinity values in various attribute locations
232+
ds = xr.Dataset(
233+
data_vars={
234+
"temperature": (
235+
["x", "y"],
236+
np.array([[1.0, 2.0], [3.0, 4.0]]),
237+
{"fill_value": float("nan"), "valid_max": float("inf")},
238+
),
239+
"pressure": (
240+
["x", "y"],
241+
np.array([[10.0, 20.0], [30.0, 40.0]]),
242+
{"fill_value": float("nan"), "valid_min": float("-inf")},
243+
),
244+
},
245+
coords={
246+
"x": (["x"], [0, 1], {"missing_value": float("nan")}),
247+
"y": (["y"], [0, 1], {"scale_factor": 1.0}),
248+
},
249+
attrs={
250+
"global_nan": float("nan"),
251+
"global_inf": float("inf"),
252+
"nested": {"inner_nan": float("nan")},
253+
},
254+
)
255+
256+
# Sanitize the dataset
257+
result = sanitize_dataset_attributes(ds)
258+
259+
# Check dataset-level attributes
260+
assert result.attrs["global_nan"] == "NaN"
261+
assert result.attrs["global_inf"] == "Infinity"
262+
assert result.attrs["nested"]["inner_nan"] == "NaN"
263+
264+
# Check data variable attributes
265+
assert result["temperature"].attrs["fill_value"] == "NaN"
266+
assert result["temperature"].attrs["valid_max"] == "Infinity"
267+
assert result["pressure"].attrs["fill_value"] == "NaN"
268+
assert result["pressure"].attrs["valid_min"] == "-Infinity"
269+
270+
# Check coordinate attributes
271+
assert result.coords["x"].attrs["missing_value"] == "NaN"
272+
assert result.coords["y"].attrs["scale_factor"] == 1.0 # Normal float unchanged
273+
274+
# Verify the original dataset was not modified
275+
assert ds.attrs["global_nan"] != ds.attrs["global_nan"] # NaN != NaN
276+
assert ds["temperature"].attrs["fill_value"] != ds["temperature"].attrs["fill_value"]

0 commit comments

Comments
 (0)