Skip to content
34 changes: 16 additions & 18 deletions astroquery/esa/euclid/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,23 +754,21 @@ def get_status_messages(self, verbose=False):
flag to display information about the process

"""
try:
sub_context = self.EUCLID_MESSAGES
conn_handler = self._TapPlus__getconnhandler()
response = conn_handler.execute_tapget(sub_context, verbose=verbose)
if response.status == 200:
if isinstance(response, Iterable):
for line in response:

try:
print(line.decode("utf-8").split('=', 1)[1])
except ValueError as e:
print(e)
except IndexError:
print("Archive down for maintenance")

except OSError:
print("Status messages could not be retrieved")
sub_context = self.EUCLID_MESSAGES
conn_handler = self._TapPlus__getconnhandler()
response = conn_handler.execute_tapget(sub_context, verbose=verbose)
if response.status == 200:
if isinstance(response, Iterable):
for line in response:

try:
print(line.decode("utf-8").split('=', 1)[1])
except ValueError as e:
print(e)
except IndexError:
print("Archive down for maintenance")
else:
raise HTTPError(f"Failed to retrieve status messages. HTTP status code: {response.status}")

@staticmethod
def __set_dirs(output_file, observation_id):
Expand Down Expand Up @@ -1601,4 +1599,4 @@ def get_scientific_product_list(self, *, observation_id=None, tile_index=None, c
return job.get_results()


Euclid = EuclidClass()
Euclid = EuclidClass(show_server_messages=False)
7 changes: 7 additions & 0 deletions astroquery/esa/euclid/tests/test_euclid_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,10 @@ def test_get_tables():

table = euclid.load_table("catalogue.mer_catalogue")
assert len(table.columns) == 471


@pytest.mark.remote_data
def test_get_status_messages():
euclid = EuclidClass(show_server_messages=True)
# redundant, but added to be extra-explicit
euclid.get_status_messages()
14 changes: 7 additions & 7 deletions astroquery/esa/euclid/tests/test_euclidtap.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,25 +160,25 @@ def column_attrs():


def test_load_environments():
tap = EuclidClass(environment='PDR')
tap = EuclidClass(environment='PDR', show_server_messages=False)

assert tap is not None

tap = EuclidClass(environment='IDR')
tap = EuclidClass(environment='IDR', show_server_messages=False)

assert tap is not None

tap = EuclidClass(environment='OTF')
tap = EuclidClass(environment='OTF', show_server_messages=False)

assert tap is not None

tap = EuclidClass(environment='REG')
tap = EuclidClass(environment='REG', show_server_messages=False)

assert tap is not None

environment = 'WRONG'
try:
tap = EuclidClass(environment='WRONG')
tap = EuclidClass(environment='WRONG', show_server_messages=False)
except Exception as e:
assert str(e).startswith(f"Invalid environment {environment}. Valid values: {list(conf.ENVIRONMENTS.keys())}")

Expand Down Expand Up @@ -672,7 +672,7 @@ def test_get_product_list_by_tile_index():


def test_get_product_list_errors():
tap = EuclidClass()
tap = EuclidClass(show_server_messages=False)

with pytest.raises(ValueError, match="Missing required argument: 'product_type'"):
tap.get_product_list(observation_id='13', product_type=None)
Expand Down Expand Up @@ -1140,7 +1140,7 @@ def test_get_scientific_data_product_list():


def test_get_scientific_data_product_list_exceptions():
eculid = EuclidClass()
eculid = EuclidClass(show_server_messages=False)

with pytest.raises(ValueError, match="Include a valid parameter to retrieve a LE3 product."):
eculid.get_scientific_product_list(observation_id=None, tile_index=None, category=None, group=None,
Expand Down
15 changes: 6 additions & 9 deletions astroquery/esa/hubble/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,15 +922,12 @@ def get_status_messages(self):
the status of eHST TAP
"""

