fix: make id3 genre mapping robust and test the id3 plugin

This commit is contained in:
randogoth 2026-10-24 21:41:04 +03:00
parent 3277e5b323
commit 675e069a51
5 changed files with 81 additions and 34 deletions

59
tests/test_id3.py Normal file
View file

@ -0,0 +1,59 @@
import wave
import pytest
taglib = pytest.importorskip('taglib')
import orvanitag.orvani as orvani
from orvanitag.orvani import OrvaniTag
@pytest.fixture
def cwd(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
return tmp_path
def make_wav(path, **tags):
with wave.open(str(path), 'wb') as audio:
audio.setnchannels(1)
audio.setsampwidth(2)
audio.setframerate(8000)
audio.writeframes(b'\0\0' * 800)
if tags:
with taglib.File(path, save_on_exit=True) as song:
for tag, value in tags.items():
song.tags[tag.upper()] = [value]
def scanned(cwd, config=None):
if config:
(cwd / orvani.CONFIG_FILE).write_text(config)
OrvaniTag().scan('.', False)
return (cwd / orvani.CONTEXT_FILE).read_text()
def test_write_and_read_comment(cwd):
make_wav(cwd / 'a.wav')
o = OrvaniTag()
o.add('a.wav', '+album', '@drums', '#lo-fi', 'first', 'take')
assert o.write_metadata('a.wav', 'id3')[0] == 0
with taglib.File(cwd / 'a.wav') as song:
assert song.tags['COMMENT'] == ['+album @drums #lo-fi first take']
(cwd / orvani.CONTEXT_FILE).unlink()
assert scanned(cwd) == 'a.wav --- +album @drums #lo-fi first take\n'
def test_map_genre_with_spaces_to_tags(cwd):
make_wav(cwd / 'a.wav', comment='#lofi', genre='Lo Fi')
assert scanned(cwd, '[id3.map]\ntags = "genre"\n') == 'a.wav --- #lofi #Lo-Fi\n'
def test_map_works_without_comment(cwd):
make_wav(cwd / 'a.wav', genre='Jazz', title='Night Walk')
assert scanned(cwd, '[id3.map]\ntags = "genre"\nnote = "title"\n') == 'a.wav --- #Jazz Night Walk\n'
def test_map_to_missing_tag_and_unknown_key_are_ignored(cwd):
make_wav(cwd / 'a.wav', comment='#lofi')
assert scanned(cwd, '[id3.map]\ncontexts = "albumartist"\nbogus = "genre"\n') == 'a.wav --- #lofi\n'