Skip to content

testing


Utility functions used in tsdat tests.


Functions#

assert_close #

assert_close(
    a: xr.Dataset,
    b: xr.Dataset,
    check_attrs: bool = True,
    check_fill_value: bool = True,
    **kwargs: Any
) -> None

Thin wrapper around xarray.assert_allclose.

Also checks dataset and variable attrs. Removes global attributes that are allowed to be different, which are currently just the 'history' attribute and the 'code_version' attribute. Also handles some obscure edge cases for variable attributes.

Parameters:

Name Type Description Default
a Dataset

The first dataset to compare.

required
b Dataset

The second dataset to compare.

required
check_attrs bool

Check global and variable attributes in addition to the data. Defaults to True.

True
check_fill_value bool

Check the _FillValue attribute. This is a special case because xarray moves the _FillValue from a variable's attributes to its encoding upon saving the dataset. Defaults to True.

True

Source code in tsdat/testing.py
def assert_close(
    a: xr.Dataset,
    b: xr.Dataset,
    check_attrs: bool = True,
    check_fill_value: bool = True,
    **kwargs: Any,
) -> None:
    """---------------------------------------------------------------------------------
    Thin wrapper around xarray.assert_allclose.

    Also checks dataset and variable attrs. Removes global attributes that are allowed
    to be different, which are currently just the 'history' attribute and the
    'code_version' attribute. Also handles some obscure edge cases for variable
    attributes.

    Args:
        a (xr.Dataset): The first dataset to compare.
        b (xr.Dataset): The second dataset to compare.
        check_attrs (bool): Check global and variable attributes in addition to the
            data. Defaults to True.
        check_fill_value (bool): Check the _FillValue attribute. This is a special case
            because xarray moves the _FillValue from a variable's attributes to its
            encoding upon saving the dataset. Defaults to True.

    ---------------------------------------------------------------------------------"""
    a, b = a.copy(), b.copy()  # type: ignore
    _convert_time(a, b)
    xr.testing.assert_allclose(a, b, **kwargs)  # type: ignore
    if check_attrs:
        _check_global_attrs(a, b)
        _check_variable_attrs(a, b, check_fill_value)

get_pydantic_error_message #

get_pydantic_error_message(error: Any) -> str
Source code in tsdat/testing.py
def get_pydantic_error_message(error: Any) -> str:
    return error.getrepr().reprcrash.message

get_pydantic_warning_message #

get_pydantic_warning_message(warning: Any) -> str
Source code in tsdat/testing.py
def get_pydantic_warning_message(warning: Any) -> str:
    warnings: List[str] = [_warning.message.args[0] for _warning in warning.list]
    return "\n".join(warnings)