try:
esautils.execute_servlet_request(
url=conf.EHST_TAP_SERVER + "/" + conf.EHST_MESSAGES,
tap=self.tap,
query_params={},
parser_method=self.parse_messages_response
)
except OSError:
print("Status messages could not be retrieved")
esautils.execute_servlet_request(
url=conf.EHST_TAP_SERVER + "/" + conf.EHST_MESSAGES,
tap=self.tap,
query_params={},
parser_method=self.parse_messages_response
)

def parse_messages_response(self, response):
string_messages = []
Expand Down
8 changes: 8 additions & 0 deletions astroquery/esa/hubble/tests/test_esa_hubble_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from astroquery.esa.hubble import ESAHubble
from astropy import coordinates

# don't show messages during test: it creates a remote call
esa_hubble = ESAHubble(show_messages=False)


Expand Down Expand Up @@ -143,3 +144,10 @@ def test_get_datalabs_path_fits(self, recwarn):
assert len(recwarn) == 1
assert "ib4x04ivq_flt.fits" in str(recwarn[0].message)
assert result == '/data/hub_hstdata_i/i/b4x/04/ib4x04ivq_flt.fits.gz'


@pytest.mark.remote_data
def test_get_status_messages():
esa_hubble = ESAHubble(show_messages=True)
# redundant, but added to be extra-explicit
esa_hubble.get_status_messages()
22 changes: 11 additions & 11 deletions astroquery/esa/jwst/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import zipfile
from datetime import datetime, timezone
from urllib.parse import urlencode
from requests.exceptions import HTTPError

import astroquery.esa.utils.utils as esautils

