Skip to content

Commit

Permalink
add more corner case tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ocefpaf committed Feb 26, 2024
1 parent 26e5133 commit e62ced9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
9 changes: 7 additions & 2 deletions erddapy/core/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@ def _sort_url(url):
"""
parts = parse.urlparse(url)
if parts.query:
variables, constraints = parts.query.split("&", maxsplit=1)
query = parts.query.split("&", maxsplit=1)
if len(query) == 1:
variables = parts.query
constraints = ""
else:
variables, constraints = parts.query.split("&", maxsplit=1)
sorted_variables = ",".join(sorted(variables.split(",")))
sorted_query = OrderedDict(sorted(dict(parse.parse_qsl(constraints)).items()))
sorted_query_str = parse.unquote(parse.urlencode(sorted_query))
sorted_url = f"{parts.scheme}://{parts.netloc}{parts.path}?{parts.params}{sorted_variables}&{sorted_query_str}{parts.fragment}"
else:
sorted_url = url
return sorted_url
return sorted_url.strip("&")


@functools.lru_cache(maxsize=128)
Expand Down
28 changes: 27 additions & 1 deletion tests/test_to_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,35 @@ def test_download_file(dataset_tabledap):


@pytest.mark.web
def test_download_file_undefined_query(dataset_tabledap):
def test_download_file_variables_only(dataset_tabledap):
"""Test direct download of tabledap dataset with undefined constraints."""
dataset_tabledap.constraints = {}
fn = dataset_tabledap.download_file("nc")
ds = xr.load_dataset(fn)
assert ds["time"].name == "time"
assert ds["temperature"].name == "temperature"
dataset_tabledap.variables = dataset_tabledap.variables[::-1]
fn_new = dataset_tabledap.download_file("nc")
assert fn_new == fn


@pytest.mark.web
def test_download_file_constraints_only(dataset_tabledap):
"""Test direct download of tabledap dataset with undefined constraints."""
dataset_tabledap.variables = []
fn = dataset_tabledap.download_file("nc")
ds = xr.load_dataset(fn)
assert ds["time"].name == "time"
assert ds["temperature"].name == "temperature"
dataset_tabledap.variables = dataset_tabledap.variables[::-1]
fn_new = dataset_tabledap.download_file("nc")
assert fn_new == fn


@pytest.mark.web
def test_download_file_undefined_query(dataset_tabledap):
"""Test direct download of tabledap dataset with undefined variables and constraints."""
dataset_tabledap.variables = []
dataset_tabledap.constraints = {}
fn = dataset_tabledap.download_file("nc")
ds = xr.load_dataset(fn)
Expand Down
25 changes: 24 additions & 1 deletion tests/test_url_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import httpx
import pytest

from erddapy.core.url import check_url_response, urlopen
from erddapy.core.url import _sort_url, check_url_response, urlopen


@pytest.mark.web
Expand Down Expand Up @@ -39,3 +39,26 @@ def test_check_url_response():
)
with pytest.raises(httpx.HTTPError):
check_url_response(bad_request)


def test__sort_url():
url = "https://erddap.sensors.ioos.us/erddap/tabledap/amelia_20180501t0000.nc?time,temperature&time>=1525737600.0&time<=1526245200.0&latitude>=36&latitude<=38&longitude>=-76&longitude<=-73"
expected = "https://erddap.sensors.ioos.us/erddap/tabledap/amelia_20180501t0000.nc?temperature,time&latitude<=38&latitude>=36&longitude<=-73&longitude>=-76&time<=1526245200.0&time>=1525737600.0"
assert _sort_url(url) == expected


def test__sort_url_variables_only():
url = "https://erddap.sensors.ioos.us/erddap/tabledap/amelia_20180501t0000.nc?&time>=1525737600.0&time<=1526245200.0&latitude>=36&latitude<=38&longitude>=-76&longitude<=-73"
expected = "https://erddap.sensors.ioos.us/erddap/tabledap/amelia_20180501t0000.nc?&latitude<=38&latitude>=36&longitude<=-73&longitude>=-76&time<=1526245200.0&time>=1525737600.0"
assert _sort_url(url) == expected


def test__sort_url_constraints_only():
url = "https://erddap.sensors.ioos.us/erddap/tabledap/amelia_20180501t0000.nc?time,temperature"
expected = "https://erddap.sensors.ioos.us/erddap/tabledap/amelia_20180501t0000.nc?temperature,time"
assert _sort_url(url) == expected


def test__sort_url_undefined_query():
url = "https://erddap.sensors.ioos.us/erddap/tabledap/amelia_20180501t0000.nc?"
assert _sort_url(url) == url

0 comments on commit e62ced9

Please sign in to comment.