Skip to content

Commit b1fcfff

Browse files
authored
Merge pull request #2468 from eerovaher/xmatch-remote-tests-Path
TST: `xmatch` test improvements
2 parents 8cd3295 + a5e4f3f commit b1fcfff

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

astroquery/xmatch/tests/test_xmatch.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2-
import os.path
2+
from pathlib import Path
33

44
import requests
55
import pytest
@@ -11,6 +11,7 @@
1111
from astroquery.utils.mocks import MockResponse
1212
from ...xmatch import XMatch
1313

14+
DATA_DIR = Path(__file__).parent / "data"
1415
DATA_FILES = {
1516
'get': 'tables.csv', # .action.getVizieRTableNames
1617
'post': 'query_res.csv', # .request.xmatch
@@ -24,8 +25,8 @@ def __init__(self, method, url, data, **kwargs):
2425
super().__init__(**kwargs)
2526

2627
self.data = data
27-
fn = data_path(DATA_FILES[method.lower()])
28-
with open(fn, 'rb') as file:
28+
filename = DATA_DIR / DATA_FILES[method.lower()]
29+
with open(filename, "rb") as file:
2930
self.content = file.read()
3031

3132
def get_content(self):
@@ -44,11 +45,6 @@ def request_mockreturn(method, url, data, **kwargs):
4445
return MockResponseXmatch(method, url, data)
4546

4647

47-
def data_path(filename):
48-
data_dir = os.path.join(os.path.dirname(__file__), 'data')
49-
return os.path.join(data_dir, filename)
50-
51-
5248
def test_xmatch_query_invalid_max_distance():
5349
with pytest.raises(ValueError) as ex:
5450
XMatch().query_async('', '', 181 * arcsec)
@@ -84,7 +80,7 @@ def test_xmatch_query_local(monkeypatch):
8480
'send_request',
8581
lambda url, data, timeout, request_type='POST', headers={}, **kwargs:
8682
request_mockreturn(request_type, url, data, **kwargs))
87-
with open(data_path('posList.csv')) as pos_list:
83+
with open(DATA_DIR / "posList.csv") as pos_list:
8884
response = xm.query_async(
8985
cat1=pos_list, cat2='vizier:II/246/out', max_distance=5 * arcsec,
9086
colRA1='ra', colDec1='dec')
@@ -105,7 +101,7 @@ def test_xmatch_query_cat1_table_local(monkeypatch):
105101
'send_request',
106102
lambda url, data, timeout, request_type='POST', headers={}, **kwargs:
107103
request_mockreturn(request_type, url, data, **kwargs))
108-
with open(data_path('posList.csv')) as pos_list:
104+
with open(DATA_DIR / "posList.csv") as pos_list:
109105
input_table = Table.read(pos_list.readlines(),
110106
format='ascii.csv',
111107
guess=False)

astroquery/xmatch/tests/test_xmatch_remote.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2-
import os.path
2+
from pathlib import Path
33
import os
44
import sys
55
import pytest
@@ -20,7 +20,7 @@
2020
from ...xmatch import XMatch
2121

2222

23-
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
23+
DATA_DIR = Path(__file__).parent / "data"
2424

2525

2626
@pytest.mark.remote_data
@@ -32,6 +32,18 @@ def test_is_xmatch_up():
3232
pytest.xfail("XMATCH appears to be down. Exception was: {0}".format(ex))
3333

3434

35+
@pytest.fixture(scope="module")
36+
def remote_table(tmp_path_factory):
37+
# this can be used to check that the API is still functional & doing as expected
38+
infile = DATA_DIR / "posList.csv"
39+
outfile = tmp_path_factory.mktemp("remote_data") / "http_result.csv"
40+
os.system("curl -X POST -F request=xmatch -F distMaxArcsec=5 -F RESPONSEFORMAT=csv "
41+
"-F cat1=@{1} -F colRA1=ra -F colDec1=dec -F cat2=vizier:II/246/out "
42+
"http://cdsxmatch.u-strasbg.fr/xmatch/api/v1/sync > {0}".
43+
format(outfile, infile))
44+
return ascii.read(outfile, format="csv", fast_reader=False)
45+
46+
3547
@pytest.mark.remote_data
3648
@pytest.mark.dependency(depends=["xmatch_up"])
3749
class TestXMatch:
@@ -55,8 +67,8 @@ def test_xmatch_is_avail_table(self, xmatch):
5567
assert xmatch.is_table_available('vizier:II/311/wise')
5668
assert not xmatch.is_table_available('blablabla')
5769

58-
def test_xmatch_query(self, xmatch):
59-
with open(os.path.join(DATA_DIR, 'posList.csv'), 'r') as pos_list:
70+
def test_xmatch_query(self, xmatch, remote_table):
71+
with open(DATA_DIR / "posList.csv", "r") as pos_list:
6072
try:
6173
table = xmatch.query(
6274
cat1=pos_list, cat2='vizier:II/246/out', max_distance=5 * arcsec,
@@ -70,12 +82,10 @@ def test_xmatch_query(self, xmatch):
7082
'e_Jmag', 'e_Hmag', 'e_Kmag', 'Qfl', 'Rfl', 'X', 'MeasureJD']
7183
assert len(table) == 11
7284

73-
http_test_table = self.http_test()
74-
assert all(table == http_test_table)
85+
assert all(table == remote_table)
7586

76-
def test_xmatch_query_astropy_table(self, xmatch):
77-
datapath = os.path.join(DATA_DIR, 'posList.csv')
78-
input_table = Table.read(datapath, format='ascii.csv')
87+
def test_xmatch_query_astropy_table(self, xmatch, remote_table):
88+
input_table = Table.read(DATA_DIR / "posList.csv", format="ascii.csv")
7989
try:
8090
table = xmatch.query(
8191
cat1=input_table, cat2='vizier:II/246/out', max_distance=5 * arcsec,
@@ -89,8 +99,7 @@ def test_xmatch_query_astropy_table(self, xmatch):
8999
'e_Jmag', 'e_Hmag', 'e_Kmag', 'Qfl', 'Rfl', 'X', 'MeasureJD']
90100
assert len(table) == 11
91101

92-
http_test_table = self.http_test()
93-
assert all(table == http_test_table)
102+
assert all(table == remote_table)
94103

95104
@pytest.mark.skipif('regions' not in sys.modules,
96105
reason="requires astropy-regions")
@@ -102,14 +111,3 @@ def test_xmatch_query_with_cone_area(self, xmatch):
102111
except ReadTimeout:
103112
pytest.xfail("xmatch query timed out.")
104113
assert len(table) == 185
105-
106-
def http_test(self):
107-
# this can be used to check that the API is still functional & doing as expected
108-
infile = os.path.join(DATA_DIR, 'posList.csv')
109-
outfile = os.path.join(DATA_DIR, 'http_result.csv')
110-
os.system('curl -X POST -F request=xmatch -F distMaxArcsec=5 -F RESPONSEFORMAT=csv '
111-
'-F cat1=@{1} -F colRA1=ra -F colDec1=dec -F cat2=vizier:II/246/out '
112-
'http://cdsxmatch.u-strasbg.fr/xmatch/api/v1/sync > {0}'.
113-
format(outfile, infile))
114-
table = ascii.read(outfile, format='csv', fast_reader=False)
115-
return table

0 commit comments

Comments
 (0)