Skip to content

test: cover utils helpers and xsg accessor; fix deprecations#124

Closed
Divyateja2709 wants to merge 1 commit intoioos:mainfrom
Divyateja2709:tests/utils-and-accessor-coverage
Closed

test: cover utils helpers and xsg accessor; fix deprecations#124
Divyateja2709 wants to merge 1 commit intoioos:mainfrom
Divyateja2709:tests/utils-and-accessor-coverage

Conversation

@Divyateja2709
Copy link
Copy Markdown
Contributor

This PR improves test coverage and reliability by adding unit tests for utility functions and accessor behavior, along with small fixes to ensure correct warnings and robust handling of inputs.

Changes

Tests

  • Added tests/test_utils.py:

    • Covers format_bytes
    • Covers asdatetime (None, datetime, cftime, ISO string)
    • Covers compute_2d_subset_mask (full vs partial coverage)
    • Tests utils.assign_ugrid_topology (deprecation + delegation)
  • Added tests/test_accessor.py:

    • Tests ds.xsg behavior when no grid is recognized:

      • Emits warning
      • subset_polygon and subset_bbox return None
      • subset_vars passthrough
      • has_vertical_levels behavior
  • Updated tests/test_grids/test_sgrid.py:

    • Defines default zarr__version__ = 0 when optional dependencies are missing
    • Prevents test collection failures (NameError)

Fixes / Improvements

  • Deprecation handling

    • Correct use of warnings.warn(...) with proper signature and stacklevel=2
    • Fixed deprecation message to reference assign_ugrid_topology correctly
  • Input robustness

    • normalize_polygon_x_coords now coerces inputs using np.asarray(poly)
    • Ensures consistent behavior for list/tuple polygon inputs

Why this matters

  • Improves coverage of previously untested core utilities
  • Ensures consistent and predictable accessor behavior in edge cases
  • Fixes incorrect deprecation warnings so users receive proper guidance
  • Prevents CI/test failures when optional dependencies are unavailable
  • Strengthens overall reliability and maintainability of the package

Notes

  • This PR focuses on tests and small reliability fixes only
  • Independent of README/documentation PR (docs/readme-quickstart-fixes)

- Add tests for format_bytes, asdatetime, compute_2d_subset_mask, and
  utils.assign_ugrid_topology forwarding with DeprecationWarning
- Add accessor tests for unknown-grid warning and None subset behavior
- Fix warnings.warn argument order in assign_ugrid_topology; correct message
- Coerce polygon coords via np.asarray in normalize_polygon_x_coords
- Define zarr__version__ when fsspec/zarr import fails (test collection)

Made-with: Cursor
Copy link
Copy Markdown
Contributor

@ChrisBarker-NOAA ChrisBarker-NOAA left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thanks!

A couple small comments to addresss, and then I'll merge.

from xarray_subset_grid.grids.sgrid import _get_location_info_from_topology

# open dataset as zarr object using fsspec reference file system and xarray
zarr__version__ = 0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to check the status of zarr3 and FSpec AWS -- this may be a non_issue now.

Comment thread tests/test_accessor.py
ds = xr.Dataset({"a": (("x",), [1, 2, 3])})
with pytest.warns(UserWarning, match="no grid type"):
out = ds.xsg.subset_vars(["a"])
# Without a recognized grid, subset_vars returns the dataset unchanged.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm -- this might take a bit of thought -- I think it should simply raise an Error.

There is no use-case for subset_vars without a grid, and having it be do-nothing might be more surprising.

Comment thread tests/test_utils.py
import xarray as xr

from tests.conftest import EXAMPLE_DATA
from xarray_subset_grid import utils as xsg_utils
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should either import the whole utils module, or import a bunch of names, but not both.

Comment thread tests/test_utils.py
assert unit in format_bytes(num)


def test_asdatetime_none():
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! I actually copied that ober from another project and forgot to bring the tests over -- so this is nice.

Comment thread tests/test_utils.py
poly = np.array([(-75.0, 39.0), (-69.0, 39.0), (-69.0, 45.0), (-75.0, 45.0)])
mask = compute_2d_subset_mask(lat_da, lon_da, poly)
assert mask.dims == ("y", "x")
assert bool(mask.all())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need the bool() call here -- .all() returns a numpy bool, which works fine. (I think assert probably calls bool() anyway).

Comment thread tests/test_utils.py
assert bool(mask.all())


def test_compute_2d_subset_mask_partial():
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be nice to have a simple test case that checks that the mask is actually correct.

the actual point-in-polygon code should be tested elswhere, but good to know it's being applied properly -- at least on one example.

Comment thread tests/test_utils.py

def test_assign_ugrid_topology_utils_deprecation_wrapper():
nc = EXAMPLE_DATA / "SFBOFS_subset1.nc"
if not nc.is_file():
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should either make sure it's there or not -- semi-silently skipping is not good.

Comment thread tests/test_utils.py
if not nc.is_file():
pytest.skip("example NetCDF not present")
ds = xr.open_dataset(nc)
with pytest.warns(DeprecationWarning, match="assign_ugrid_topology"):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, let's jsut get rid of that Depricated Function -- and make sure that none of the examples use it!

#129

x (np.array): x-coordinates of the vertices
poly (np.array): polygon vertices
"""
poly = np.asarray(poly)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, thanks!

Divyateja2709 added a commit to Divyateja2709/xarray-subset-grid that referenced this pull request Apr 5, 2026
- Remove deprecated assign_ugrid_topology from utils (use grids.ugrid only)

- Raise ValueError from xsg.subset_vars when no grid is recognized

- test_utils: single utils import style; stronger mask checks; list polygon test

- test_accessor: expect ValueError for subset_vars without grid

- test_sgrid: drop zarr>=3 skip for online kerchunk test (per review)

Made-with: Cursor
ChrisBarker-NOAA pushed a commit that referenced this pull request Apr 6, 2026
…havior, tests) (#130)

* test: cover utils helpers and xsg accessor; fix deprecations

- Add tests for format_bytes, asdatetime, compute_2d_subset_mask, and
  utils.assign_ugrid_topology forwarding with DeprecationWarning
- Add accessor tests for unknown-grid warning and None subset behavior
- Fix warnings.warn argument order in assign_ugrid_topology; correct message
- Coerce polygon coords via np.asarray in normalize_polygon_x_coords
- Define zarr__version__ when fsspec/zarr import fails (test collection)

Made-with: Cursor

* fix: address PR #124 review (subset_vars, utils cleanup, tests)

- Remove deprecated assign_ugrid_topology from utils (use grids.ugrid only)

- Raise ValueError from xsg.subset_vars when no grid is recognized

- test_utils: single utils import style; stronger mask checks; list polygon test

- test_accessor: expect ValueError for subset_vars without grid

- test_sgrid: drop zarr>=3 skip for online kerchunk test (per review)

Made-with: Cursor
@ChrisBarker-NOAA
Copy link
Copy Markdown
Contributor

Add fixed by #130

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants