generated from ioos/ioos-python-package-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from noaaroland/quote
Some fixes for OPeNDAP attributes
- Loading branch information
Showing
8 changed files
with
179 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"""Py.test configuration and shared fixtures.""" | ||
|
||
from pathlib import Path | ||
|
||
import pytest | ||
from xprocess import ProcessStarter | ||
|
||
server_path = Path(__file__).parent / "server.py" | ||
|
||
|
||
@pytest.fixture | ||
def xpublish_server(xprocess): | ||
"""Launch an Xpublish server in the background. | ||
Server has the air_temperature tutorial dataset | ||
at `air` and has the OpenDAP plugin running with | ||
defaults. | ||
""" | ||
|
||
class Starter(ProcessStarter): | ||
# Wait till the pattern is printed before | ||
# considering things started | ||
pattern = "Uvicorn running on" | ||
|
||
# server startup args | ||
args = ["python", str(server_path)] | ||
|
||
# seconds before timing out on server startup | ||
timeout = 30 | ||
|
||
# Try to cleanup if inturrupted | ||
terminate_on_interrupt = True | ||
|
||
xprocess.ensure("xpublish", Starter) | ||
yield "http://0.0.0.0:9000" | ||
xprocess.getinfo("xpublish").terminate() | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def dataset(): | ||
"""Xarray air temperature tutorial dataset.""" | ||
from xarray.tutorial import open_dataset | ||
|
||
ds = open_dataset("air_temperature") | ||
|
||
return ds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"""Test OpenDAP server with air temperature dataset.""" | ||
|
||
import numpy as np | ||
import xarray.tutorial | ||
import xpublish | ||
|
||
from xpublish_opendap import OpenDapPlugin | ||
|
||
ds = xarray.tutorial.open_dataset("air_temperature") | ||
|
||
ds_attrs_quote = xarray.tutorial.open_dataset("air_temperature") | ||
ds_attrs_quote.attrs["quotes"] = 'This attribute uses "quotes"' | ||
ds_attrs_cast = xarray.tutorial.open_dataset("air_temperature") | ||
ds_attrs_cast.attrs["npint"] = np.int16(16) | ||
ds_attrs_cast.attrs["npintthirtytwo"] = np.int32(32) | ||
|
||
rest = xpublish.Rest( | ||
{"air": ds, "attrs_quote": ds_attrs_quote, "attrs_cast": ds_attrs_cast}, | ||
plugins={"opendap": OpenDapPlugin()}, | ||
) | ||
|
||
rest.serve() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
"""Test OpenDAP clients against Xpublish OpenDAP plugin. | ||
Live tests are currently failing on Windows, see: | ||
- https://github.com/Unidata/netcdf-c/issues/2459 | ||
- https://github.com/Unidata/netcdf4-python/issues/1246 | ||
- https://github.com/pydata/xarray/issues/7773 | ||
""" | ||
|
||
import sys | ||
|
||
import netCDF4 | ||
import pytest | ||
import xarray as xr | ||
|
||
|
||
@pytest.mark.skipif( | ||
sys.platform == "win32", | ||
reason="NetCDF4 is failing on Windows Github Actions workers", | ||
) | ||
def test_netcdf4(xpublish_server): | ||
"""Test opening OpenDAP air dataset directly with NetCDF4 library.""" | ||
url = f"{xpublish_server}/datasets/air/opendap" | ||
netCDF4.Dataset(url) | ||
|
||
|
||
@pytest.mark.skipif( | ||
sys.platform == "win32", | ||
reason="NetCDF4 is failing on Windows Github Actions workers", | ||
) | ||
def test_default_xarray_engine(xpublish_server, dataset): | ||
"""Test opening OpenDAP air dataset with default Xarray engine.""" | ||
url = f"{xpublish_server}/datasets/air/opendap" | ||
ds = xr.open_dataset(url) | ||
assert ds == dataset | ||
|
||
|
||
@pytest.mark.skipif( | ||
sys.platform == "win32", | ||
reason="NetCDF4 is failing on Windows Github Actions workers", | ||
) | ||
@pytest.mark.parametrize( | ||
"engine", | ||
[ | ||
"netcdf4", | ||
# "h5netcdf", # fails with 404 not found | ||
# "pydap" # fails with incomplete read | ||
], | ||
) | ||
def test_xarray_engines(xpublish_server, engine, dataset): | ||
"""Test opening OpenDAP dataset with specified engines.""" | ||
url = f"{xpublish_server}/datasets/air/opendap" | ||
ds = xr.open_dataset(url, engine=engine) | ||
assert ds == dataset | ||
|
||
|
||
@pytest.mark.skipif( | ||
sys.platform == "win32", | ||
reason="NetCDF4 is failing on Windows Github Actions workers", | ||
) | ||
def test_attrs_quotes(xpublish_server): | ||
"""Test that we are formatting OpenDAP attributes that contain '"' properly.""" | ||
url = f"{xpublish_server}/datasets/attrs_quote/opendap" | ||
ds = xr.open_dataset(url) | ||
|
||
assert ds.attrs["quotes"] == 'This attribute uses "quotes"' | ||
|
||
|
||
@pytest.mark.skipif( | ||
sys.platform == "win32", | ||
reason="NetCDF4 is failing on Windows Github Actions workers", | ||
) | ||
def test_attrs_types(xpublish_server): | ||
"""Test that we are formatting OpenDAP attributes that contain '"' properly.""" | ||
url = f"{xpublish_server}/datasets/attrs_cast/opendap" | ||
ds = xr.open_dataset(url) | ||
|
||
assert ds.attrs["npint"] == 16 | ||
assert ds.attrs["npintthirtytwo"] == 32 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters