File size: 5,679 Bytes
3f3265f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | import json
import tempfile
import unittest
from pathlib import Path
from scripts.generate_questions import build_questions, write_metadata_template
from scripts.verify_assets import (
check_embedding_cache,
check_metadata_cache,
check_questions,
sha256_file,
)
class ReleaseScriptTests(unittest.TestCase):
def test_generate_questions_from_image_directory(self):
with tempfile.TemporaryDirectory() as tmpdir:
root = Path(tmpdir)
images = root / "images"
images.mkdir()
(images / "123_4.jpg").write_bytes(b"fake")
(images / "123_5.png").write_bytes(b"fake")
(images / "123_10.png").write_bytes(b"fake")
(images / "notes.txt").write_text("ignored", encoding="utf-8")
questions = build_questions(images, "Prompt")
metadata_template = root / "metadata.json"
write_metadata_template(metadata_template, questions)
metadata = json.loads(metadata_template.read_text(encoding="utf-8"))
self.assertEqual([record["image_id"] for record in questions], [123004, 123005, 123010])
self.assertEqual(questions[0]["text"], "Prompt")
self.assertEqual(sorted(metadata), ["123"])
def test_recursive_question_generation_uses_basename_for_app_id(self):
with tempfile.TemporaryDirectory() as tmpdir:
root = Path(tmpdir)
images = root / "images"
nested = images / "nested"
nested.mkdir(parents=True)
(nested / "123_4.jpg").write_bytes(b"fake")
questions = build_questions(images, "Prompt", recursive=True)
metadata_template = root / "metadata.json"
write_metadata_template(metadata_template, questions)
metadata = json.loads(metadata_template.read_text(encoding="utf-8"))
self.assertEqual(questions[0]["image"], "nested/123_4.jpg")
self.assertEqual(sorted(metadata), ["123"])
def test_verify_questions_and_metadata_cache(self):
with tempfile.TemporaryDirectory() as tmpdir:
root = Path(tmpdir)
images = root / "images"
images.mkdir()
(images / "123_4.jpg").write_bytes(b"fake")
questions = root / "questions.jsonl"
questions.write_text(
json.dumps(
{
"question_id": 0,
"image": "123_4.jpg",
"image_id": 123004,
"text": "Prompt",
}
)
+ "\n",
encoding="utf-8",
)
metadata = root / "metadata.json"
metadata.write_text(
json.dumps(
{
"123": {
"app_name": "Test VR",
"app_description": "Description.",
}
}
),
encoding="utf-8",
)
report = check_questions(questions, images)
metadata_report = check_metadata_cache(metadata, report["app_ids"])
self.assertEqual(report["questions"], 1)
self.assertEqual(metadata_report["covered_app_ids"], 1)
def test_verify_metadata_cache_rejects_duplicates_and_missing_fields(self):
with tempfile.TemporaryDirectory() as tmpdir:
root = Path(tmpdir)
duplicate = root / "duplicate.jsonl"
duplicate.write_text(
"\n".join(
[
json.dumps(
{
"app_id": 123,
"app_name": "First",
"app_description": "Description.",
}
),
json.dumps(
{
"app_id": "123",
"app_name": "Second",
"app_description": "Description.",
}
),
]
)
+ "\n",
encoding="utf-8",
)
missing_field = root / "missing_field.json"
missing_field.write_text(
json.dumps({"123": {"app_name": "Test VR"}}),
encoding="utf-8",
)
with self.assertRaises(ValueError):
check_metadata_cache(duplicate, ["123"])
with self.assertRaises(ValueError):
check_metadata_cache(missing_field, ["123"])
def test_verify_external_embedding_cache_against_manifest(self):
with tempfile.TemporaryDirectory() as tmpdir:
root = Path(tmpdir)
cache = root / "embedding_dict.json"
cache.write_bytes(b"frozen-cache")
manifest = root / "cache_manifest.json"
manifest.write_text(
json.dumps(
{
"artifact": "embedding_dict.json",
"size_bytes": cache.stat().st_size,
"sha256": sha256_file(cache),
"entry_count": 1,
"embedding_dimension": 2,
}
),
encoding="utf-8",
)
report = check_embedding_cache(cache, manifest)
self.assertEqual(report["entry_count"], 1)
self.assertEqual(report["embedding_dimension"], 2)
if __name__ == "__main__":
unittest.main()
|