Expand Down Expand Up @@ -694,16 +695,15 @@ def get_status_messages(self):
the status of JWST TAP
"""

try:
subContext = conf.JWST_MESSAGES
connHandler = self.__jwsttap._TapPlus__getconnhandler()
response = connHandler.execute_tapget(subContext, verbose=False)
if response.status == 200:
for line in response:
string_message = line.decode("utf-8")
print(string_message[string_message.index('=') + 1:])
except OSError:
print("Status messages could not be retrieved")
subContext = conf.JWST_MESSAGES
connHandler = self.__jwsttap._TapPlus__getconnhandler()
response = connHandler.execute_tapget(subContext, verbose=False)
if response.status == 200:
for line in response:
string_message = line.decode("utf-8")
print(string_message[string_message.index('=') + 1:])
else:
raise HTTPError(f"Failed to retrieve status messages. HTTP status code: {response.status}")

def get_product_list(self, *, observation_id=None,
cal_level="ALL",
Expand Down Expand Up @@ -1275,4 +1275,4 @@ def get_decoded_string(str):
return str


Jwst = JwstClass()
Jwst = JwstClass(show_messages=False)
7 changes: 7 additions & 0 deletions astroquery/esa/jwst/tests/test_jwstdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,10 @@ def test_login_error():
with pytest.raises(HTTPError) as err:
jwst.login(user="dummy", password="dummy")
assert "Unauthorized" in err.value.args[0]


@pytest.mark.remote_data
def test_get_status_messages():
jwst = JwstClass(show_messages=True)
# redundant, but added to be extra-explicit
jwst.get_status_messages()
34 changes: 16 additions & 18 deletions astroquery/gaia/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,23 +1168,21 @@ def get_status_messages(self):
"""Retrieve the messages to inform users about
the status of Gaia TAP
"""
try:
sub_context = self.GAIA_MESSAGES
conn_handler = self._TapPlus__getconnhandler()
response = conn_handler.execute_tapget(sub_context, verbose=False)
if response.status == 200:
if isinstance(response, Iterable):
for line in response:

try:
print(line.decode("utf-8").split('=', 1)[1])
except ValueError as e:
print(e)
except IndexError:
print("Archive down for maintenance")

except OSError:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exception catching is a particular problem: it hid from the tests that there is a remote query being performed, since pytest-remotedata raises an OSError

print("Status messages could not be retrieved")
sub_context = self.GAIA_MESSAGES
conn_handler = self._TapPlus__getconnhandler()
response = conn_handler.execute_tapget(sub_context, verbose=False)
if response.status == 200:
if isinstance(response, Iterable):
for line in response:

try:
print(line.decode("utf-8").split('=', 1)[1])
except ValueError as e:
print(e)
except IndexError:
print("Archive down for maintenance")
else:
raise HTTPError(f"Failed to retrieve status messages. HTTP status code: {response.status}")


Gaia = GaiaClass()
Gaia = GaiaClass(show_server_messages=False)
7 changes: 7 additions & 0 deletions astroquery/gaia/tests/test_gaia_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,10 @@ def test_search_async_jobs():
jobfilter.limit = 10
jobs = gaia.search_async_jobs(jobfilter=jobfilter, verbose=True)
assert len(jobs) == 10


@pytest.mark.remote_data
def test_get_status_messages():
gaia = GaiaClass(show_server_messages=True)
# redundant, but added to be extra-explicit
gaia.get_status_messages()
101 changes: 101 additions & 0 deletions astroquery/tests/test_imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import sys
import importlib
import pytest
from unittest.mock import patch
from pytest_remotedata.disable_internet import no_internet

# List of all astroquery modules to test
ASTROQUERY_MODULES = [
'astroquery.alma',
'astroquery.astrometry_net',
'astroquery.besancon',
'astroquery.cadc',
'astroquery.cosmosim',
'astroquery.esa',
'astroquery.esasky',
'astroquery.eso',
'astroquery.exoplanet_orbit_database',
'astroquery.fermi',
'astroquery.gaia',
'astroquery.gama',
'astroquery.gemini',
'astroquery.heasarc',
'astroquery.hitran',
'astroquery.ipac.irsa.ibe',
'astroquery.hips2fits',
'astroquery.image_cutouts',
'astroquery.imcce',
'astroquery.ipac',
'astroquery.ipac.irsa',
'astroquery.ipac.irsa.irsa_dust',
'astroquery.ipac.ned',
'astroquery.ipac.nexsci.nasa_exoplanet_archive',
'astroquery.jplhorizons',
'astroquery.jplsbdb',
'astroquery.jplspec',
'astroquery.linelists',
'astroquery.magpis',
'astroquery.mast',
'astroquery.mocserver',
'astroquery.mpc',
'astroquery.nasa_ads',
'astroquery.nist',
'astroquery.nvas',
'astroquery.oac',
'astroquery.ogle',
'astroquery.open_exoplanet_catalogue',
'astroquery.sdss',
'astroquery.simbad',
'astroquery.skyview',
'astroquery.solarsystem',
'astroquery.splatalogue',
'astroquery.svo_fps',
'astroquery.utils',
'astroquery.vamdc',
'astroquery.vo_conesearch',
'astroquery.vizier',
'astroquery.vsa',
'astroquery.wfau',
'astroquery.xmatch',
]


class SocketTracker:
def __init__(self):
self.socket_attempts = []

def __call__(self, *args, **kwargs):
# Record the attempt
self.socket_attempts.append((args, kwargs))
# Raise a clear error to indicate socket creation
raise RuntimeError("Socket creation attempted during import")


@pytest.mark.parametrize("module_name", ASTROQUERY_MODULES)
def test_no_http_calls_during_import(module_name):
"""
Test that importing astroquery modules does not make any remote calls.

This is a regression test for 3343, and the error that raises is not
properly caught by the framework below, but that's an unrelated issue.

This is the error shown if Gaia(show_server_messages=True) is called:
```
E TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union
```
"""
with no_internet():
if module_name in sys.modules:
del sys.modules[module_name]

tracker = SocketTracker()
with patch('socket.socket', tracker):
importlib.import_module(module_name)

assert not tracker.socket_attempts, (
f"Module {module_name} attempted to create {len(tracker.socket_attempts)} "
"socket(s) during import:\n" + (
"\n".join(f" - {args} {kwargs}" for args, kwargs in tracker.socket_attempts)
)
)
Loading