File size: 2,130 Bytes
e434719 | 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 | """Tests for the cached-showcase registry + loader. No gradio/models needed.
Run: .venv/bin/python src/examples_test.py (or pytest)
"""
import json
import os
import sys
import tempfile
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from src import examples # noqa: E402
def test_three_examples_span_the_slider():
assert [e["level"] for e in examples.EXAMPLES] == [1, 5, 10]
assert len({e["id"] for e in examples.EXAMPLES}) == 3
for e in examples.EXAMPLES:
assert e["resume"].strip() and e["jd"].strip() and e["genre"]
def test_load_example_missing_media_is_all_none():
examples.EX_DIR = tempfile.mkdtemp() # empty
ex, lyr, tr, song, vid = examples.load_example(0)
assert ex["id"] == examples.EXAMPLES[0]["id"]
assert lyr is None and tr is None and song is None and vid is None
assert examples.has_cache(0) is False
def test_load_example_reads_committed_bundle():
d = tempfile.mkdtemp()
examples.EX_DIR = d
eid = examples.EXAMPLES[2]["id"]
with open(os.path.join(d, f"{eid}.lyrics.txt"), "w", encoding="utf-8") as f:
f.write('🎵 "x" — Drill\n[verse]\nhi')
with open(os.path.join(d, f"{eid}.trace.json"), "w", encoding="utf-8") as f:
json.dump({"engine": "gpt-oss-20b"}, f)
with open(os.path.join(d, f"{eid}.song.mp3"), "wb") as f:
f.write(b"ID3fake")
ex, lyr, tr, song, vid = examples.load_example(2)
assert "[verse]" in lyr and tr["engine"] == "gpt-oss-20b"
assert song.endswith(f"{eid}.song.mp3") and vid is None # no video file present
assert examples.has_cache(2) is True
def _run():
fns = {k: v for k, v in globals().items() if k.startswith("test_") and callable(v)}
failed = 0
for name, fn in fns.items():
try:
fn()
print(f"PASS {name}")
except Exception as e:
failed += 1
import traceback
print(f"FAIL {name}: {e}")
traceback.print_exc()
print(f"\n{len(fns) - failed}/{len(fns)} passed")
sys.exit(1 if failed else 0)
if __name__ == "__main__":
_run()
|