6
6
7
7
import pytest
8
8
from requests .sessions import Session
9
- from requests_testadapter import TestAdapter , TestSession
9
+ import requests_mock
10
10
11
11
from invokeai .app .services .config import InvokeAIAppConfig
12
12
from invokeai .app .services .download import DownloadQueueService , DownloadQueueServiceBase
@@ -213,81 +213,83 @@ def mm2_model_manager(
213
213
@pytest .fixture
214
214
def mm2_session (embedding_file : Path , diffusers_dir : Path ) -> Session :
215
215
"""This fixtures defines a series of mock URLs for testing download and installation."""
216
- sess : Session = TestSession ()
217
- sess .mount (
216
+ sess = Session ()
217
+ adapter = requests_mock .Adapter ()
218
+ sess .mount ("http://" , adapter )
219
+ sess .mount ("https://" , adapter )
220
+ adapter .register_uri (
221
+ "GET" ,
218
222
"https://test.com/missing_model.safetensors" ,
219
- TestAdapter (
220
- b"missing" ,
221
- status = 404 ,
222
- ),
223
+ text = "missing" ,
224
+ status_code = 404 ,
225
+ reason = "NOT FOUND" ,
223
226
)
224
- sess .mount (
227
+ adapter .register_uri (
228
+ "GET" ,
225
229
"https://huggingface.co/api/models/stabilityai/sdxl-turbo" ,
226
- TestAdapter (
227
- RepoHFMetadata1 ,
228
- headers = {"Content-Type" : "application/json; charset=utf-8" , "Content-Length" : len (RepoHFMetadata1 )},
229
- ),
230
- )
231
- sess .mount (
232
- "https://huggingface.co/api/models/stabilityai/sdxl-turbo-nofp16" ,
233
- TestAdapter (
234
- RepoHFMetadata1_nofp16 ,
235
- headers = {"Content-Type" : "application/json; charset=utf-8" , "Content-Length" : len (RepoHFMetadata1_nofp16 )},
236
- ),
230
+ content = RepoHFMetadata1 ,
231
+ headers = {"Content-Type" : "application/json; charset=utf-8" , "Content-Length" : str (len (RepoHFMetadata1 ))},
237
232
)
238
- sess .mount (
239
- "https://civitai.com/api/v1/model-versions/242807" ,
240
- TestAdapter (
241
- RepoCivitaiVersionMetadata1 ,
233
+ (
234
+ adapter .register_uri (
235
+ "GET" ,
236
+ "https://huggingface.co/api/models/stabilityai/sdxl-turbo-nofp16" ,
237
+ content = RepoHFMetadata1_nofp16 ,
242
238
headers = {
243
- "Content-Length" : len (RepoCivitaiVersionMetadata1 ),
239
+ "Content-Type" : "application/json; charset=utf-8" ,
240
+ "Content-Length" : str (len (RepoHFMetadata1_nofp16 )),
244
241
},
245
242
),
246
243
)
247
- sess .mount (
244
+ adapter .register_uri (
245
+ "GET" ,
246
+ "https://civitai.com/api/v1/model-versions/242807" ,
247
+ content = RepoCivitaiVersionMetadata1 ,
248
+ headers = {"Content-Length" : str (len (RepoCivitaiVersionMetadata1 ))},
249
+ )
250
+ adapter .register_uri (
251
+ "GET" ,
248
252
"https://civitai.com/api/v1/models/215485" ,
249
- TestAdapter (
250
- RepoCivitaiModelMetadata1 ,
251
- headers = {
252
- "Content-Length" : len (RepoCivitaiModelMetadata1 ),
253
- },
254
- ),
253
+ content = RepoCivitaiModelMetadata1 ,
254
+ headers = {"Content-Length" : str (len (RepoCivitaiModelMetadata1 ))},
255
255
)
256
- sess .mount (
256
+ adapter .register_uri (
257
+ "GET" ,
257
258
"https://huggingface.co/stabilityai/sdxl-turbo/resolve/main/model_index.json" ,
258
- TestAdapter (
259
- RepoHFModelJson1 ,
260
- headers = {
261
- "Content-Length" : len (RepoHFModelJson1 ),
262
- },
263
- ),
259
+ content = RepoHFModelJson1 ,
260
+ headers = {"Content-Length" : str (len (RepoHFModelJson1 ))},
264
261
)
265
262
with open (embedding_file , "rb" ) as f :
266
263
data = f .read () # file is small - just 15K
267
- sess .mount (
264
+ adapter .register_uri (
265
+ "GET" ,
268
266
"https://www.test.foo/download/test_embedding.safetensors" ,
269
- TestAdapter (data , headers = {"Content-Type" : "application/octet-stream" , "Content-Length" : len (data )}),
267
+ content = data ,
268
+ headers = {"Content-Type" : "application/octet-stream" , "Content-Length" : str (len (data ))},
270
269
)
271
- sess .mount (
270
+ adapter .register_uri (
271
+ "GET" ,
272
272
"https://huggingface.co/api/models/stabilityai/sdxl-turbo" ,
273
- TestAdapter (
274
- RepoHFMetadata1 ,
275
- headers = {"Content-Type" : "application/json; charset=utf-8" , "Content-Length" : len (RepoHFMetadata1 )},
276
- ),
273
+ content = RepoHFMetadata1 ,
274
+ headers = {"Content-Type" : "application/json; charset=utf-8" , "Content-Length" : str (len (RepoHFMetadata1 ))},
275
+ )
276
+ adapter .register_uri (
277
+ "GET" ,
278
+ "https://huggingface.co/api/models/stabilityai/sdxl-turbo/revision/ModelRepoVariant.Default?blobs=True" ,
279
+ content = RepoHFMetadata1 ,
280
+ headers = {"Content-Type" : "application/json; charset=utf-8" , "Content-Length" : str (len (RepoHFMetadata1 ))},
277
281
)
278
- sess .mount (
282
+ adapter .register_uri (
283
+ "GET" ,
279
284
"https://huggingface.co/api/models/InvokeAI-test/textual_inversion_tests?blobs=True" ,
280
- TestAdapter (
281
- HFTestLoraMetadata ,
282
- headers = {"Content-Type" : "application/json; charset=utf-8" , "Content-Length" : len (HFTestLoraMetadata )},
283
- ),
285
+ content = HFTestLoraMetadata ,
286
+ headers = {"Content-Type" : "application/json; charset=utf-8" , "Content-Length" : str (len (HFTestLoraMetadata ))},
284
287
)
285
- sess .mount (
288
+ adapter .register_uri (
289
+ "GET" ,
286
290
"https://huggingface.co/InvokeAI-test/textual_inversion_tests/resolve/main/learned_embeds-steps-1000.safetensors" ,
287
- TestAdapter (
288
- data ,
289
- headers = {"Content-Type" : "application/json; charset=utf-8" , "Content-Length" : len (data )},
290
- ),
291
+ content = data ,
292
+ headers = {"Content-Type" : "application/json; charset=utf-8" , "Content-Length" : str (len (data ))},
291
293
)
292
294
for root , _ , files in os .walk (diffusers_dir ):
293
295
for name in files :
@@ -296,55 +298,57 @@ def mm2_session(embedding_file: Path, diffusers_dir: Path) -> Session:
296
298
url = f"https://huggingface.co/stabilityai/sdxl-turbo/resolve/main/{ url_base } "
297
299
with open (path , "rb" ) as f :
298
300
data = f .read ()
299
- sess .mount (
301
+ adapter .register_uri (
302
+ "GET" ,
300
303
url ,
301
- TestAdapter (
302
- data ,
303
- headers = {
304
- "Content-Type" : "application/json; charset=utf-8" ,
305
- "Content-Length" : len (data ),
306
- },
307
- ),
304
+ content = data ,
305
+ headers = {
306
+ "Content-Type" : "application/json; charset=utf-8" ,
307
+ "Content-Length" : str (len (data )),
308
+ },
308
309
)
309
310
310
311
for i in ["12345" , "9999" , "54321" ]:
311
312
content = (
312
313
b"I am a safetensors file " + bytearray (i , "utf-8" ) + bytearray (32_000 )
313
314
) # for pause tests, must make content large
314
- sess .mount (
315
+ adapter .register_uri (
316
+ "GET" ,
315
317
f"http://www.civitai.com/models/{ i } " ,
316
- TestAdapter (
317
- content ,
318
- headers = {
319
- "Content-Length" : len (content ),
320
- "Content-Disposition" : f'filename="mock{ i } .safetensors"' ,
321
- },
322
- ),
318
+ content = content ,
319
+ headers = {
320
+ "Content-Length" : str (len (content )),
321
+ "Content-Disposition" : f'filename="mock{ i } .safetensors"' ,
322
+ },
323
323
)
324
324
325
- sess .mount (
325
+ adapter .register_uri (
326
+ "GET" ,
326
327
"http://www.huggingface.co/foo.txt" ,
327
- TestAdapter (
328
- content ,
329
- headers = {
330
- "Content-Length" : len (content ),
331
- "Content-Disposition" : 'filename="foo.safetensors"' ,
332
- },
333
- ),
328
+ content = content ,
329
+ headers = {
330
+ "Content-Length" : str (len (content )),
331
+ "Content-Disposition" : 'filename="foo.safetensors"' ,
332
+ },
334
333
)
335
334
336
335
# here are some malformed URLs to test
337
336
# missing the content length
338
- sess .mount (
337
+ adapter .register_uri (
338
+ "GET" ,
339
339
"http://www.civitai.com/models/missing" ,
340
- TestAdapter (
341
- b"Missing content length" ,
342
- headers = {
343
- "Content-Disposition" : 'filename="missing.txt"' ,
344
- },
345
- ),
340
+ text = "Missing content length" ,
341
+ headers = {
342
+ "Content-Disposition" : 'filename="missing.txt"' ,
343
+ },
346
344
)
347
345
# not found test
348
- sess .mount ("http://www.civitai.com/models/broken" , TestAdapter (b"Not found" , status = 404 ))
346
+ adapter .register_uri (
347
+ "GET" ,
348
+ "http://www.civitai.com/models/broken" ,
349
+ text = "Not found" ,
350
+ status_code = 404 ,
351
+ reason = "NOT FOUND" ,
352
+ )
349
353
350
354
return sess
0 commit comments