1
1
import numpy as np
2
+ import pytest
2
3
3
4
import os
5
+ import requests
4
6
5
7
from astropy import units as u
6
8
from astropy .table import Table
7
9
from astroquery .linelists .cdms .core import CDMS , parse_letternumber
10
+ from astroquery .utils .mocks import MockResponse
8
11
9
12
colname_set = set (['FREQ' , 'ERR' , 'LGINT' , 'DR' , 'ELO' , 'GUP' , 'TAG' , 'QNFMT' ,
10
13
'Ju' , 'Jl' , "vu" , "F1u" , "F2u" , "F3u" , "vl" , "Ku" , "Kl" ,
@@ -17,26 +20,36 @@ def data_path(filename):
17
20
return os .path .join (data_dir , filename )
18
21
19
22
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 )
21
34
22
- def __init__ (self , filename ):
23
- self .filename = data_path (filename )
24
35
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
29
42
30
43
31
44
def test_input_async ():
32
45
33
46
response = CDMS .query_lines_async (min_frequency = 100 * u .GHz ,
34
47
max_frequency = 1000 * u .GHz ,
35
48
min_strength = - 500 ,
36
- molecule = "028001 CO" ,
49
+ molecule = "028503 CO, v=0 " ,
37
50
get_query_payload = True )
38
51
response = dict (response )
39
- assert response ['Molecules' ] == "028001 CO"
52
+ assert response ['Molecules' ] == "028503 CO, v=0 "
40
53
np .testing .assert_almost_equal (response ['MinNu' ], 100. )
41
54
np .testing .assert_almost_equal (response ['MaxNu' ], 1000. )
42
55
@@ -55,10 +68,12 @@ def test_input_multi():
55
68
np .testing .assert_almost_equal (response ['MaxNu' ], 1000. )
56
69
57
70
58
- def test_query ():
71
+ def test_query (patch_post ):
59
72
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" )
62
77
assert isinstance (tbl , Table )
63
78
assert len (tbl ) == 8
64
79
assert set (tbl .keys ()) == colname_set
@@ -86,15 +101,14 @@ def test_parseletternumber():
86
101
assert parse_letternumber ("ZZ" ) == 3535
87
102
88
103
89
- def test_hc7s ():
104
+ def test_hc7s (patch_post ):
90
105
"""
91
106
Test for a very complicated molecule
92
107
93
108
CDMS.query_lines_async(100*u.GHz, 100.755608*u.GHz, molecule='HC7S', parse_name_locally=True)
94
109
"""
95
110
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' ,)
98
112
assert isinstance (tbl , Table )
99
113
assert len (tbl ) == 5
100
114
assert set (tbl .keys ()) == colname_set
0 commit comments