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

Fixed formatter in IntervalQueryParam #225

Merged
merged 2 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions pyvo/dal/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def __init__(self, unit=None, equivalencies=None):
super().__init__()

def get_dal_format(self, val):
if isinstance(val, tuple):
if isinstance(val, (tuple, Quantity)):
if len(val) == 1:
high = low = val[0]
elif len(val) == 2:
Expand All @@ -391,16 +391,21 @@ def get_dal_format(self, val):
format(val))
else:
high = low = val
if isinstance(low, (int, float)) and isinstance(high, (int, float))\
and low > high:
raise ValueError('Invalid interval: min({}) > max({})'.format(
low, high))
if self._unit:
if not isinstance(low, Quantity):
low = low*self._unit
low = low.to(self._unit, equivalencies=self._equivalencies).value
if not isinstance(high, Quantity):
high = high*self._unit
high = high.to(self._unit, equivalencies=self._equivalencies).value
if low > high:
raise ValueError('Invalid interval: min({}) > max({})'.format(
low, high))

if low > high:
# interval could become invalid during transform (e.g. GHz->m)
low, high = high, low

return '{} {}'.format(low, high)

Expand Down
17 changes: 16 additions & 1 deletion pyvo/dal/tests/test_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from pyvo.dal.adhoc import DatalinkResults
from pyvo.dal.params import find_param_by_keyword, get_converter,\
AbstractDalQueryParam
AbstractDalQueryParam, IntervalQueryParam
from pyvo.dal.exceptions import DALServiceError

import pytest
Expand Down Expand Up @@ -207,3 +207,18 @@ def get_dal_format(self, item):
test_obs.clear()
assert len(test_obs) == 0
assert len(test_obs.dal) == 0


def test_dal_format():
iqp = IntervalQueryParam(unit=u.m, equivalencies=u.spectral())
assert '1.0 1.0' == iqp.get_dal_format(1)
assert '1.0 2.0' == iqp.get_dal_format((1, 2))
assert '1.0 2.0' == iqp.get_dal_format((100*u.cm, 200*u.cm))
assert '1.0 2.0' == iqp.get_dal_format((100, 200)*u.cm)
assert '0.14989622900000002 1.0' == iqp.get_dal_format((100*u.cm, 2*u.GHz))
assert '14.9896229 29.9792458' == iqp.get_dal_format((0.01, 0.02)*u.GHz)
# Quantity intervals are corrected in terms of min and max ..
assert '1.0 2.0' == iqp.get_dal_format((2, 1)*u.m)
# But unitless intervals are not
with pytest.raises(ValueError):
iqp.get_dal_format((2, 1))