1+ # Licensed under a 3-clause BSD style license - see LICENSE.rst
2+ import sys
3+ import importlib
4+ import pytest
5+ import socket
6+ from unittest .mock import patch
7+ from pytest_remotedata .disable_internet import no_internet
8+ from pytest_remotedata .disable_internet import INTERNET_OFF
9+
10+ # List of all astroquery modules to test
11+ ASTROQUERY_MODULES = [
12+ 'astroquery.alma' ,
13+ 'astroquery.astrometry_net' ,
14+ 'astroquery.besancon' ,
15+ 'astroquery.cadc' ,
16+ 'astroquery.cosmosim' ,
17+ 'astroquery.esa' ,
18+ 'astroquery.esasky' ,
19+ 'astroquery.eso' ,
20+ 'astroquery.exoplanet_orbit_database' ,
21+ 'astroquery.fermi' ,
22+ 'astroquery.gaia' ,
23+ 'astroquery.gama' ,
24+ 'astroquery.gemini' ,
25+ 'astroquery.heasarc' ,
26+ 'astroquery.hitran' ,
27+ 'astroquery.ipac.irsa.ibe' ,
28+ 'astroquery.hips2fits' ,
29+ 'astroquery.image_cutouts' ,
30+ 'astroquery.imcce' ,
31+ 'astroquery.ipac' ,
32+ 'astroquery.ipac.irsa' ,
33+ 'astroquery.ipac.irsa.irsa_dust' ,
34+ 'astroquery.ipac.ned' ,
35+ 'astroquery.ipac.nexsci.nasa_exoplanet_archive' ,
36+ 'astroquery.jplhorizons' ,
37+ 'astroquery.jplsbdb' ,
38+ 'astroquery.jplspec' ,
39+ 'astroquery.linelists' ,
40+ 'astroquery.magpis' ,
41+ 'astroquery.mast' ,
42+ 'astroquery.mocserver' ,
43+ 'astroquery.mpc' ,
44+ 'astroquery.nasa_ads' ,
45+ 'astroquery.nist' ,
46+ 'astroquery.nvas' ,
47+ 'astroquery.oac' ,
48+ 'astroquery.ogle' ,
49+ 'astroquery.open_exoplanet_catalogue' ,
50+ 'astroquery.sdss' ,
51+ 'astroquery.simbad' ,
52+ 'astroquery.skyview' ,
53+ 'astroquery.solarsystem' ,
54+ 'astroquery.splatalogue' ,
55+ 'astroquery.svo_fps' ,
56+ 'astroquery.utils' ,
57+ 'astroquery.vamdc' ,
58+ 'astroquery.vo_conesearch' ,
59+ 'astroquery.vizier' ,
60+ 'astroquery.vsa' ,
61+ 'astroquery.wfau' ,
62+ 'astroquery.xmatch' ,
63+ ]
64+
65+ class SocketTracker :
66+ def __init__ (self ):
67+ self .socket_attempts = []
68+
69+ def __call__ (self , * args , ** kwargs ):
70+ # Record the attempt
71+ self .socket_attempts .append ((args , kwargs ))
72+ # Raise a clear error to indicate socket creation
73+ raise RuntimeError ("Socket creation attempted during import" )
74+
75+
76+ @pytest .mark .parametrize ("module_name" , ASTROQUERY_MODULES )
77+ def test_no_http_calls_during_import (module_name ):
78+ """
79+ Test that importing astroquery modules does not make any remote calls.
80+
81+ This is a regression test for 3343, and the error that raises is not
82+ properly caught by the framework below, but that's an unrelated issue.
83+
84+ This is the error shown if Gaia(show_server_messages=True) is called:
85+ ```
86+ E TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union
87+ ```
88+ """
89+ with no_internet ():
90+ if module_name in sys .modules :
91+ del sys .modules [module_name ]
92+
93+ tracker = SocketTracker ()
94+ with patch ('socket.socket' , tracker ):
95+ importlib .import_module (module_name )
96+
97+ assert not tracker .socket_attempts , (
98+ f"Module { module_name } attempted to create { len (tracker .socket_attempts )} "
99+ f"socket(s) during import:\n " +
100+ "\n " .join (f" - { args } { kwargs } " for args , kwargs in tracker .socket_attempts )
101+ )
0 commit comments