test: add comprehensive test suite for schemas, tempo map, runner, and cli
This commit is contained in:
104
tests/test_tempo.py
Normal file
104
tests/test_tempo.py
Normal file
@ -0,0 +1,104 @@
|
||||
"""Unit tests for Tempo-Map conversion (sec_to_beat and beat_to_sec)."""
|
||||
|
||||
import pytest
|
||||
|
||||
from songsplit.schema.tempo import TempoAnchor, TempoDocument
|
||||
from songsplit.tempo import beat_to_sec, sec_to_beat
|
||||
|
||||
|
||||
def test_empty_tempo_map_raises_error():
|
||||
empty_doc = TempoDocument(anchors=[])
|
||||
with pytest.raises(ValueError, match="no anchors"):
|
||||
sec_to_beat(1.0, empty_doc)
|
||||
with pytest.raises(ValueError, match="no anchors"):
|
||||
beat_to_sec(1.0, empty_doc)
|
||||
|
||||
|
||||
def test_constant_tempo_single_anchor():
|
||||
# 120 bpm = 2 beats per second = 0.5s per beat
|
||||
doc = TempoDocument(anchors=[TempoAnchor(sec=0.5, beat=0.0, bpm=120.0)])
|
||||
|
||||
# At anchor
|
||||
assert sec_to_beat(0.5, doc) == pytest.approx(0.0)
|
||||
assert beat_to_sec(0.0, doc) == pytest.approx(0.5)
|
||||
|
||||
# 1 second later = 2 beats
|
||||
assert sec_to_beat(1.5, doc) == pytest.approx(2.0)
|
||||
assert beat_to_sec(2.0, doc) == pytest.approx(1.5)
|
||||
|
||||
# 4 beats later = 2.0s duration -> 2.5s absolute
|
||||
assert beat_to_sec(4.0, doc) == pytest.approx(2.5)
|
||||
assert sec_to_beat(2.5, doc) == pytest.approx(4.0)
|
||||
|
||||
# Before anchor (extrapolation)
|
||||
# sec = 0.0 -> -0.5s -> -1.0 beat
|
||||
assert sec_to_beat(0.0, doc) == pytest.approx(-1.0)
|
||||
assert beat_to_sec(-1.0, doc) == pytest.approx(0.0)
|
||||
|
||||
|
||||
def test_tempo_change_multiple_anchors():
|
||||
# Anchor 0: sec=1.0, beat=0.0, bpm=120 (0.5 sec/beat)
|
||||
# -> 4 beats take 2.0s -> reaches sec=3.0 at beat=4.0
|
||||
# Anchor 1: sec=3.0, beat=4.0, bpm=60 (1.0 sec/beat)
|
||||
# -> 4 beats take 4.0s -> reaches sec=7.0 at beat=8.0
|
||||
# Anchor 2: sec=7.0, beat=8.0, bpm=240 (0.25 sec/beat)
|
||||
doc = TempoDocument(
|
||||
anchors=[
|
||||
TempoAnchor(sec=1.0, beat=0.0, bpm=120.0),
|
||||
TempoAnchor(sec=3.0, beat=4.0, bpm=60.0),
|
||||
TempoAnchor(sec=7.0, beat=8.0, bpm=240.0),
|
||||
]
|
||||
)
|
||||
|
||||
# Exact anchor points
|
||||
assert sec_to_beat(1.0, doc) == pytest.approx(0.0)
|
||||
assert sec_to_beat(3.0, doc) == pytest.approx(4.0)
|
||||
assert sec_to_beat(7.0, doc) == pytest.approx(8.0)
|
||||
|
||||
assert beat_to_sec(0.0, doc) == pytest.approx(1.0)
|
||||
assert beat_to_sec(4.0, doc) == pytest.approx(3.0)
|
||||
assert beat_to_sec(8.0, doc) == pytest.approx(7.0)
|
||||
|
||||
# Inside section 1 (120 bpm)
|
||||
# sec=2.0 -> beat=2.0
|
||||
assert sec_to_beat(2.0, doc) == pytest.approx(2.0)
|
||||
assert beat_to_sec(2.0, doc) == pytest.approx(2.0)
|
||||
|
||||
# Inside section 2 (60 bpm)
|
||||
# sec=5.0 -> 2 seconds after sec=3.0 -> 2 beats -> beat=6.0
|
||||
assert sec_to_beat(5.0, doc) == pytest.approx(6.0)
|
||||
assert beat_to_sec(6.0, doc) == pytest.approx(5.0)
|
||||
|
||||
# Beyond last anchor (240 bpm)
|
||||
# sec=8.0 -> 1 second after sec=7.0 -> 4 beats -> beat=12.0
|
||||
assert sec_to_beat(8.0, doc) == pytest.approx(12.0)
|
||||
assert beat_to_sec(12.0, doc) == pytest.approx(8.0)
|
||||
|
||||
|
||||
def test_roundtrip_identity_across_ranges():
|
||||
# Anchor 0: sec=0.2, beat=0.0, bpm=120.0
|
||||
# 18 beats at 120 bpm = 9.0s -> sec = 9.2, beat = 18.0
|
||||
# Anchor 1: sec=9.2, beat=18.0, bpm=150.0
|
||||
# 42 beats at 150 bpm = 16.8s -> sec = 26.0, beat = 60.0
|
||||
# Anchor 2: sec=26.0, beat=60.0, bpm=95.0
|
||||
doc = TempoDocument(
|
||||
anchors=[
|
||||
TempoAnchor(sec=0.2, beat=0.0, bpm=120.0),
|
||||
TempoAnchor(sec=9.2, beat=18.0, bpm=150.0),
|
||||
TempoAnchor(sec=26.0, beat=60.0, bpm=95.0),
|
||||
]
|
||||
)
|
||||
|
||||
# Test roundtrip for a wide range of seconds
|
||||
test_seconds = [-5.0, 0.0, 0.2, 5.0, 9.199, 9.2, 15.0, 25.999, 26.0, 100.0, 300.0]
|
||||
for s in test_seconds:
|
||||
b = sec_to_beat(s, doc)
|
||||
s_back = beat_to_sec(b, doc)
|
||||
assert s_back == pytest.approx(s, rel=1e-7, abs=1e-7)
|
||||
|
||||
# Test roundtrip for a wide range of beats
|
||||
test_beats = [-10.0, 0.0, 5.0, 18.0, 35.0, 60.0, 150.0]
|
||||
for b in test_beats:
|
||||
s = beat_to_sec(b, doc)
|
||||
b_back = sec_to_beat(s, doc)
|
||||
assert b_back == pytest.approx(b, rel=1e-7, abs=1e-7)
|
||||
Reference in New Issue
Block a user