test_id3.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import os
  2. import re
  3. import mutagen
  4. import pytest
  5. from symuid._tag_interface import ID3
  6. # pylint: disable=protected-access
  7. @pytest.mark.parametrize("track_name", ["id3v2.4-empty.mp3"])
  8. def test_get_track_path(tracks_dir_path, track_name):
  9. track_path = os.path.join(tracks_dir_path, track_name)
  10. iface = ID3(mutagen.File(track_path))
  11. assert track_path == iface.track_path
  12. @pytest.mark.parametrize(
  13. ("track_name", "tag_label", "expected_text"),
  14. [
  15. ("id3v2.4-typical.mp3", "TPE1", "some artist"),
  16. ("id3v2.4-typical.mp3", "COMM::eng", "some comment"),
  17. ],
  18. )
  19. def test__get_str(tracks_dir_path, track_name, tag_label, expected_text):
  20. iface = ID3(mutagen.File(os.path.join(tracks_dir_path, track_name)))
  21. assert expected_text == iface._get_str(tag_label)
  22. @pytest.mark.parametrize(
  23. ("track_name", "tag_label"),
  24. [
  25. ("id3v2.4-empty.mp3", "TPE1"),
  26. ("id3v2.4-typical.mp3", "COMM"),
  27. ],
  28. )
  29. def test__get_str_missing(tracks_dir_path, track_name, tag_label):
  30. iface = ID3(mutagen.File(os.path.join(tracks_dir_path, track_name)))
  31. with pytest.raises(KeyError, match=re.escape(tag_label)):
  32. iface._get_str(tag_label)
  33. def test__get_free_str(empty_id3_path):
  34. mutagen_file = mutagen.File(empty_id3_path)
  35. mutagen_file.tags.add(mutagen.id3.TXXX(desc="foo", text="bar"))
  36. mutagen_file.save()
  37. id3_iface = ID3(mutagen.File(empty_id3_path))
  38. assert id3_iface._get_free_str("foo") == "bar"
  39. def test__get_free_str_missing(empty_id3_path):
  40. iface = ID3(mutagen.File(empty_id3_path))
  41. with pytest.raises(KeyError, match=r"TXXX:foo"):
  42. iface._get_free_str("foo")
  43. @pytest.mark.parametrize(
  44. ("track_name", "expected_comment"),
  45. [
  46. ("id3v2.4-empty.mp3", None),
  47. ("id3v2.4-typical.mp3", "some comment"),
  48. ],
  49. )
  50. def test_get_comment(tracks_dir_path, track_name, expected_comment):
  51. iface = ID3(mutagen.File(os.path.join(tracks_dir_path, track_name)))
  52. assert expected_comment == iface.get_comment()
  53. def test_set_comment(empty_id3_path):
  54. empty_id3_iface = ID3(mutagen.File(empty_id3_path))
  55. assert empty_id3_iface.get_comment() is None
  56. empty_id3_iface.set_comment("latin")
  57. assert empty_id3_iface.get_comment() == "latin"
  58. empty_id3_iface.set_comment("你好")
  59. assert empty_id3_iface.get_comment() == "你好"
  60. empty_id3_iface.save()
  61. tags = mutagen.File(empty_id3_iface.track_path).tags
  62. assert len(tags) == 1
  63. tag = tags.values()[0]
  64. assert isinstance(tag, mutagen.id3.COMM)
  65. assert tag.lang == "eng"
  66. assert tag.text == ["你好"]
  67. @pytest.mark.parametrize(
  68. ("tag_label", "tag_value", "expected_value"),
  69. [
  70. ("foo", "21/42", (21, 42)),
  71. ("bar", "-1/4", (-1, 4)),
  72. ("ham:egg", "0/-3", (0, -3)),
  73. ],
  74. )
  75. def test_get_free_int_ratio(empty_id3_path, tag_label, tag_value, expected_value):
  76. mutagen_file = mutagen.File(empty_id3_path)
  77. mutagen_file.tags.add(
  78. mutagen.id3.TXXX(
  79. encoding=mutagen.id3.Encoding.LATIN1,
  80. desc=tag_label,
  81. text=[tag_value],
  82. )
  83. )
  84. mutagen_file.save()
  85. id3_iface = ID3(mutagen.File(empty_id3_path))
  86. assert id3_iface.get_free_int_ratio(tag_label) == expected_value
  87. @pytest.mark.parametrize(
  88. ("tag_label", "nominator", "denominator", "expected_tag_value"),
  89. [("foo", 21, 42, "21/42"), ("bar", -1, 4, "-1/4"), ("ham:egg", 0, -3, "0/-3")],
  90. )
  91. def test_set_free_int_ratio(
  92. empty_id3_path, tag_label, nominator, denominator, expected_tag_value
  93. ):
  94. id3_iface = ID3(mutagen.File(empty_id3_path))
  95. assert repr(id3_iface.set_free_int_ratio(tag_label, nominator, denominator))
  96. id3_iface.save()
  97. mutagen_file = mutagen.File(empty_id3_path)
  98. assert len(mutagen_file.tags) == 1
  99. assert mutagen_file.tags["TXXX:" + tag_label].text == [expected_tag_value]