Files
unmix/tests/test_schema.py

232 lines
7.1 KiB
Python

"""Unit tests for Song-Paket Pydantic schemas (Round-trip & validation)."""
import json
import pytest
from pydantic import ValidationError
from songsplit.schema import (
ChordsDocument,
MetaDocument,
ModelInfo,
SourceFileInfo,
StrummingDocument,
TempoAnchor,
TempoDocument,
VocalsDocument,
VocalSyllable,
)
def test_meta_document_roundtrip():
doc = MetaDocument(
title="Test Song",
artist="Test Artist",
language="de",
duration_sec=180.5,
source_file=SourceFileInfo(path="/tmp/test.wav", sha256="abc123hash", size_bytes=1048576),
key="Am",
capo=2,
models_used=[ModelInfo(stage="s2_stems", name="htdemucs_ft", version="1.0.0")],
created_at="2026-11-02T10:00:00Z",
stages_done=["s1_extract", "s2_stems"],
stages_failed={"s3_lyrics": "Sample error"},
)
json_str = doc.model_dump_json()
loaded = MetaDocument.model_validate_json(json_str)
assert loaded == doc
assert loaded.schema_version == "1.0"
assert loaded.title == "Test Song"
assert loaded.stages_done == ["s1_extract", "s2_stems"]
def test_vocals_document_roundtrip_and_spec_example():
spec_example = {
"schema_version": "1.0",
"generated_by": {
"stage": "s4_pitch",
"model": "torchcrepe-full",
"model_version": "1.0",
"params": {"hop": 10},
},
"lines": [
{
"start": 12.34,
"end": 15.80,
"syllables": [
{
"text": "Hel",
"start": 12.34,
"end": 12.61,
"midi": 64.0,
"golden": False,
"confidence": 0.88,
"source": "auto",
}
],
}
],
}
doc = VocalsDocument.model_validate(spec_example)
assert len(doc.lines) == 1
syl = doc.lines[0].syllables[0]
assert syl.text == "Hel"
assert syl.midi == 64.0
assert syl.confidence == 0.88
assert syl.source == "auto"
dumped = json.loads(doc.model_dump_json())
assert dumped["lines"][0]["syllables"][0]["text"] == "Hel"
def test_vocals_validation_error():
with pytest.raises(ValidationError):
# confidence > 1.0 is invalid
VocalSyllable(text="test", start=1.0, end=2.0, midi=60.0, confidence=1.5)
def test_tempo_document_roundtrip_and_spec_example():
spec_example = {
"schema_version": "1.0",
"anchors": [{"sec": 0.52, "beat": 0.0, "bpm": 120.0}],
"time_signature": {"numerator": 4, "denominator": 4},
"downbeat_offset": 0,
}
doc = TempoDocument.model_validate(spec_example)
assert len(doc.anchors) == 1
assert doc.anchors[0].bpm == 120.0
assert doc.time_signature.numerator == 4
dumped = json.loads(doc.model_dump_json())
assert dumped["anchors"][0]["bpm"] == 120.0
def test_tempo_invalid_bpm():
with pytest.raises(ValidationError):
TempoAnchor(sec=0.0, beat=0.0, bpm=0.0)
def test_chords_document_roundtrip_and_spec_example():
spec_example = {
"schema_version": "1.0",
"chords": [
{
"start_beat": 0.0,
"end_beat": 4.0,
"label": "Am",
"root": "A",
"quality": "min",
"bass": None,
"confidence": 0.74,
"source": "auto",
}
],
}
doc = ChordsDocument.model_validate(spec_example)
assert len(doc.chords) == 1
chord = doc.chords[0]
assert chord.label == "Am"
assert chord.root == "A"
assert chord.quality == "min"
assert chord.confidence == 0.74
dumped = json.loads(doc.model_dump_json())
assert dumped["chords"][0]["label"] == "Am"
def test_strumming_document_roundtrip_and_spec_example():
spec_example = {
"schema_version": "1.0",
"events": [{"beat": 0.0, "direction": "down", "strength": 0.9, "confidence": 0.5, "source": "auto"}],
"patterns": [{"start_beat": 0.0, "end_beat": 16.0, "pattern": "D . D U . U D U", "grid": "8th"}],
}
doc = StrummingDocument.model_validate(spec_example)
assert len(doc.events) == 1
assert doc.events[0].direction == "down"
assert doc.events[0].strength == 0.9
assert len(doc.patterns) == 1
assert doc.patterns[0].pattern == "D . D U . U D U"
dumped = json.loads(doc.model_dump_json())
assert dumped["events"][0]["direction"] == "down"
def test_manifest_document_roundtrip():
from songsplit.schema.manifest import ManifestDocument, ManifestFileInfo, ManifestStageInfo
manifest = ManifestDocument(
created="2026-11-02T11:40:00+01:00",
modified="2026-11-02T11:55:12+01:00",
files=[
ManifestFileInfo(path="meta.json", role="meta", sha256="abc", size=100),
ManifestFileInfo(path="stems/vocals.wav", role="stem", stem="vocals", sha256="def", size=2000),
],
stages={
"stems": ManifestStageInfo(status="done", model="htdemucs_ft"),
},
)
dumped = json.loads(manifest.model_dump_json())
assert dumped["format"] == "songpack"
assert dumped["schema_version"] == "1.0"
assert len(dumped["files"]) == 2
assert dumped["stages"]["stems"]["status"] == "done"
loaded = ManifestDocument.model_validate(dumped)
assert loaded.format == "songpack"
assert loaded.files[0].path == "meta.json"
def test_tracks_document_kinds():
from songsplit.schema.tracks import DrumEvent, NoteEvent, StrumEvent, StrumPattern, TrackDocument
# Strumming track
t_strum = TrackDocument(
name="guitar",
instrument="guitar",
kind="strumming",
stem="guitar",
events=[StrumEvent(id="e_0001", beat=0.0, direction="down", strength=0.9, confidence=0.5)],
patterns=[StrumPattern(id="p_0001", start_beat=0.0, end_beat=16.0, pattern="D . D U . U D U", grid="8th")],
)
assert t_strum.kind == "strumming"
assert len(t_strum.events) == 1
assert len(t_strum.patterns) == 1
# Notes track
t_notes = TrackDocument(
name="bass",
instrument="bass",
kind="notes",
stem="bass",
events=[NoteEvent(id="n_0001", start_beat=4.0, end_beat=4.5, midi=45, velocity=0.7)],
)
assert t_notes.kind == "notes"
assert len(t_notes.events) == 1
assert t_notes.events[0].midi == 45
# Drums track
t_drums = TrackDocument(
name="drums",
instrument="drums",
kind="drums",
stem="drums",
events=[DrumEvent(id="d_0001", beat=0.0, piece="kick", strength=0.9)],
)
assert t_drums.kind == "drums"
assert len(t_drums.events) == 1
assert t_drums.events[0].piece == "kick"
def test_vocals_f0_document():
from songsplit.schema.vocals_f0 import VocalsF0Document
f0 = VocalsF0Document(
hop_sec=0.01,
start_sec=0.0,
hz=[None, None, 220.0, 222.0],
confidence=[0.0, 0.1, 0.95, 0.98],
)
dumped = json.loads(f0.model_dump_json())
assert dumped["hop_sec"] == 0.01
assert dumped["hz"][2] == 220.0