Download test_archive.py from openmodelai/Terra-2.5: direct link, hf CLI and curl.
- Browser
- Download file 2.03 kB
-
https://huggingface.co/openmodelai/Terra-2.5/resolve/main/test_archive.py
- Command line
-
hf download hf://openmodelai/Terra-2.5/test_archive.py
-
curl -L -o test_archive.py https://huggingface.co/openmodelai/Terra-2.5/resolve/main/test_archive.py
2.03 kB
| import hashlib | |
| import json | |
| import zipfile | |
| import pytest | |
| from scripts.archive import REQUIRED, pack, restore | |
| def checkpoint(tmp_path): | |
| source = tmp_path / "source" | |
| source.mkdir() | |
| for name in REQUIRED: | |
| (source / name).write_text("test data: " + name) | |
| return source | |
| def test_roundtrip(tmp_path, compression): | |
| source = checkpoint(tmp_path) | |
| archive = tmp_path / "checkpoint.zip" | |
| checksum = pack(source, archive, compression=compression) | |
| assert checksum == hashlib.sha256(archive.read_bytes()).hexdigest() | |
| dest = tmp_path / "restored" | |
| restore(archive, dest) | |
| assert {p.name for p in dest.iterdir()} == REQUIRED | |
| for name in REQUIRED: | |
| assert (source / name).read_bytes() == (dest / name).read_bytes() | |
| with pytest.raises(ValueError, match="уже существует"): | |
| restore(archive, dest) | |
| def test_unsafe_paths(tmp_path, name): | |
| archive = tmp_path / "bad.zip" | |
| with zipfile.ZipFile(archive, "w") as z: | |
| z.writestr(name, "malicious") | |
| with pytest.raises(ValueError, match="Небезопасный"): | |
| restore(archive, tmp_path / "restored") | |
| assert not (tmp_path / "restored").exists() | |
| def test_corrupted_checksum(tmp_path): | |
| archive = tmp_path / "bad.zip" | |
| with zipfile.ZipFile(archive, "w") as z: | |
| for name in REQUIRED: | |
| z.writestr(name, "corrupted") | |
| z.writestr("manifest.json", json.dumps({name: "0" * 64 for name in REQUIRED})) | |
| with pytest.raises(ValueError, match="контрольная сумма"): | |
| restore(archive, tmp_path / "restored") | |
| assert not (tmp_path / "restored").exists() | |
| def test_pack_requires_complete_checkpoint(tmp_path): | |
| source = tmp_path / "empty" | |
| source.mkdir() | |
| with pytest.raises(ValueError, match="Неполный"): | |
| pack(source, tmp_path / "checkpoint.zip") | |