Download tests/test_batch_upload.py from Jiaxuannn/PAR: direct link, hf CLI and curl.
- Browser
- Download file 8.65 kB
-
https://huggingface.co/datasets/Jiaxuannn/PAR/resolve/main/tests/test_batch_upload.py
- Command line
-
hf download hf://datasets/Jiaxuannn/PAR/tests/test_batch_upload.py
-
curl -L -o test_batch_upload.py https://huggingface.co/datasets/Jiaxuannn/PAR/resolve/main/tests/test_batch_upload.py
8.65 kB
| import importlib.util | |
| import json | |
| from pathlib import Path | |
| import sys | |
| import tempfile | |
| import unittest | |
| from unittest.mock import patch | |
| from types import SimpleNamespace | |
| SCRIPT = Path(__file__).resolve().parents[1] / "scripts" / "batch_upload.py" | |
| spec = importlib.util.spec_from_file_location("batch_upload", SCRIPT) | |
| b = importlib.util.module_from_spec(spec) | |
| spec.loader.exec_module(b) | |
| class BatchTests(unittest.TestCase): | |
| def setUp(self): | |
| self.tmp = tempfile.TemporaryDirectory() | |
| self.root = Path(self.tmp.name) | |
| self.con = b.open_ledger(self.root / "state" / "ledger.sqlite") | |
| def tearDown(self): | |
| self.con.close() | |
| self.tmp.cleanup() | |
| def manifest(self, event="event-1"): | |
| folder = self.root / "incoming" / event | |
| folder.mkdir(parents=True, exist_ok=True) | |
| assets = [] | |
| for kind in sorted(b.KINDS): | |
| path = folder / (kind + ".bin") | |
| path.write_bytes((event + kind).encode() * 3) | |
| assets.append(dict(kind=kind, local_path=str(path), remote_path=f"data/events/{event}/{path.name}", | |
| size_bytes=path.stat().st_size, sha256=b.sha256_file(path))) | |
| path = folder / "event.manifest.json" | |
| path.write_text(json.dumps(dict(schema_version="1", event_id=event, | |
| quality_status="full_av_text_verified", assets=assets))) | |
| return path | |
| def register(self, event="event-1"): | |
| rows = list(b.read_manifests(self.manifest(event))) | |
| b.register(self.con, "Jiaxuannn/PAR", rows) | |
| return rows | |
| def mark_verified(self, rows): | |
| with self.con: | |
| for row in rows: | |
| self.con.execute("UPDATE transfer_assets SET state='verified',verified_commit='abc123',verification_method='hub_lfs_sha256_and_size',verified_at='2026-11-26T00:00:00+00:00' WHERE remote_path=?", (row["remote_path"],)) | |
| def test_reject_incomplete_and_pending(self): | |
| path = self.manifest() | |
| obj = json.loads(path.read_text()) | |
| obj["quality_status"] = "pending" | |
| path.write_text(json.dumps(obj)) | |
| with self.assertRaises(ValueError): | |
| list(b.read_manifests(path)) | |
| obj["quality_status"] = "full_av_text_verified" | |
| obj["assets"] = obj["assets"][:-1] | |
| path.write_text(json.dumps(obj)) | |
| with self.assertRaises(ValueError): | |
| list(b.read_manifests(path)) | |
| def test_batches_whole_events_no_implicit_flush(self): | |
| one = [dict(size_bytes=4)] * 4 | |
| two = [dict(size_bytes=3)] * 4 | |
| self.assertEqual(list(b.plan_batches([one], 20, False)), []) | |
| batches = list(b.plan_batches([one, two], 20, False)) | |
| self.assertEqual([len(x) for x in batches], [8]) | |
| self.assertEqual(len(list(b.plan_batches([one], 20, True))[0]), 4) | |
| def test_registration_idempotent_but_no_overwrite(self): | |
| rows = self.register() | |
| b.register(self.con, "Jiaxuannn/PAR", rows) | |
| self.assertEqual(self.con.execute("SELECT COUNT(*) FROM transfer_assets").fetchone()[0], 4) | |
| rows[0]["sha256"] = "0" * 64 | |
| with self.assertRaises(ValueError): | |
| b.register(self.con, "Jiaxuannn/PAR", rows) | |
| def test_hash_mismatch_blocks_upload(self): | |
| rows = self.register() | |
| Path(rows[0]["local_path"]).write_bytes(b"changed") | |
| with self.assertRaises(ValueError): | |
| b.verify_local(rows[0]) | |
| def test_xet_or_blob_is_not_file_sha256(self): | |
| self.assertIsNone(b.lfs_sha({"xet_hash": "a" * 64, "blob_id": "a" * 64})) | |
| self.assertEqual(b.lfs_sha({"lfs": {"sha256": "b" * 64}}), "b" * 64) | |
| def test_cleanup_requires_all_assets_verified(self): | |
| rows = self.register() | |
| self.mark_verified(rows[:-1]) | |
| b.cleanup_verified(self.con, "Jiaxuannn/PAR", self.root / "incoming") | |
| self.assertTrue(all(Path(r["local_path"]).exists() for r in rows)) | |
| def test_cleanup_preserves_text_manifest_and_receipt(self): | |
| rows = self.register() | |
| self.mark_verified(rows) | |
| b.cleanup_verified(self.con, "Jiaxuannn/PAR", self.root / "incoming") | |
| for row in rows: | |
| self.assertEqual(Path(row["local_path"]).exists(), row["kind"] in {"text", "metadata"}) | |
| folder = self.root / "incoming" / "event-1" | |
| marker = json.loads((folder / "remote_verified.json").read_text()) | |
| self.assertTrue(marker["verified"] and marker["all_assets_verified"]) | |
| self.assertEqual(marker["commit_sha"], "abc123") | |
| self.assertTrue((folder / "event.manifest.json").exists()) | |
| # Re-import after cleanup is resumable, without requiring deleted media. | |
| b.register(self.con, "Jiaxuannn/PAR", list(b.read_manifests(folder / "event.manifest.json"))) | |
| self.assertEqual(b.pending_groups(self.con, "Jiaxuannn/PAR"), []) | |
| def test_cleanup_refuses_modified_media(self): | |
| rows = self.register() | |
| self.mark_verified(rows) | |
| audio = next(r for r in rows if r["kind"] == "audio") | |
| Path(audio["local_path"]).write_bytes(b"different file") | |
| with self.assertRaises(ValueError): | |
| b.cleanup_verified(self.con, "Jiaxuannn/PAR", self.root / "incoming") | |
| self.assertTrue(Path(audio["local_path"]).exists()) | |
| def test_lost_commit_response_recovers_without_duplicate_upload(self): | |
| rows = self.register() | |
| class FakeAPI: | |
| token = "unit-test-token-placeholder" | |
| head = "before" | |
| files = {} | |
| commits = 0 | |
| def repo_info(self, *args, **kwargs): | |
| return SimpleNamespace(sha=self.head) | |
| def get_paths_info(self, repo, paths, **kwargs): | |
| return [self.files[path] for path in paths if path in self.files] | |
| def create_commit(self, **kwargs): | |
| self.commits += 1 | |
| assert kwargs["parent_commit"] == self.head | |
| for op in kwargs["operations"]: | |
| p = Path(op.path_or_fileobj) | |
| self.files[op.path_in_repo] = SimpleNamespace(path=op.path_in_repo, | |
| size=p.stat().st_size, lfs=SimpleNamespace(sha256=b.sha256_file(p))) | |
| self.head = "committed-on-server" | |
| raise ConnectionError("response lost after server commit") | |
| api = FakeAPI() | |
| fake_module = SimpleNamespace(CommitOperationAdd=lambda **kwargs: SimpleNamespace(**kwargs)) | |
| with patch.dict(sys.modules, {"huggingface_hub": fake_module}), patch.object(b.time, "sleep"): | |
| b.upload_batch(self.con, api, "Jiaxuannn/PAR", rows, "main", self.root / "receipts", 2) | |
| self.assertEqual(api.commits, 1) | |
| self.assertEqual(b.pending_groups(self.con, "Jiaxuannn/PAR"), []) | |
| self.assertTrue((self.root / "incoming" / "event-1" / "remote_verified.json").exists()) | |
| self.assertTrue(all(Path(row["local_path"]).exists() for row in rows)) | |
| def test_receipt_recovers_after_verified_db_commit_crash(self): | |
| rows = self.register() | |
| batch_id, record, receipt_row = b.batch_record(self.con, "Jiaxuannn/PAR", rows, self.root / "receipts") | |
| self.mark_verified(rows) | |
| with self.con: | |
| self.con.execute("UPDATE transfer_batches SET state='verified',commit_sha='abc123',completed_at='2026-11-26T00:00:00+00:00' WHERE batch_id=?", (batch_id,)) | |
| receipt = self.root / "receipts" / f"{batch_id}.verified.json" | |
| self.assertFalse(receipt.exists()) | |
| # Restart sees no upload work, but must still recover publishable evidence. | |
| self.assertEqual(b.pending_groups(self.con, "Jiaxuannn/PAR"), []) | |
| b.recover_verified_receipts(self.con, "Jiaxuannn/PAR", self.root / "receipts") | |
| proof = json.loads(receipt.read_text()) | |
| self.assertEqual(proof["verified_commit"], "abc123") | |
| self.assertEqual(len(proof["assets"]), 4) | |
| self.assertTrue(proof["recovered_from_transfer_ledger"]) | |
| b.cleanup_verified(self.con, "Jiaxuannn/PAR", self.root / "incoming") | |
| self.assertTrue(receipt.exists()) | |
| self.assertTrue((self.root / "incoming" / "event-1" / "remote_verified.json").exists()) | |
| def test_remote_size_and_lfs_hash_must_match(self): | |
| row = self.register()[0] | |
| info = dict(size=row["size_bytes"], lfs=dict(sha256=row["sha256"])) | |
| self.assertEqual(b.verify_remote(row, info, None, "repo", "commit"), "hub_lfs_sha256_and_size") | |
| info["lfs"]["sha256"] = "0" * 64 | |
| with self.assertRaises(ValueError): | |
| b.verify_remote(row, info, None, "repo", "commit") | |
| if __name__ == "__main__": | |
| unittest.main() | |