123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- import os
- import re
- import mutagen
- import pytest
- from symuid._tag_interface import ID3
- # pylint: disable=protected-access
- @pytest.mark.parametrize("track_name", ["id3v2.4-empty.mp3"])
- def test_get_track_path(tracks_dir_path, track_name):
- track_path = os.path.join(tracks_dir_path, track_name)
- iface = ID3(mutagen.File(track_path))
- assert track_path == iface.track_path
- @pytest.mark.parametrize(
- ("track_name", "tag_label", "expected_text"),
- [
- ("id3v2.4-typical.mp3", "TPE1", "some artist"),
- ("id3v2.4-typical.mp3", "COMM::eng", "some comment"),
- ],
- )
- def test__get_str(tracks_dir_path, track_name, tag_label, expected_text):
- iface = ID3(mutagen.File(os.path.join(tracks_dir_path, track_name)))
- assert expected_text == iface._get_str(tag_label)
- @pytest.mark.parametrize(
- ("track_name", "tag_label"),
- [
- ("id3v2.4-empty.mp3", "TPE1"),
- ("id3v2.4-typical.mp3", "COMM"),
- ],
- )
- def test__get_str_missing(tracks_dir_path, track_name, tag_label):
- iface = ID3(mutagen.File(os.path.join(tracks_dir_path, track_name)))
- with pytest.raises(KeyError, match=re.escape(tag_label)):
- iface._get_str(tag_label)
- def test__get_free_str(empty_id3_path):
- mutagen_file = mutagen.File(empty_id3_path)
- mutagen_file.tags.add(mutagen.id3.TXXX(desc="foo", text="bar"))
- mutagen_file.save()
- id3_iface = ID3(mutagen.File(empty_id3_path))
- assert id3_iface._get_free_str("foo") == "bar"
- def test__get_free_str_missing(empty_id3_path):
- iface = ID3(mutagen.File(empty_id3_path))
- with pytest.raises(KeyError, match=r"TXXX:foo"):
- iface._get_free_str("foo")
- @pytest.mark.parametrize(
- ("track_name", "expected_comment"),
- [
- ("id3v2.4-empty.mp3", None),
- ("id3v2.4-typical.mp3", "some comment"),
- ],
- )
- def test_get_comment(tracks_dir_path, track_name, expected_comment):
- iface = ID3(mutagen.File(os.path.join(tracks_dir_path, track_name)))
- assert expected_comment == iface.get_comment()
- def test_set_comment(empty_id3_path):
- empty_id3_iface = ID3(mutagen.File(empty_id3_path))
- assert empty_id3_iface.get_comment() is None
- empty_id3_iface.set_comment("latin")
- assert empty_id3_iface.get_comment() == "latin"
- empty_id3_iface.set_comment("你好")
- assert empty_id3_iface.get_comment() == "你好"
- empty_id3_iface.save()
- tags = mutagen.File(empty_id3_iface.track_path).tags
- assert len(tags) == 1
- tag = tags.values()[0]
- assert isinstance(tag, mutagen.id3.COMM)
- assert tag.lang == "eng"
- assert tag.text == ["你好"]
- @pytest.mark.parametrize(
- ("tag_label", "tag_value", "expected_value"),
- [
- ("foo", "21/42", (21, 42)),
- ("bar", "-1/4", (-1, 4)),
- ("ham:egg", "0/-3", (0, -3)),
- ],
- )
- def test_get_free_int_ratio(empty_id3_path, tag_label, tag_value, expected_value):
- mutagen_file = mutagen.File(empty_id3_path)
- mutagen_file.tags.add(
- mutagen.id3.TXXX(
- encoding=mutagen.id3.Encoding.LATIN1,
- desc=tag_label,
- text=[tag_value],
- )
- )
- mutagen_file.save()
- id3_iface = ID3(mutagen.File(empty_id3_path))
- assert id3_iface.get_free_int_ratio(tag_label) == expected_value
- @pytest.mark.parametrize(
- ("tag_label", "nominator", "denominator", "expected_tag_value"),
- [("foo", 21, 42, "21/42"), ("bar", -1, 4, "-1/4"), ("ham:egg", 0, -3, "0/-3")],
- )
- def test_set_free_int_ratio(
- empty_id3_path, tag_label, nominator, denominator, expected_tag_value
- ):
- id3_iface = ID3(mutagen.File(empty_id3_path))
- assert repr(id3_iface.set_free_int_ratio(tag_label, nominator, denominator))
- id3_iface.save()
- mutagen_file = mutagen.File(empty_id3_path)
- assert len(mutagen_file.tags) == 1
- assert mutagen_file.tags["TXXX:" + tag_label].text == [expected_tag_value]
|