@@ -125,9 +125,9 @@ def test_cachedownloader_cached_file(tmp_path, monkeypatch, default_response):
125
125
126
126
127
127
@pytest .mark .parametrize ("disable_cache" , (True , False ))
128
- def test_cachedownloader_on_success (cache_dir , disable_cache ):
128
+ def test_cachedownloader_on_success (get_download_cache_loc , disable_cache ):
129
129
add_default_response ()
130
- f = cache_dir / "check_jsonschema" / "downloads" / " schema1.json"
130
+ f = get_download_cache_loc ( " schema1.json")
131
131
cd = CacheDownloader (disable_cache = disable_cache ).bind (
132
132
"https://example.com/schema1.json"
133
133
)
@@ -151,7 +151,9 @@ def test_cachedownloader_using_alternate_target_dir(cache_dir):
151
151
152
152
@pytest .mark .parametrize ("disable_cache" , (True , False ))
153
153
@pytest .mark .parametrize ("failures" , (1 , 2 , requests .ConnectionError ))
154
- def test_cachedownloader_succeeds_after_few_errors (cache_dir , disable_cache , failures ):
154
+ def test_cachedownloader_succeeds_after_few_errors (
155
+ get_download_cache_loc , disable_cache , failures
156
+ ):
155
157
if isinstance (failures , int ):
156
158
for _i in range (failures ):
157
159
responses .add (
@@ -168,7 +170,7 @@ def test_cachedownloader_succeeds_after_few_errors(cache_dir, disable_cache, fai
168
170
match_querystring = None ,
169
171
)
170
172
add_default_response ()
171
- f = cache_dir / "check_jsonschema" / "downloads" / " schema1.json"
173
+ f = get_download_cache_loc ( " schema1.json")
172
174
cd = CacheDownloader (disable_cache = disable_cache ).bind (
173
175
"https://example.com/schema1.json"
174
176
)
@@ -184,7 +186,7 @@ def test_cachedownloader_succeeds_after_few_errors(cache_dir, disable_cache, fai
184
186
@pytest .mark .parametrize ("disable_cache" , (True , False ))
185
187
@pytest .mark .parametrize ("connection_error" , (True , False ))
186
188
def test_cachedownloader_fails_after_many_errors (
187
- cache_dir , disable_cache , connection_error
189
+ get_download_cache_loc , disable_cache , connection_error
188
190
):
189
191
for _i in range (10 ):
190
192
if connection_error :
@@ -202,7 +204,7 @@ def test_cachedownloader_fails_after_many_errors(
202
204
match_querystring = None ,
203
205
)
204
206
add_default_response () # never reached, the 11th response
205
- f = cache_dir / "check_jsonschema" / "downloads" / " schema1.json"
207
+ f = get_download_cache_loc ( " schema1.json")
206
208
cd = CacheDownloader (disable_cache = disable_cache ).bind (
207
209
"https://example.com/schema1.json"
208
210
)
@@ -213,7 +215,7 @@ def test_cachedownloader_fails_after_many_errors(
213
215
214
216
215
217
@pytest .mark .parametrize ("disable_cache" , (True , False ))
216
- def test_cachedownloader_retries_on_bad_data (cache_dir , disable_cache ):
218
+ def test_cachedownloader_retries_on_bad_data (get_download_cache_loc , disable_cache ):
217
219
responses .add (
218
220
"GET" ,
219
221
"https://example.com/schema1.json" ,
@@ -222,7 +224,7 @@ def test_cachedownloader_retries_on_bad_data(cache_dir, disable_cache):
222
224
match_querystring = None ,
223
225
)
224
226
add_default_response ()
225
- f = cache_dir / "check_jsonschema" / "downloads" / " schema1.json"
227
+ f = get_download_cache_loc ( " schema1.json")
226
228
cd = CacheDownloader (
227
229
disable_cache = disable_cache ,
228
230
).bind (
@@ -245,20 +247,19 @@ def test_cachedownloader_retries_on_bad_data(cache_dir, disable_cache):
245
247
"failure_mode" , ("header_missing" , "header_malformed" , "time_overflow" )
246
248
)
247
249
def test_cachedownloader_handles_bad_lastmod_header (
248
- monkeypatch , cache_dir , file_exists , failure_mode
250
+ monkeypatch ,
251
+ get_download_cache_loc ,
252
+ inject_cached_download ,
253
+ file_exists ,
254
+ failure_mode ,
249
255
):
256
+ uri = "https://example.com/schema1.json"
250
257
if failure_mode == "header_missing" :
251
- responses .add (
252
- "GET" ,
253
- "https://example.com/schema1.json" ,
254
- headers = {},
255
- json = {},
256
- match_querystring = None ,
257
- )
258
+ responses .add ("GET" , uri , headers = {}, json = {}, match_querystring = None )
258
259
elif failure_mode == "header_malformed" :
259
260
responses .add (
260
261
"GET" ,
261
- "https://example.com/schema1.json" ,
262
+ uri ,
262
263
headers = {"Last-Modified" : "Jan 2000 00:00:01" },
263
264
json = {},
264
265
match_querystring = None ,
@@ -274,47 +275,47 @@ def fake_mktime(*args):
274
275
raise NotImplementedError
275
276
276
277
original_file_contents = b'{"foo": "bar"}'
277
- (cache_dir / "check_jsonschema" / "downloads" ).mkdir (parents = True )
278
- f = cache_dir / "check_jsonschema" / "downloads" / "schema1.json"
278
+ file_path = get_download_cache_loc (uri )
279
279
280
+ assert not file_path .exists ()
280
281
if file_exists :
281
- f .write_bytes (original_file_contents )
282
- else :
283
- assert not f .exists ()
282
+ inject_cached_download (uri , original_file_contents )
284
283
285
- cd = CacheDownloader ().bind ("https://example.com/schema1.json" , filename = str ( f ) )
284
+ cd = CacheDownloader ().bind (uri )
286
285
287
286
# if the file already existed, it will not be overwritten by the cachedownloader
288
287
# so the returned value for both the downloader and a direct file read should be the
289
288
# original contents
290
289
if file_exists :
291
290
with cd .open () as fp :
292
291
assert fp .read () == original_file_contents
293
- assert f .read_bytes () == original_file_contents
292
+ assert file_path .read_bytes () == original_file_contents
294
293
# otherwise, the file will have been created with new content
295
294
# both reads will show that new content
296
295
else :
297
296
with cd .open () as fp :
298
297
assert fp .read () == b"{}"
299
- assert f .read_bytes () == b"{}"
298
+ assert file_path .read_bytes () == b"{}"
300
299
301
300
# at the end, the file always exists on disk
302
- assert f .exists ()
301
+ assert file_path .exists ()
303
302
304
303
305
- def test_cachedownloader_validation_is_not_invoked_on_hit (monkeypatch , cache_dir ):
304
+ def test_cachedownloader_validation_is_not_invoked_on_hit (
305
+ monkeypatch , inject_cached_download
306
+ ):
306
307
"""
307
308
Regression test for https://github.com/python-jsonschema/check-jsonschema/issues/453
308
309
309
310
This was a bug in which the validation callback was invoked eagerly, even on a cache
310
311
hit. As a result, cache hits did not demonstrate their expected performance gain.
311
312
"""
313
+ uri = "https://example.com/schema1.json"
314
+
312
315
# 1: construct some perfectly good data (it doesn't really matter what it is)
313
316
add_default_response ()
314
317
# 2: put equivalent data on disk
315
- (cache_dir / "check_jsonschema" / "downloads" ).mkdir (parents = True )
316
- f = cache_dir / "check_jsonschema" / "downloads" / "schema1.json"
317
- f .write_text ("{}" )
318
+ inject_cached_download (uri , "{}" )
318
319
319
320
# 3: construct a validator which marks that it ran in a variable
320
321
validator_ran = False
@@ -327,7 +328,6 @@ def dummy_validate_bytes(data):
327
328
# and use the above validation method
328
329
cd = CacheDownloader ().bind (
329
330
"https://example.com/schema1.json" ,
330
- filename = str (f ),
331
331
validation_callback = dummy_validate_bytes ,
332
332
)
333
333
0 commit comments