RuinedHugg / tests /test_space_models.py
jhonier23's picture
Stage Ragnarok locally before SDXL pipeline load
98334dd
Raw History Blame Contribute Delete
3.11 kB
import hashlib
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
import space_models
class FakeResponse:
def __init__(self, body):
self.body = body
self.headers = {"content-length": str(len(body))}
def __enter__(self):
return self
def __exit__(self, *_):
return False
def raise_for_status(self):
pass
def iter_content(self, chunk_size):
yield self.body[:3]
yield self.body[3:]
class SpaceModelsTests(unittest.TestCase):
def setUp(self):
space_models._verified_paths.clear()
def test_downloads_verifies_and_reuses_checkpoint(self):
body = b"checkpoint test content"
with tempfile.TemporaryDirectory(dir=Path(__file__).parent) as temporary:
with patch.object(space_models, "model_cache_dir", return_value=Path(temporary)), \
patch.object(space_models, "RAGNAROK_SHA256", hashlib.sha256(body).hexdigest()), \
patch.object(space_models.requests, "get", return_value=FakeResponse(body)) as get:
path = space_models.prepare_model(space_models.RAGNAROK)
self.assertEqual(Path(path).read_bytes(), body)
self.assertEqual(space_models.prepare_model(space_models.RAGNAROK), path)
get.assert_called_once()
def test_rejects_bad_download_and_cleans_partial_file(self):
with tempfile.TemporaryDirectory(dir=Path(__file__).parent) as temporary:
with patch.object(space_models, "model_cache_dir", return_value=Path(temporary)), \
patch.object(space_models.requests, "get", return_value=FakeResponse(b"wrong")):
with self.assertRaisesRegex(ValueError, "SHA-256"):
space_models.prepare_model(space_models.RAGNAROK)
self.assertEqual(list(Path(temporary).iterdir()), [])
def test_stages_checkpoint_locally_with_checksum(self):
body = b"bucket checkpoint"
with tempfile.TemporaryDirectory(dir=Path(__file__).parent) as temporary:
source = Path(temporary) / "bucket.safetensors"
target = Path(temporary) / "local.safetensors"
source.write_bytes(body)
checksum = hashlib.sha256(body).hexdigest()
self.assertEqual(space_models.stage_checkpoint(source, target, checksum), str(target))
self.assertEqual(target.read_bytes(), body)
self.assertEqual(space_models.stage_checkpoint(source, target, checksum), str(target))
target.write_bytes(b"corrupt")
with self.assertRaisesRegex(ValueError, "SHA-256"):
space_models.stage_checkpoint(source, target, "wrong")
self.assertFalse(target.with_suffix(".safetensors.part").exists())
def test_base_model_needs_no_civitai_download(self):
with patch.object(space_models.requests, "get") as get:
self.assertEqual(space_models.prepare_model(space_models.SDXL_BASE), "")
get.assert_not_called()
if __name__ == "__main__":
unittest.main()