id3.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import os
  2. import shutil
  3. import mutagen
  4. import pytest
  5. from symuid._tag_interface import ID3
  6. @pytest.fixture
  7. def empty_id3_iface(tmpdir, tracks_dir_path):
  8. p = tmpdir.join('empty.mp3').strpath
  9. shutil.copyfile(
  10. src=os.path.join(tracks_dir_path, 'id3v2.4-empty.mp3'),
  11. dst=p,
  12. )
  13. return ID3(mutagen.File(p))
  14. @pytest.mark.parametrize('track_name', ['id3v2.4-empty.mp3'])
  15. def test_get_track_path(tracks_dir_path, track_name):
  16. track_path = os.path.join(tracks_dir_path, track_name)
  17. iface = ID3(mutagen.File(track_path))
  18. assert track_path == iface.track_path
  19. @pytest.mark.parametrize(('track_name', 'tag_label', 'expected_text'), [
  20. ('id3v2.4-empty.mp3', 'TPE1', None),
  21. ('id3v2.4-typical.mp3', 'TPE1', 'some artist'),
  22. ('id3v2.4-typical.mp3', 'COMM::eng', 'some comment'),
  23. ('id3v2.4-typical.mp3', 'COMM', None),
  24. ])
  25. def test__get_single_text(tracks_dir_path, track_name, tag_label, expected_text):
  26. iface = ID3(mutagen.File(os.path.join(tracks_dir_path, track_name)))
  27. assert expected_text == iface._get_single_text(tag_label)
  28. @pytest.mark.parametrize(('track_name', 'expected_comment'), [
  29. ('id3v2.4-empty.mp3', None),
  30. ('id3v2.4-typical.mp3', 'some comment'),
  31. ])
  32. def test_get_comment(tracks_dir_path, track_name, expected_comment):
  33. iface = ID3(mutagen.File(os.path.join(tracks_dir_path, track_name)))
  34. assert expected_comment == iface.get_comment()
  35. def test_set_comment(empty_id3_iface):
  36. assert None == empty_id3_iface.get_comment()
  37. empty_id3_iface.set_comment('latin')
  38. assert 'latin' == empty_id3_iface.get_comment()
  39. empty_id3_iface.set_comment('你好')
  40. assert '你好' == empty_id3_iface.get_comment()
  41. empty_id3_iface.save()
  42. tags = mutagen.File(empty_id3_iface.track_path).tags
  43. assert len(tags) == 1
  44. tag = tags.values()[0]
  45. assert isinstance(tag, mutagen.id3.COMM)
  46. assert tag.lang == 'eng'
  47. assert tag.text == ['你好']