k9cli's picture
Flatten package contents to repo root
de01f6a verified
Raw History Blame Contribute Delete
1.83 kB
"""Tests for data models and IO helpers."""
from __future__ import annotations
from pathlib import Path
import pytest
from video_vec2wav2_tokenizer.utils.io import (
DatasetEntry,
Segment,
clip_index_name,
iter_video_files,
read_jsonl,
write_jsonl,
)
def test_segment_roundtrip() -> None:
seg = Segment(start=0.0, end=2.5, text="example sentence")
assert seg.duration == 2.5
assert seg.to_dict() == {"start": 0.0, "end": 2.5, "text": "example sentence"}
assert Segment.from_dict(seg.to_dict()) == seg
def test_dataset_entry_jsonl_shape() -> None:
entry = DatasetEntry(audio="audio/000001.wav", text="Hello world", duration=2.5)
row = entry.to_jsonl_dict()
assert row == {"audio": "audio/000001.wav", "text": "Hello world", "duration": 2.5}
def test_dataset_entry_with_speaker() -> None:
entry = DatasetEntry(
audio="audio/000001.wav", text="hi", duration=1.0, speaker="speaker_002"
)
assert entry.to_jsonl_dict()["speaker"] == "speaker_002"
def test_clip_index_name() -> None:
assert clip_index_name(1) == "000001.wav"
assert clip_index_name(42) == "000042.wav"
def test_jsonl_roundtrip(tmp_path: Path) -> None:
rows = [{"a": 1}, {"b": "x"}]
path = tmp_path / "out.jsonl"
assert write_jsonl(path, rows) == 2
assert list(read_jsonl(path)) == rows
def test_iter_video_files(tmp_path: Path) -> None:
(tmp_path / "a.mp4").write_bytes(b"")
(tmp_path / "sub").mkdir()
(tmp_path / "sub" / "b.mkv").write_bytes(b"")
(tmp_path / "note.txt").write_bytes(b"")
found = [p.name for p in iter_video_files(tmp_path)]
assert found == ["a.mp4", "b.mkv"]
def test_iter_video_files_missing(tmp_path: Path) -> None:
with pytest.raises(FileNotFoundError):
list(iter_video_files(tmp_path / "nope"))