-
-
Notifications
You must be signed in to change notification settings - Fork 437
Expand file tree
/
Copy pathtest_gemini.py
More file actions
244 lines (197 loc) · 7.86 KB
/
test_gemini.py
File metadata and controls
244 lines (197 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
""" Test Gemini Astroquery module.
For information on how/why this test is built the way it is, see the astroquery
documentation at:
https://astroquery.readthedocs.io/en/latest/testing.html
"""
from datetime import date
import json
import os
import pytest
import requests
from astropy import units
from astropy.coordinates import SkyCoord
from astropy.table import Table
from astroquery import gemini
from astroquery.gemini.urlhelper import URLHelper
DATA_FILES = {"m101": "m101.json"}
class MockResponse:
def __init__(self, text):
self.text = text
def json(self):
return json.loads(self.text)
@pytest.fixture
def patch_get(request):
""" mock get requests so they return our canned JSON to mimic Gemini's archive website """
mp = request.getfixturevalue("monkeypatch")
mp.setattr(requests.Session, 'request', get_mockreturn)
return mp
# to inspect behavior, updated when the mock get call is made
saved_request = None
def get_mockreturn(url, *args, **kwargs):
""" generate the actual mock textual data from our included datafile with json results """
global saved_request
saved_request = {'url': url, 'args': args, 'kwargs': kwargs}
filename = data_path(DATA_FILES['m101'])
f = open(filename, 'r')
text = f.read()
retval = MockResponse(text)
f.close()
return retval
def data_path(filename):
""" determine the path to our sample data file """
data_dir = os.path.join(os.path.dirname(__file__), 'data')
return os.path.join(data_dir, filename)
""" Coordinates to use for testing """
coords = SkyCoord(210.80242917, 54.34875, unit="deg")
def test_observations_query_region(patch_get):
""" test query against a region of the sky """
result = gemini.Observations.query_region(coords, radius=0.3 * units.deg)
assert isinstance(result, Table)
assert len(result) > 0
def test_observations_query_criteria(patch_get):
""" test query against an instrument/program via criteria """
result = gemini.Observations.query_criteria(instrument='GMOS-N', program_id='GN-CAL20191122',
observation_type='BIAS',
utc_date=(date(2019, 10, 1), date(2019, 11, 25)))
assert isinstance(result, Table)
assert len(result) > 0
def test_observations_query_criteria_radius_defaults(patch_get):
""" test query against an instrument/program via criteria """
result = gemini.Observations.query_criteria(instrument='GMOS-N', program_id='GN-CAL20191122',
observation_type='BIAS')
global saved_request
assert (saved_request is not None and 'args' in saved_request and len(saved_request['args']) >= 2)
assert ('/sr=' not in saved_request['args'][1])
saved_request = None
result = gemini.Observations.query_criteria(instrument='GMOS-N', program_id='GN-2016A-Q-9',
observation_type='BIAS', coordinates=coords)
assert len(result) > 0
assert (saved_request is not None and 'args' in saved_request and len(saved_request['args']) >= 2)
assert ('/sr=0.300000d' in saved_request['args'][1])
saved_request = None
gemini.Observations.query_criteria(instrument='GMOS-N', program_id='GN-2016A-Q-9',
observation_type='BIAS', objectname='m101')
assert (saved_request is not None and 'args' in saved_request and len(saved_request['args']) >= 2)
assert ('/sr=0.300000d' in saved_request['args'][1])
def test_observations_query_raw(patch_get):
""" test querying raw """
result = gemini.Observations.query_raw('GMOS-N', 'BIAS', progid='GN-CAL20191122')
assert isinstance(result, Table)
assert len(result) > 0
def test_url_helper_arg():
""" test the urlhelper logic """
urlh = URLHelper()
args = ["foo"]
kwargs = {}
url = urlh.build_url(*args, **kwargs)
assert url == "https://archive.gemini.edu/jsonsummary/notengineering/NotFail/foo"
def test_url_helper_kwarg():
""" test the urlhelper logic """
urlh = URLHelper()
args = []
kwargs = {"foo": "bar"}
url = urlh.build_url(*args, **kwargs)
assert url == "https://archive.gemini.edu/jsonsummary/notengineering/NotFail/foo=bar"
def test_url_helper_radius():
""" test the urlhelper logic """
urlh = URLHelper()
args = []
kwargs = {"radius": "0.4d"}
url = urlh.build_url(*args, **kwargs)
assert url == "https://archive.gemini.edu/jsonsummary/notengineering/NotFail/sr=0.400000d"
def test_url_helper_coordinates():
""" test the urlhelper logic """
urlh = URLHelper()
args = []
kwargs = {"coordinates": "210.80242917 54.348753"}
url = urlh.build_url(*args, **kwargs)
assert url == "https://archive.gemini.edu/jsonsummary/notengineering/NotFail/ra=210.802429/dec=54.348753"
# send arg, should it have notengineering?, should it have NotFail?
eng_fail_tests = [
('notengineering', True, True),
('engineering', False, True),
('includeengineering', False, True),
('NotFail', True, True),
('AnyQA', True, False),
('Pass', True, False),
('Lucky', True, False),
('Win', True, False),
('Usable', True, False),
('Undefind', True, False),
('Fail', True, False),
]
@pytest.mark.parametrize("test_arg", eng_fail_tests)
def test_url_helper_eng_fail(test_arg):
""" test the urlhelper logic around engineering/fail requests/defaults """
urlh = URLHelper()
args = [test_arg[0]]
should_have_noteng = test_arg[1]
should_have_notfail = test_arg[2]
kwargs = {}
url = urlh.build_url(*args, **kwargs)
urlsplit = url.split('/')
assert (('notengineering' in urlsplit) == should_have_noteng)
assert (('NotFail' in urlsplit) == should_have_notfail)
def test_observations_query_region_get_query_payload():
coords = SkyCoord(210.80242917, 54.34875, unit="deg")
result = gemini.Observations.query_region(
coords,
radius=0.3 * units.deg,
get_query_payload=True
)
assert isinstance(result, dict)
assert 'url' in result
assert 'method' in result
assert result['method'] == 'GET'
assert 'data' in result
assert result['data'] == {}
assert 'ra=210.802429' in result['url']
assert 'dec=54.348750' in result['url']
assert 'sr=0.300000d' in result['url']
def test_observations_query_object_get_query_payload():
result = gemini.Observations.query_object(
'M101',
radius=0.3 * units.deg,
get_query_payload=True
)
assert isinstance(result, dict)
assert 'url' in result
assert 'method' in result
assert result['method'] == 'GET'
assert 'data' in result
assert result['data'] == {}
assert 'object=M101' in result['url']
assert 'sr=0.300000d' in result['url']
def test_observations_query_criteria_get_query_payload():
result = gemini.Observations.query_criteria(
instrument='GMOS-N',
program_id='GN-CAL20191122',
observation_type='BIAS',
utc_date=(date(2019, 10, 1), date(2019, 11, 25)),
get_query_payload=True
)
assert isinstance(result, dict)
assert 'url' in result
assert 'method' in result
assert result['method'] == 'GET'
assert 'data' in result
assert result['data'] == {}
assert 'GMOS-N' in result['url']
assert 'BIAS' in result['url']
assert '20191001-20191125' in result['url']
assert 'GN-CAL20191122' in result['url']
def test_observations_query_raw_get_query_payload():
result = gemini.Observations.query_raw(
'GMOS-N', 'BIAS',
progid='GN-CAL20191122',
get_query_payload=True
)
assert isinstance(result, dict)
assert 'url' in result
assert 'method' in result
assert result['method'] == 'GET'
assert 'data' in result
assert result['data'] == {}
assert 'GMOS-N' in result['url']
assert 'BIAS' in result['url']
assert 'progid=GN-CAL20191122' in result['url']