152 lines
4.5 KiB
Python
152 lines
4.5 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"
|