Skip to content

Commit 8cdc136

Browse files
committed
Allow cache tests and remote tests to co-exist
Previously the cache tests permanently replaced `requests.Session.request`, which caused the following remote tests to fail. Now the replacement is handled by a test fixture, which ensures it does not affect any tests that do not want to be affected. Furthermore, the fixture can automatically change the mocked response after being queried once, which reduces manual setup needed in the cache tests.
1 parent 07fb421 commit 8cdc136

File tree

1 file changed

+23
-39
lines changed

1 file changed

+23
-39
lines changed

astroquery/tests/test_cache.py

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,25 @@
1616
TEXT1 = "Penguin"
1717
TEXT2 = "Walrus"
1818

19-
pytest.skip(reason='request function mocking is buggy here, skipping tests until '
20-
'https://github.com/astropy/astroquery/issues/2632 is resolved',
21-
allow_module_level=True)
2219

20+
def _create_response(response_text):
21+
mock_response = requests.Response()
22+
mock_response._content = response_text
23+
mock_response.request = requests.PreparedRequest()
24+
mock_response.status_code = 200
25+
return mock_response
2326

24-
def set_response(resp_text, resp_status=200):
25-
"""Function that allows us to set a specific mock response for cache testing"""
2627

27-
def get_mockreturn(url, *args, **kwargs):
28-
"""Generate a mock return to a requests call"""
28+
@pytest.fixture
29+
def changing_mocked_response(monkeypatch):
30+
"""Provide responses that can change after being queried once."""
31+
first_responses = {URL1: _create_response(TEXT1), "ceb": _create_response(TEXT1)}
32+
default_response = _create_response(TEXT2)
2933

30-
myresp = requests.Response()
31-
myresp._content = resp_text
32-
myresp.request = requests.PreparedRequest()
33-
myresp.status_code = resp_status
34+
def get_mockreturn(*args, **kwargs):
35+
return first_responses.pop(args[2], default_response)
3436

35-
return myresp
36-
37-
requests.Session.request = get_mockreturn
37+
monkeypatch.setattr(requests.Session, "request", get_mockreturn)
3838

3939

4040
class CacheTestClass(QueryWithLogin):
@@ -75,7 +75,7 @@ def test_conf():
7575
assert cache_conf.cache_active == default_active
7676

7777

78-
def test_basic_caching():
78+
def test_basic_caching(changing_mocked_response):
7979
cache_conf.reset()
8080

8181
mytest = CacheTestClass()
@@ -84,14 +84,10 @@ def test_basic_caching():
8484
mytest.clear_cache()
8585
assert len(os.listdir(mytest.cache_location)) == 0
8686

87-
set_response(TEXT1)
88-
8987
resp = mytest.test_func(URL1)
9088
assert resp.content == TEXT1
9189
assert len(os.listdir(mytest.cache_location)) == 1
9290

93-
set_response(TEXT2)
94-
9591
resp = mytest.test_func(URL2) # query that has not been cached
9692
assert resp.content == TEXT2
9793
assert len(os.listdir(mytest.cache_location)) == 2
@@ -131,7 +127,7 @@ def test_change_location(tmp_path):
131127
assert mytest.name in mytest.cache_location.parts
132128

133129

134-
def test_login():
130+
def test_login(changing_mocked_response):
135131
cache_conf.reset()
136132

137133
mytest = CacheTestClass()
@@ -140,19 +136,15 @@ def test_login():
140136
mytest.clear_cache()
141137
assert len(os.listdir(mytest.cache_location)) == 0
142138

143-
set_response(TEXT1) # Text 1 is set as the approved password
144-
145139
mytest.login("ceb")
146140
assert mytest.authenticated()
147141
assert len(os.listdir(mytest.cache_location)) == 0 # request should not be cached
148142

149-
set_response(TEXT2) # Text 2 is not the approved password
150-
151143
mytest.login("ceb")
152144
assert not mytest.authenticated() # Should not be accessing cache
153145

154146

155-
def test_timeout(monkeypatch):
147+
def test_timeout(changing_mocked_response, monkeypatch):
156148
cache_conf.reset()
157149

158150
mytest = CacheTestClass()
@@ -161,13 +153,9 @@ def test_timeout(monkeypatch):
161153
mytest.clear_cache()
162154
assert len(os.listdir(mytest.cache_location)) == 0
163155

164-
set_response(TEXT1) # setting the response
165-
166156
resp = mytest.test_func(URL1) # should be cached
167157
assert resp.content == TEXT1
168158

169-
set_response(TEXT2) # changing the response
170-
171159
resp = mytest.test_func(URL1) # should access cached value
172160
assert resp.content == TEXT1
173161

@@ -181,13 +169,14 @@ def test_timeout(monkeypatch):
181169

182170
# Testing a cache timeout of "none"
183171
cache_conf.cache_timeout = None
184-
set_response(TEXT1)
172+
# Ensure response can only come from cache.
173+
monkeypatch.delattr(requests.Session, "request")
185174

186175
resp = mytest.test_func(URL1)
187176
assert resp.content == TEXT2 # cache is accessed
188177

189178

190-
def test_deactivate():
179+
def test_deactivate_directly(changing_mocked_response):
191180
cache_conf.reset()
192181

193182
mytest = CacheTestClass()
@@ -196,33 +185,28 @@ def test_deactivate():
196185
mytest.clear_cache()
197186
assert len(os.listdir(mytest.cache_location)) == 0
198187

199-
set_response(TEXT1)
200-
201188
resp = mytest.test_func(URL1)
202189
assert resp.content == TEXT1
203190
assert len(os.listdir(mytest.cache_location)) == 0
204191

205-
set_response(TEXT2)
206-
207192
resp = mytest.test_func(URL1)
208193
assert resp.content == TEXT2
209194
assert len(os.listdir(mytest.cache_location)) == 0
210195

211196
cache_conf.reset()
212197
assert cache_conf.cache_active is True
213198

199+
200+
def test_deactivate_with_set_temp(changing_mocked_response):
201+
mytest = CacheTestClass()
214202
with cache_conf.set_temp('cache_active', False):
215203
mytest.clear_cache()
216204
assert len(os.listdir(mytest.cache_location)) == 0
217205

218-
set_response(TEXT1)
219-
220206
resp = mytest.test_func(URL1)
221207
assert resp.content == TEXT1
222208
assert len(os.listdir(mytest.cache_location)) == 0
223209

224-
set_response(TEXT2)
225-
226210
resp = mytest.test_func(URL1)
227211
assert resp.content == TEXT2
228212
assert len(os.listdir(mytest.cache_location)) == 0

0 commit comments

Comments
 (0)