Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for load_dataarray to increase code coverage #1518

Merged
merged 2 commits into from
Sep 18, 2021

Conversation

weiji14
Copy link
Member

@weiji14 weiji14 commented Sep 17, 2021

Description of proposed changes

Patches missing code coverage of pygmt.load_dataarray function wrapped in #1439. This commit was cherry-picked from test_io_load_dataarray in #1494.

Fixes #

Reminders

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.
  • Add new public functions/methods/classes to doc/api/index.rst.
  • Write detailed docstrings for all functions/methods.
  • If adding new functionality, add an example to docstrings or tutorials.

Slash Commands

You can write slash commands (/command) in the first line of a comment to perform
specific operations. Supported slash commands are:

  • /format: automatically format and lint the code
  • /test-gmt-dev: run full tests on the latest GMT development version

@weiji14 weiji14 added maintenance Boring but important stuff for the core devs skip-changelog Skip adding Pull Request to changelog labels Sep 17, 2021
@weiji14 weiji14 added this to the 0.5.0 milestone Sep 17, 2021
@weiji14 weiji14 self-assigned this Sep 17, 2021
@weiji14 weiji14 marked this pull request as ready for review September 17, 2021 09:47
Comment on lines +11 to +26
def test_io_load_dataarray():
"""
Check that load_dataarray works to read a NetCDF grid with
GMTDataArrayAccessor information loaded.
"""
with GMTTempFile(suffix=".nc") as tmpfile:
grid = xr.DataArray(
data=np.random.rand(2, 2), coords=[[0.1, 0.2], [0.3, 0.4]], dims=("x", "y")
)
grid.to_netcdf(tmpfile.name)
dataarray = load_dataarray(tmpfile.name)
assert dataarray.gmt.gtype == 0 # Cartesian grid
assert dataarray.gmt.registration == 1 # Pixel registration
# this would fail if we used xr.open_dataarray instead of
# load_dataarray
dataarray.to_netcdf(tmpfile.name)
Copy link
Member

Choose a reason for hiding this comment

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

Can you clarify why we need this test? To me, it seems redundant with the other tests that read NetCDF grids with GMTDataArrayAccessor information loaded with the exception of the to_netcdf line, but that is xarray functionality rather than pygmt.

Copy link
Member Author

Choose a reason for hiding this comment

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

This test was based on xarray's similar test at https://github.com/pydata/xarray/blob/befd1b98bd84047d62307419a30bcda7a0727926/xarray/tests/test_backends.py#L3729-L3736. I agree that this doesn't increase test coverage and seems redundant, but I feel like we should still have an explicit unit test for load_dataarray here, rather than just testing it indirectly in other functions.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good

@weiji14 weiji14 added the final review call This PR requires final review and approval from a second reviewer label Sep 17, 2021
@weiji14 weiji14 merged commit f7d5526 into main Sep 18, 2021
@weiji14 weiji14 deleted the test_load_dataarray branch September 18, 2021 21:30
@weiji14 weiji14 removed the final review call This PR requires final review and approval from a second reviewer label Sep 20, 2021
sixy6e pushed a commit to sixy6e/pygmt that referenced this pull request Dec 21, 2022
…gTools#1518)

Patches missing code coverage of pygmt.load_dataarray
function wrapped in GenericMappingTools#1439. Checks that a NetCDF grid
can be read with GMTDataArrayAccessor information loaded,
and that load_dataarray fails when the cache argument is used.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Boring but important stuff for the core devs skip-changelog Skip adding Pull Request to changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants