16
16
TEXT1 = "Penguin"
17
17
TEXT2 = "Walrus"
18
18
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 )
22
19
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
23
26
24
- def set_response (resp_text , resp_status = 200 ):
25
- """Function that allows us to set a specific mock response for cache testing"""
26
27
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 )
29
33
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 )
34
36
35
- return myresp
36
-
37
- requests .Session .request = get_mockreturn
37
+ monkeypatch .setattr (requests .Session , "request" , get_mockreturn )
38
38
39
39
40
40
class CacheTestClass (QueryWithLogin ):
@@ -75,7 +75,7 @@ def test_conf():
75
75
assert cache_conf .cache_active == default_active
76
76
77
77
78
- def test_basic_caching ():
78
+ def test_basic_caching (changing_mocked_response ):
79
79
cache_conf .reset ()
80
80
81
81
mytest = CacheTestClass ()
@@ -84,14 +84,10 @@ def test_basic_caching():
84
84
mytest .clear_cache ()
85
85
assert len (os .listdir (mytest .cache_location )) == 0
86
86
87
- set_response (TEXT1 )
88
-
89
87
resp = mytest .test_func (URL1 )
90
88
assert resp .content == TEXT1
91
89
assert len (os .listdir (mytest .cache_location )) == 1
92
90
93
- set_response (TEXT2 )
94
-
95
91
resp = mytest .test_func (URL2 ) # query that has not been cached
96
92
assert resp .content == TEXT2
97
93
assert len (os .listdir (mytest .cache_location )) == 2
@@ -131,7 +127,7 @@ def test_change_location(tmp_path):
131
127
assert mytest .name in mytest .cache_location .parts
132
128
133
129
134
- def test_login ():
130
+ def test_login (changing_mocked_response ):
135
131
cache_conf .reset ()
136
132
137
133
mytest = CacheTestClass ()
@@ -140,19 +136,15 @@ def test_login():
140
136
mytest .clear_cache ()
141
137
assert len (os .listdir (mytest .cache_location )) == 0
142
138
143
- set_response (TEXT1 ) # Text 1 is set as the approved password
144
-
145
139
mytest .login ("ceb" )
146
140
assert mytest .authenticated ()
147
141
assert len (os .listdir (mytest .cache_location )) == 0 # request should not be cached
148
142
149
- set_response (TEXT2 ) # Text 2 is not the approved password
150
-
151
143
mytest .login ("ceb" )
152
144
assert not mytest .authenticated () # Should not be accessing cache
153
145
154
146
155
- def test_timeout (monkeypatch ):
147
+ def test_timeout (changing_mocked_response , monkeypatch ):
156
148
cache_conf .reset ()
157
149
158
150
mytest = CacheTestClass ()
@@ -161,13 +153,9 @@ def test_timeout(monkeypatch):
161
153
mytest .clear_cache ()
162
154
assert len (os .listdir (mytest .cache_location )) == 0
163
155
164
- set_response (TEXT1 ) # setting the response
165
-
166
156
resp = mytest .test_func (URL1 ) # should be cached
167
157
assert resp .content == TEXT1
168
158
169
- set_response (TEXT2 ) # changing the response
170
-
171
159
resp = mytest .test_func (URL1 ) # should access cached value
172
160
assert resp .content == TEXT1
173
161
@@ -181,13 +169,14 @@ def test_timeout(monkeypatch):
181
169
182
170
# Testing a cache timeout of "none"
183
171
cache_conf .cache_timeout = None
184
- set_response (TEXT1 )
172
+ # Ensure response can only come from cache.
173
+ monkeypatch .delattr (requests .Session , "request" )
185
174
186
175
resp = mytest .test_func (URL1 )
187
176
assert resp .content == TEXT2 # cache is accessed
188
177
189
178
190
- def test_deactivate ( ):
179
+ def test_deactivate_directly ( changing_mocked_response ):
191
180
cache_conf .reset ()
192
181
193
182
mytest = CacheTestClass ()
@@ -196,33 +185,28 @@ def test_deactivate():
196
185
mytest .clear_cache ()
197
186
assert len (os .listdir (mytest .cache_location )) == 0
198
187
199
- set_response (TEXT1 )
200
-
201
188
resp = mytest .test_func (URL1 )
202
189
assert resp .content == TEXT1
203
190
assert len (os .listdir (mytest .cache_location )) == 0
204
191
205
- set_response (TEXT2 )
206
-
207
192
resp = mytest .test_func (URL1 )
208
193
assert resp .content == TEXT2
209
194
assert len (os .listdir (mytest .cache_location )) == 0
210
195
211
196
cache_conf .reset ()
212
197
assert cache_conf .cache_active is True
213
198
199
+
200
+ def test_deactivate_with_set_temp (changing_mocked_response ):
201
+ mytest = CacheTestClass ()
214
202
with cache_conf .set_temp ('cache_active' , False ):
215
203
mytest .clear_cache ()
216
204
assert len (os .listdir (mytest .cache_location )) == 0
217
205
218
- set_response (TEXT1 )
219
-
220
206
resp = mytest .test_func (URL1 )
221
207
assert resp .content == TEXT1
222
208
assert len (os .listdir (mytest .cache_location )) == 0
223
209
224
- set_response (TEXT2 )
225
-
226
210
resp = mytest .test_func (URL1 )
227
211
assert resp .content == TEXT2
228
212
assert len (os .listdir (mytest .cache_location )) == 0
0 commit comments