Skip to content
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
16 changes: 10 additions & 6 deletions astroquery/eso/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from astropy.utils.decorators import deprecated_renamed_argument
from bs4 import BeautifulSoup
from pyvo.dal import TAPService
from pyvo.dal.exceptions import DALQueryError, DALFormatError
from pyvo.dal.exceptions import DALQueryError, DALFormatError, DALOverflowWarning

from astroquery import log
from . import conf
Expand Down Expand Up @@ -284,20 +284,24 @@ def message(query_str):
f"For maximum query freedom use the query_tap method:\n\n"
f' >>> Eso().query_tap( "{query_str}", tap_endpoint="{tap_endpoint}")\n\n')

try:
row_limit_plus_one = self.ROW_LIMIT
if self.ROW_LIMIT < conf.MAX_ROW_LIMIT:
row_limit_plus_one = self.ROW_LIMIT + 1
row_limit_plus_one = self.ROW_LIMIT
if self.ROW_LIMIT < conf.MAX_ROW_LIMIT:
row_limit_plus_one = self.ROW_LIMIT + 1

try:
table_with_an_extra_row = tap.search(query=query_str, maxrec=row_limit_plus_one).to_table()
self._maybe_warn_about_table_length(table_with_an_extra_row, row_limit_plus_one)
except DALQueryError:
log.error(message(query_str))
except DALOverflowWarning as ex:
# DALOverflowWarning is raised when the result set is incomplete
# We handle it silently and let _maybe_warn_about_table_length issue the appropriate warning
log.warning(f"Partial result set: {ex}")
except DALFormatError as e:
raise DALFormatError(message(query_str) + f"cause: {e.cause}") from e
except Exception as e:
raise type(e)(f"{e}\n" + message(query_str)) from e

self._maybe_warn_about_table_length(table_with_an_extra_row, row_limit_plus_one)
return table_with_an_extra_row[:self.ROW_LIMIT]

def tap(self, authenticated: bool = False, *, tap_endpoint: str = "tap_obs") -> TAPService:
Expand Down
17 changes: 13 additions & 4 deletions astroquery/eso/tests/test_eso.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,17 +366,26 @@ def test_maxrec():
assert maxrec == EXPECTED_MAX_ROW_LIMIT


def test_retrieve_pyvo_table():
def test_retrieve_pyvo_table(monkeypatch):
eso_instance = Eso()
dal = pyvo.dal.TAPService(eso_instance._tap_url())

q_str = "select * from ivoa.ObsCore"
table = None

# Mock the tap.search method to avoid making remote calls
def mock_search(*args, **kwargs):
from unittest.mock import MagicMock
# Create a mock result that raises DALFormatError
mock_result = MagicMock()
mock_result.to_table.side_effect = pyvo.dal.exceptions.DALFormatError("Test error")
return mock_result

monkeypatch.setattr(dal, 'search', mock_search)

# The method should raise DALFormatError as expected
with pytest.raises(pyvo.dal.exceptions.DALFormatError):
table = eso_instance._try_retrieve_pyvo_table(q_str, dal)

assert table is None


def test_issue_table_length_warnings():
eso_instance = Eso()
Expand Down
63 changes: 46 additions & 17 deletions astroquery/eso/tests/test_eso_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"""

from collections import Counter
import warnings
import pytest

from astropy.table import Table
Expand All @@ -31,6 +32,7 @@
'CRIRESplus',
'ERIS-SPIFFIER',
'GIRAFFE',
'GRAVITY',
'HARPS',
'HAWKI',
'KMOS',
Expand All @@ -43,6 +45,7 @@
]

ONE_RECORD_SURVEYS = [
'109.22XS',
'081.C-0827',
'108.2289',
'1100.A-0528',
Expand Down Expand Up @@ -266,17 +269,28 @@ def test_help(self, instrument):
def test_each_instrument_sgrastar(self, instrument):
eso = Eso()
eso.ROW_LIMIT = 1 # Either we have maxrec results or none at all
try:
with pytest.warns(MaxResultsWarning):
result = eso.query_instrument(instrument,
cone_ra=266.41681662,
cone_dec=-29.00782497,
cone_radius=1.0)
except NoResultsWarning: # we don't care if there are no results
pass
else:
assert isinstance(result, Table)
assert len(result) > 0, f"query_instrument({instrument}) returned no records"

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
result = eso.query_instrument(instrument,
cone_ra=266.41681662,
cone_dec=-29.00782497,
cone_radius=1.0)

# Check if we got any warnings
if len(w) == 0:
raise AssertionError(f"No warnings issued for {instrument}")

warning = w[-1] # Get the last warning
if issubclass(warning.category, NoResultsWarning):
# No results is acceptable
pass
elif issubclass(warning.category, MaxResultsWarning):
# Results were truncated
assert isinstance(result, Table)
assert len(result) > 0, f"query_instrument({instrument}) returned no records"
else:
raise AssertionError(f"Unexpected warning type: {warning.category}")

@pytest.mark.filterwarnings("ignore::pyvo.dal.exceptions.DALOverflowWarning")
def test_each_survey_sgrastar(self, tmp_path):
Expand All @@ -299,19 +313,34 @@ def test_each_survey_sgrastar(self, tmp_path):

assert isinstance(result_s, Table)
assert len(result_s) > 0
else: # survey does not contain SGRA
with pytest.warns(NoResultsWarning):
else: # survey does not contain SGRA (according to the list)
# Some surveys may still have data around Sgr A*, so we handle both cases
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
result_s = eso.query_surveys(surveys=survey, cone_ra=266.41681662,
cone_dec=-29.00782497,
cone_radius=0.1775)

# Check if we got any warnings
if len(w) > 0:
warning = w[-1] # Get the last warning
# Either MaxResultsWarning or NoResultsWarning are acceptable
assert issubclass(warning.category, (MaxResultsWarning, NoResultsWarning)), \
f"Unexpected warning type: {warning.category}"

assert isinstance(result_s, Table)
assert isinstance(result_s, Table)
assert len(result_s) == 0, f"Failed for survey {survey}"

if survey not in ONE_RECORD_SURVEYS: # Expect warnings
with pytest.warns(MaxResultsWarning):
if survey not in ONE_RECORD_SURVEYS: # Might expect warnings
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
generic_result = eso.query_surveys(surveys=survey)

# Check if we got any warnings
if len(w) > 0:
warning = w[-1] # Get the last warning
# Either MaxResultsWarning or NoResultsWarning are acceptable
assert issubclass(warning.category, (MaxResultsWarning, NoResultsWarning)), \
f"Unexpected warning type: {warning.category}"
else: # Do not expect warnings
generic_result = eso.query_surveys(surveys=survey)
assert isinstance(generic_result, Table)
Expand Down
Loading