| """Tests for MIDI analyzer service.""" |
|
|
| import pytest |
| from backend.services.midi_analyzer import ( |
| extract_bpm_from_midi, |
| extract_key_from_midi, |
| validate_midi_audio_sync, |
| ) |
|
|
|
|
| def test_midi_bpm_extraction(sample_midi_120bpm): |
| """Should extract exactly 120 BPM from MIDI.""" |
| result = extract_bpm_from_midi(sample_midi_120bpm) |
| assert result["bpm"] == 120.0 |
| assert result["confidence"] == 1.0 |
|
|
|
|
| def test_midi_key_extraction(sample_midi_120bpm): |
| """C major scale MIDI should detect C major.""" |
| result = extract_key_from_midi(sample_midi_120bpm) |
| assert result["key"] == "C" |
| assert result["mode"] == "major" |
|
|
|
|
| def test_midi_sync_validation(sample_midi_120bpm): |
| """Duration validation should work.""" |
| |
| is_synced = validate_midi_audio_sync(sample_midi_120bpm, audio_duration=8.0) |
| assert isinstance(is_synced, bool) |
|
|
|
|
| def test_midi_bpm_returns_dict(sample_midi_120bpm): |
| """BPM extraction should return properly structured dict.""" |
| result = extract_bpm_from_midi(sample_midi_120bpm) |
|
|
| assert isinstance(result, dict) |
| assert "bpm" in result |
| assert "confidence" in result |
| assert "tempo_changes" in result |
|
|
|
|
| def test_midi_key_returns_dict(sample_midi_120bpm): |
| """Key extraction should return properly structured dict.""" |
| result = extract_key_from_midi(sample_midi_120bpm) |
|
|
| assert isinstance(result, dict) |
| assert "key" in result |
| assert "mode" in result |
| assert "confidence" in result |
|
|
|
|
| def test_midi_bpm_default_tempo(): |
| """MIDI without tempo should default to 120 BPM.""" |
| import mido |
|
|
| mid = mido.MidiFile() |
| track = mido.MidiTrack() |
| mid.tracks.append(track) |
| |
| track.append(mido.Message('note_on', note=60, velocity=100, time=0)) |
| track.append(mido.Message('note_off', note=60, velocity=0, time=480)) |
|
|
| result = extract_bpm_from_midi(mid) |
| assert result["bpm"] == 120.0 |
|
|