Skip to content

Commit a424464

Browse files
Add basic checks for errors.
1 parent 769b0db commit a424464

File tree

3 files changed

+81
-8
lines changed

3 files changed

+81
-8
lines changed

astroquery/ipac/irsa/most.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import io
22
import re
33
import tarfile
4+
import warnings
45

56
from bs4 import BeautifulSoup
67

@@ -9,7 +10,7 @@
910

1011
from astroquery.query import BaseQuery
1112
from astroquery.utils import class_or_instance
12-
from astroquery.exceptions import InvalidQueryError
13+
from astroquery.exceptions import InvalidQueryError, NoResultsWarning
1314

1415
from . import conf
1516

@@ -354,6 +355,11 @@ def get_images(self, catalog="wise_merge", input_mode="name_input", ephem_step=0
354355
with_tarballs=True,
355356
)
356357

358+
if queryres is None:
359+
# A warning will already be issued by query_object so no need to
360+
# raise a new one here.
361+
return None
362+
357363
response = self._request("GET", queryres["fits_tarball"],
358364
save=save, savedir=savedir)
359365

@@ -524,10 +530,28 @@ def query_object(self, catalog="wise_merge", input_mode="name_input", output_mod
524530
response = self._request("POST", self.URL,
525531
data=qparams, timeout=self.TIMEOUT)
526532

533+
# service unreachable or some other reason
534+
if not response.ok:
535+
raise response.raise_for_status()
536+
537+
# MOST will not raise an bad response if the query is bad because they
538+
# are not a REST API
539+
if "MOST: *** error:" in response.text:
540+
raise InvalidQueryError(response.text)
541+
542+
# presume that response is HTML to simplify conditions
543+
if "Number of Matched Image Frames = 0" in response.text:
544+
warnings.warn("Number of Matched Image Frames = 0", NoResultsWarning)
545+
return None
546+
527547
if qparams["output_mode"] in ("Brief", "Gator"):
528548
return Table.read(response.text, format="ipac")
529549
elif qparams["output_mode"] == "VOTable":
530-
return votable.parse(io.BytesIO(response.content))
550+
matches = votable.parse(io.BytesIO(response.content))
551+
if matches.get_table_by_index(0).nrows == 0:
552+
warnings.warn("Number of Matched Image Frames = 0", NoResultsWarning)
553+
return None
554+
return matches
531555
else:
532556
return self._parse_full_regular_response(response, qparams["fits_region_files"])
533557

astroquery/ipac/irsa/tests/test_most.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,21 @@
4444
}
4545

4646

47+
class MockResponse(MockResponse):
48+
"""
49+
We overwrite the default MockResponse to provide it wih `ok`
50+
attribute. In these tests it is assumed they are all expected
51+
to pass so it's fine to put it always True.
52+
53+
This is ok, these tests can really only cover parsing errors
54+
because the mocking of failure cases of MOST is too complicated.
55+
These tests are farmed out to remote data tests.
56+
"""
57+
def __init__(self, *args, **kwargs):
58+
super().__init__(*args, **kwargs)
59+
self.ok = True
60+
61+
4762
def data_path(filename):
4863
"""Returns the path to the file in the module's test data directory."""
4964
data_dir = os.path.join(os.path.dirname(__file__), 'data')

astroquery/ipac/irsa/tests/test_most_remote.py

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2-
3-
import io
42
import os
53
import pytest
4+
import warnings
65

76
import numpy as np
87

98
import astropy.io.fits as fits
109
from astropy.table import Table
1110
from astroquery.ipac.irsa import Most
12-
11+
from astroquery.exceptions import InvalidQueryError
1312

1413
# each MOST query is given a PID and a temporary directory availible publicly
1514
# from this base URL. This URL is then used to create links on the returned
@@ -46,8 +45,6 @@ def test_query_object():
4645
# not returned in same order nor header width. So we have to compare
4746
# manually
4847
results = Table.read(data_path("most_regular_results.tbl"), format="ipac")
49-
metadata = Table.read(data_path("most_imgframes_matched_final_table.tbl"), format="ipac")
50-
url = REGULAR_BASE_URL + "ds9region/ds9_orbit_path.reg"
5148

5249
columns = [col for col in results.columns if col not in ("image_url", "postcard_url", "region_file")]
5350
for col in columns:
@@ -89,7 +86,44 @@ def test_list_catalogs():
8986
]
9087

9188
cats = Most.list_catalogs()
92-
9389
assert expected == cats
9490

9591

92+
@pytest.mark.remote_data
93+
def test_no_results():
94+
with warnings.catch_warnings():
95+
warnings.simplefilter("ignore")
96+
response = Most.query_object(
97+
obj_name="Victoria",
98+
obs_begin="2019-05-21",
99+
obs_end="2019-05-22"
100+
)
101+
102+
assert response is None
103+
104+
with warnings.catch_warnings():
105+
warnings.simplefilter("ignore")
106+
response = Most.get_images(
107+
obj_name="Victoria",
108+
obs_begin="2019-05-21",
109+
obs_end="2019-05-22"
110+
)
111+
112+
assert response is None
113+
114+
115+
@pytest.mark.remote_data
116+
def test_invalid_query():
117+
with pytest.raises(InvalidQueryError):
118+
Most.query_object(
119+
obj_name="Victoria",
120+
obs_begin="2014-05-21",
121+
obs_end="2014-05-19"
122+
)
123+
124+
with pytest.raises(InvalidQueryError):
125+
Most.get_images(
126+
obj_name="Victoria",
127+
obs_begin="2014-05-21",
128+
obs_end="2014-05-19"
129+
)

0 commit comments

Comments
 (0)