Skip to content

Commit 1476f96

Browse files
rickynilssonbsipocz
authored andcommitted
Add mock test
1 parent d5b3b19 commit 1476f96

File tree

1 file changed

+35
-19
lines changed

1 file changed

+35
-19
lines changed

astroquery/ipac/nexsci/nasa_exoplanet_archive/tests/test_nasa_exoplanet_archive.py

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,41 @@ def patch_get(request): # pragma: nocover
119119
return mp
120120

121121

122+
# aliaslookup file in data/
123+
LOOKUP_DATA_FILE = 'bpic_aliaslookup.json'
124+
125+
126+
def data_path(filename):
127+
data_dir = os.path.join(os.path.dirname(__file__), 'data')
128+
return os.path.join(data_dir, filename)
129+
130+
131+
# monkeypatch replacement request function
132+
def nonremote_request(self, url, **kwargs):
133+
with open(data_path(LOOKUP_DATA_FILE), 'rb') as f:
134+
response = MockResponse(content=json.loads(f), url=url)
135+
return response
136+
137+
138+
# use a pytest fixture to create a dummy 'requests.get' function,
139+
# that mocks(monkeypatches) the actual 'requests.get' function:
140+
@pytest.fixture
141+
def patch_request(request):
142+
mp = request.getfixturevalue("monkeypatch")
143+
144+
mp.setattr(NasaExoplanetArchiveClass, '_request', nonremote_request)
145+
return mp
146+
147+
148+
def test_query_aliases(patch_request):
149+
nasa_exoplanet_archive = NasaExoplanetArchiveClass()
150+
result = nasa_exoplanet_archive.query_aliases('bet Pic')
151+
assert len(result) > 10
152+
assert 'GJ 219' in result
153+
assert 'bet Pic' in result
154+
assert '2MASS J05471708-5103594' in result
155+
156+
122157
def test_backwards_compat(patch_get):
123158
"""
124159
These are the tests from the previous version of this interface.
@@ -285,25 +320,6 @@ def mock_run_query(url=conf.url_tap):
285320
assert 'pscomppars' in result
286321

287322

288-
@patch('astroquery.ipac.nexsci.nasa_exoplanet_archive.core.get_access_url',
289-
Mock(side_effect=lambda x: 'https://some.url'))
290-
def test_query_aliases():
291-
nasa_exoplanet_archive = NasaExoplanetArchiveClass()
292-
293-
def mock_run_query(url=conf.url_aliaslookup, object_name="HD 209458"):
294-
assert url == conf.url_aliaslookup
295-
assert object_name == "HD 209458"
296-
result = PropertyMock()
297-
result = ['HD 209458', '2MASS J22031077+1853036', 'BD+18 4917', 'Gaia DR2 1779546757669063552',
298-
'HIP 108859', 'SAO 107623', 'TIC 420814525', 'TYC 1688-01821-1', 'V0376 Peg', 'WISE J220310.79+185303.3']
299-
return result
300-
nasa_exoplanet_archive.query_aliases = mock_run_query
301-
result = nasa_exoplanet_archive.query_aliases()
302-
assert 'HD 209458' in result
303-
assert 'HIP 108859' in result
304-
assert 'V0376 Peg' in result
305-
306-
307323
def test_deprecated_namespace_import_warning():
308324
with pytest.warns(DeprecationWarning):
309325
import astroquery.nasa_exoplanet_archive

0 commit comments

Comments
 (0)