Skip to content

Commit 8c03e98

Browse files
committed
add some layers of testing
1 parent b0f98d4 commit 8c03e98

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

astroquery/linelists/cdms/setup_package.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
def get_package_data():
88

99
paths_test = [os.path.join('data', 'CO.data'),
10-
os.path.join('data', 'HC7S.data')]
10+
os.path.join('data', 'HC7S.data'),
11+
os.path.join('data', 'post_response.html'),
12+
]
1113
paths_data = [os.path.join('data', 'catdir.cat')]
1214

1315
return {'astroquery.linelists.cdms.tests': paths_test,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html><head>
2+
</head>
3+
<frameset rows="60,*" bordercolor="#ffffff">
4+
<frame src="/classic/predictions/cdmstabhead.html" border=0 frameborder=0 framespacing=0 marginheight=30 marginwidth=16 scrolling=no>
5+
<frame src="/classic/predictions/cdmscache/cdmstab{replace}.html" border=0 frameborder=0 framespacing=0 marginheight=0 marginwidth=12 scrolling=yes>
6+
</frameset>
7+
<body>Sorry, your browser does not support frames!<br>
8+
<a href="/classic/predictions/cdmscache/cdmstab{replace}.html">tabular</a></body>
9+
</html>

astroquery/linelists/cdms/tests/test_cdms.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import numpy as np
2+
import pytest
23

34
import os
5+
import requests
46

57
from astropy import units as u
68
from astropy.table import Table
79
from astroquery.linelists.cdms.core import CDMS, parse_letternumber
10+
from astroquery.utils.mocks import MockResponse
811

912
colname_set = set(['FREQ', 'ERR', 'LGINT', 'DR', 'ELO', 'GUP', 'TAG', 'QNFMT',
1013
'Ju', 'Jl', "vu", "F1u", "F2u", "F3u", "vl", "Ku", "Kl",
@@ -17,26 +20,36 @@ def data_path(filename):
1720
return os.path.join(data_dir, filename)
1821

1922

20-
class MockResponseSpec:
23+
def mockreturn(*args, method='GET', data={}, url='', **kwargs):
24+
if method == 'GET':
25+
molecule = url.split('cdmstab')[1].split('.')[0]
26+
with open(data_path(molecule+".data"), 'rb') as fh:
27+
content = fh.read()
28+
return MockResponse(content=content)
29+
elif method == 'POST':
30+
molecule = dict(data)['Molecules']
31+
with open(data_path("post_response.html"), 'r') as fh:
32+
content = fh.read().format(replace=molecule).encode()
33+
return MockResponse(content=content)
2134

22-
def __init__(self, filename):
23-
self.filename = data_path(filename)
2435

25-
@property
26-
def text(self):
27-
with open(self.filename) as f:
28-
return f.read()
36+
@pytest.fixture
37+
def patch_post(request):
38+
mp = request.getfixturevalue("monkeypatch")
39+
40+
mp.setattr(CDMS, '_request', mockreturn)
41+
return mp
2942

3043

3144
def test_input_async():
3245

3346
response = CDMS.query_lines_async(min_frequency=100 * u.GHz,
3447
max_frequency=1000 * u.GHz,
3548
min_strength=-500,
36-
molecule="028001 CO",
49+
molecule="028503 CO, v=0",
3750
get_query_payload=True)
3851
response = dict(response)
39-
assert response['Molecules'] == "028001 CO"
52+
assert response['Molecules'] == "028503 CO, v=0"
4053
np.testing.assert_almost_equal(response['MinNu'], 100.)
4154
np.testing.assert_almost_equal(response['MaxNu'], 1000.)
4255

@@ -55,10 +68,12 @@ def test_input_multi():
5568
np.testing.assert_almost_equal(response['MaxNu'], 1000.)
5669

5770

58-
def test_query():
71+
def test_query(patch_post):
5972

60-
response = MockResponseSpec('CO.data')
61-
tbl = CDMS._parse_result(response)
73+
tbl = CDMS.query_lines(min_frequency=100 * u.GHz,
74+
max_frequency=1000 * u.GHz,
75+
min_strength=-500,
76+
molecule="CO")
6277
assert isinstance(tbl, Table)
6378
assert len(tbl) == 8
6479
assert set(tbl.keys()) == colname_set
@@ -86,15 +101,14 @@ def test_parseletternumber():
86101
assert parse_letternumber("ZZ") == 3535
87102

88103

89-
def test_hc7s():
104+
def test_hc7s(patch_post):
90105
"""
91106
Test for a very complicated molecule
92107
93108
CDMS.query_lines_async(100*u.GHz, 100.755608*u.GHz, molecule='HC7S', parse_name_locally=True)
94109
"""
95110

96-
response = MockResponseSpec('HC7S.data')
97-
tbl = CDMS._parse_result(response)
111+
tbl = CDMS.query_lines(100*u.GHz, 100.755608*u.GHz, molecule='HC7S',)
98112
assert isinstance(tbl, Table)
99113
assert len(tbl) == 5
100114
assert set(tbl.keys()) == colname_set

0 commit comments

Comments
 (0)