Skip to content

Commit 44cf4ba

Browse files
committed
TST: add more test coverage
1 parent 464b9f6 commit 44cf4ba

File tree

3 files changed

+60
-61
lines changed

3 files changed

+60
-61
lines changed

astroquery/ipac/irsa/core.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def query_tap(self, query, *, maxrec=None):
5959
"""
6060
log.debug(f'TAP query: {query}')
6161
return self.tap.search(query, language='ADQL', maxrec=maxrec)
62+
6263
def query_region(self, coordinates=None, *, catalog=None, spatial='Cone',
6364
radius=10 * u.arcsec, width=None, polygon=None,
6465
get_query_payload=False, selcols=None,
@@ -77,13 +78,13 @@ def query_region(self, coordinates=None, *, catalog=None, spatial='Cone',
7778
be resolved on the server (see `here
7879
<https://irsa.ipac.caltech.edu/search_help.html>`_ for more
7980
details). Required if spatial is ``'Cone'`` or ``'Box'``. Optional
80-
if spatial is ``'Polygon'``.
81+
if spatial is ``'Polygon'`` or ``'All-Sky'``.
8182
catalog : str
8283
The catalog to be used. To list the available catalogs, use
8384
:meth:`~astroquery.ipac.irsa.IrsaClass.print_catalogs`.
8485
spatial : str
8586
Type of spatial query: ``'Cone'``, ``'Box'``, ``'Polygon'``, and
86-
``'All-Sky'``. If missing then defaults to ``'Cone'``.
87+
``'All-Sky'``. Defaults to ``'Cone'``.
8788
radius : str or `~astropy.units.Quantity` object, [optional for spatial is ``'Cone'``]
8889
The string must be parsable by `~astropy.coordinates.Angle`. The
8990
appropriate `~astropy.units.Quantity` object from
@@ -119,19 +120,10 @@ def query_region(self, coordinates=None, *, catalog=None, spatial='Cone',
119120
else:
120121
columns = selcols
121122

122-
adql = f'SELECT {columns} from {catalog}'
123-
124-
coords_icrs = parse_coordinates(coordinates).icrs
125-
ra, dec = coords_icrs.ra.deg, coords_icrs.dec.deg
123+
adql = f'SELECT {columns} FROM {catalog}'
126124

127125
if spatial == 'All-Sky':
128126
where = ''
129-
elif spatial == 'Cone':
130-
where = (" WHERE CONTAINS(POINT('ICRS',ra,dec),"
131-
f"CIRCLE('ICRS',{ra},{dec},{radius.to(u.deg).value}))=1")
132-
elif spatial == 'Box':
133-
where = (" WHERE CONTAINS(POINT('ICRS',ra,dec),"
134-
f"BOX('ICRS',{ra},{dec},{width.to(u.deg).value},{width.to(u.deg).value}))=1")
135127
elif spatial == 'Polygon':
136128
try:
137129
coordinates_list = [parse_coordinates(coord).icrs for coord in polygon]
@@ -147,10 +139,18 @@ def query_region(self, coordinates=None, *, catalog=None, spatial='Cone',
147139
coordinates_str = [f'{coord.ra.deg},{coord.dec.deg}' for coord in coordinates_list]
148140
where = (" WHERE CONTAINS(POINT('ICRS',ra,dec),"
149141
f"POLYGON('ICRS',{','.join(coordinates_str)}))=1")
150-
151142
else:
152-
raise ValueError("Unrecognized spatial query type. Must be one of "
153-
"'Cone', 'Box', 'Polygon', or 'All-Sky'.")
143+
coords_icrs = parse_coordinates(coordinates).icrs
144+
ra, dec = coords_icrs.ra.deg, coords_icrs.dec.deg
145+
if spatial == 'Cone':
146+
where = (" WHERE CONTAINS(POINT('ICRS',ra,dec),"
147+
f"CIRCLE('ICRS',{ra},{dec},{radius.to(u.deg).value}))=1")
148+
elif spatial == 'Box':
149+
where = (" WHERE CONTAINS(POINT('ICRS',ra,dec),"
150+
f"BOX('ICRS',{ra},{dec},{width.to(u.deg).value},{width.to(u.deg).value}))=1")
151+
else:
152+
raise ValueError("Unrecognized spatial query type. Must be one of "
153+
"'Cone', 'Box', 'Polygon', or 'All-Sky'.")
154154

155155
adql += where
156156

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,32 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
22

3-
import os
4-
import re
5-
import numpy as np
6-
73
import pytest
84
from astropy.coordinates import SkyCoord
95
from astropy.table import Table
106
import astropy.units as u
117

12-
from astroquery.utils.mocks import MockResponse
13-
from astroquery.ipac.irsa import Irsa, conf
14-
from astroquery.ipac import irsa
15-
16-
DATA_FILES = {'Cone': 'Cone.xml',
17-
'Box': 'Box.xml',
18-
'Polygon': 'Polygon.xml'}
8+
from astroquery.ipac.irsa import Irsa
9+
from astroquery.exceptions import InvalidQueryError
1910

2011
OBJ_LIST = ["m31", "00h42m44.330s +41d16m07.50s",
2112
SkyCoord(l=121.1743 * u.deg, b=-21.5733 * u.deg, frame="galactic")]
2213

2314

24-
def data_path(filename):
25-
data_dir = os.path.join(os.path.dirname(__file__), 'data')
26-
return os.path.join(data_dir, filename)
27-
28-
29-
@pytest.fixture
30-
def patch_get(request):
31-
mp = request.getfixturevalue("monkeypatch")
32-
33-
mp.setattr(Irsa, '_request', get_mockreturn)
34-
return mp
35-
36-
37-
def get_mockreturn(method, url, params=None, timeout=10, cache=False, **kwargs):
38-
filename = data_path(DATA_FILES[params['spatial']])
39-
with open(filename, 'rb') as infile:
40-
content = infile.read()
41-
return MockResponse(content, **kwargs)
42-
43-
44-
#def test_args_to_payload():
45-
# out = Irsa._args_to_payload("fp_psc")
46-
# assert out == dict(catalog='fp_psc', outfmt=3, outrows=conf.row_limit,
47-
# selcols='')
48-
49-
5015
@pytest.mark.parametrize(("coordinates"), OBJ_LIST)
51-
def test_query_region_cone(coordinates, patch_get):
52-
result = Irsa.query_region(
53-
coordinates, catalog='fp_psc', spatial='Cone', radius=2 * u.arcmin)
16+
def test_query_region_cone(coordinates):
17+
query = Irsa.query_region(coordinates, catalog='fp_psc', spatial='Cone', radius=2 * u.arcmin,
18+
get_query_payload=True)
5419

55-
assert isinstance(result, Table)
20+
# We don't fully float compare in this string, there are slight differences due to the name-coordinate
21+
# resolution and conversions
22+
assert "SELECT * FROM fp_psc WHERE CONTAINS(POINT('ICRS',ra,dec),CIRCLE('ICRS',10.68" in query
23+
assert ",41.26" in query
24+
assert ",0.0333" in query
5625

5726

5827
@pytest.mark.skip("Upstream TAP doesn't support Box geometry yet")
5928
@pytest.mark.parametrize("coordinates", OBJ_LIST)
60-
def test_query_region_box(coordinates, patch_get):
29+
def test_query_region_box(coordinates):
6130
result = Irsa.query_region(
6231
coordinates, catalog='fp_psc', spatial='Box', width=2 * u.arcmin)
6332

@@ -72,10 +41,22 @@ def test_query_region_box(coordinates, patch_get):
7241

7342

7443
@pytest.mark.parametrize("polygon", [poly1, poly2])
75-
def test_query_region_polygon(polygon, patch_get):
76-
result = Irsa.query_region("m31", catalog="fp_psc", spatial="Polygon", polygon=polygon)
44+
def test_query_region_polygon(polygon):
45+
query1 = Irsa.query_region(catalog="fp_psc", spatial="Polygon", polygon=polygon,
46+
get_query_payload=True)
47+
query2 = Irsa.query_region("m31", catalog="fp_psc", spatial="Polygon", polygon=polygon,
48+
get_query_payload=True)
49+
50+
assert query1 == query2
51+
assert query1 == ("SELECT * FROM fp_psc "
52+
"WHERE CONTAINS(POINT('ICRS',ra,dec),POLYGON('ICRS',10.1,10.1,10.0,10.1,10.0,10.0))=1")
7753

78-
assert isinstance(result, Table)
54+
55+
def test_query_allsky():
56+
query1 = Irsa.query_region(catalog="fp_psc", spatial="All-Sky", get_query_payload=True)
57+
query2 = Irsa.query_region("m31", catalog="fp_psc", spatial="All-Sky", get_query_payload=True)
58+
59+
assert query1 == query2 == "SELECT * FROM fp_psc"
7960

8061

8162
@pytest.mark.parametrize('spatial', ['cone', 'box', 'polygon', 'all-Sky', 'All-sky', 'invalid'])
@@ -84,6 +65,11 @@ def test_spatial_invalid(spatial):
8465
Irsa.query_region("m31", catalog='invalid_spatial', spatial=spatial)
8566

8667

68+
def test_no_catalog():
69+
with pytest.raises(InvalidQueryError):
70+
Irsa.query_region("m31", spatial='Cone')
71+
72+
8773
def test_deprecated_namespace_import_warning():
8874
with pytest.warns(DeprecationWarning):
8975
import astroquery.irsa # noqa: F401

astroquery/ipac/irsa/tests/test_irsa_remote.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
22

3-
43
import pytest
54
import astropy.units as u
65
from astropy.table import Table
76
from astropy.coordinates import SkyCoord
87

8+
try:
9+
# This requires pyvo 1.4
10+
from pyvo.dal.exceptions import DALOverflowWarning
11+
except ImportError:
12+
pass
13+
914
from astroquery.ipac.irsa import Irsa
1015

1116

@@ -56,3 +61,11 @@ def test_list_catalogs(self):
5661
# Number of available catalogs may change over time, test only for significant drop.
5762
# (at the time of writing there are 933 tables in the list).
5863
assert len(catalogs) > 900
64+
65+
def test_tap(self):
66+
query = "SELECT TOP 5 ra,dec FROM cosmos2015"
67+
with pytest.warns(expected_warning=DALOverflowWarning,
68+
match="Partial result set. Potential causes MAXREC, async storage space, etc."):
69+
result = Irsa.query_tap(query=query)
70+
assert len(result) == 5
71+
assert result.colnames == ['ra', 'dec']

0 commit comments

Comments
 (0)