Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiascode committed May 29, 2024
1 parent 90f22b3 commit 398d41b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
Binary file modified tinytag/tests/samples/aiff_with_image.aiff
Binary file not shown.
23 changes: 12 additions & 11 deletions tinytag/tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@

# WMA
('samples/test2.wma',
{'extra': {'track': ['0'],
{'extra': {'_track': ['0'],
'mediaprimaryclassid': ['{D1607DBC-E323-4BE2-86A1-48A42A28441E}'],
'encodingtime': ['128861118183900000'], 'wmfsdkversion': ['11.0.5721.5145'],
'wmfsdkneeded': ['0.0.0.0000'], 'isvbr': ['1'], 'peakvalue': ['30369'],
Expand Down Expand Up @@ -763,6 +763,12 @@ def test_image_loading_extra(path: str) -> None:
"PROFILE\\x00\\x01\\x01\\x00\\x00\\x02\\xa0lcm..', 'mime_type': 'image/jpeg', "
"'description': None}"
)
assert str(tag.images) == (
"{'extra': {'bright_colored_fish': [{'name': 'extra.bright_colored_fish', "
"'data': b'\\xff\\xd8\\xff\\xe0\\x00\\x10JFIF\\x00\\x01\\x01\\x01\\x00H\\x00H"
"\\x00\\x00\\xff\\xe2\\x02\\xb0ICC_PROFILE\\x00\\x01\\x01\\x00\\x00\\x02\\xa0"
"lcm..', 'mime_type': 'image/jpeg', 'description': None}]}}"
)


def test_mp3_utf_8_invalid_string() -> None:
Expand Down Expand Up @@ -825,15 +831,10 @@ def test_to_str() -> None:


def test_to_str_flatten() -> None:
tag = TinyTag.get(os.path.join(testfolder, 'samples/id3v22-test.mp3'))
tag = TinyTag.get(os.path.join(testfolder, 'samples/id3_multiple_artists.mp3'))
assert (
"'filesize': 5120, 'duration': 0.13836297152858082, 'channels': 2, 'bitrate': 160.0, "
"'samplerate': 44100, 'artist': ['Anais Mitchell'], "
"'album': ['Hymns for the Exiled'], "
"'title': ['cosmic american'], 'track': 3, 'track_total': 11, "
"'year': ['2004'], 'comment': ['Waterbug Records, www.anaismitchell.com'], "
"'encoded_by': ['iTunes v4.6'], 'itunnorm': [' 0000044E 00000061 00009B67 "
"000044C3 00022478 00022182 00007FCC 00007E5C 0002245E 0002214E'], 'itunes_cddb_1': "
"['9D09130B+174405+11+150+14097+27391+43983+65786+84877+99399+113226+132452+146426+"
"163829'], 'itunes_cddb_tracknumber': ['3'], 'images': {}"
"'filesize': 2007, 'duration': 0.1306122448979592, 'channels': 1, "
"'bitrate': 57.39124999999999, 'samplerate': 44100, 'artist': "
"['artist1', 'artist2', 'artist3', 'artist4', 'artist5', 'artist6', 'artist7'], "
"'genre': ['something 1'], 'images': {}"
) in str(tag.as_dict(flatten=True))
5 changes: 5 additions & 0 deletions tinytag/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ def test_meta_data_output_format_tabularcsv() -> None:
assert set(header.split(',')).issubset(tinytag_attributes)


def test_meta_data_output_format_invalid() -> None:
output = run_cli('-f invalid ' + mp3_with_image)
assert not output


def test_fail_on_unsupported_file() -> None:
with pytest.raises(CalledProcessError):
run_cli(bogus_file)
Expand Down
26 changes: 13 additions & 13 deletions tinytag/tinytag.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ def as_dict(self, flatten: bool = True) -> dict[
if flatten and key == 'extra':
for extra_key, extra_values in value.items():
if extra_key in fields:
fields[extra_key] += extra_values
if isinstance(fields[extra_key], list):
fields[extra_key] += extra_values
else:
fields[extra_key] = extra_values
continue
Expand Down Expand Up @@ -259,9 +260,12 @@ def _load(self, tags: bool, duration: bool, image: bool = False) -> None:
self._filehandler.seek(0)
self._determine_duration(self._filehandler)

def _set_field(self, fieldname: str, value: str | int | float) -> None:
def _set_field(self, fieldname: str, value: str | int | float,
check_conflict: bool = True) -> None:
if fieldname.startswith(self._EXTRA_PREFIX):
fieldname = fieldname[len(self._EXTRA_PREFIX):]
if check_conflict and fieldname in self.__dict__:
fieldname = '_' + fieldname
extra_values = self.extra.get(fieldname, [])
if not isinstance(value, str) or value in extra_values:
return
Expand All @@ -275,14 +279,11 @@ def _set_field(self, fieldname: str, value: str | int | float) -> None:
if isinstance(new_value, str):
# First value goes in tag, others in tag.extra
values = new_value.split('\x00')
new_value = values[0]
start_pos = 0 if old_value else 1
if len(values) > 1:
for i_value in values[start_pos:]:
self._set_field(self._EXTRA_PREFIX + fieldname, i_value)
elif old_value and new_value != old_value:
self._set_field(self._EXTRA_PREFIX + fieldname, new_value)
return
for index, i_value in enumerate(values):
if index or old_value and i_value != old_value:
self._set_field(self._EXTRA_PREFIX + fieldname, i_value, check_conflict=False)
continue
new_value = i_value
if old_value:
return
elif not new_value and old_value:
Expand All @@ -307,7 +308,8 @@ def _update(self, other: TinyTag) -> None:
for extra_key, extra_values in value.items():
for extra_value in extra_values:
if isinstance(extra_value, str):
self._set_field(self._EXTRA_PREFIX + extra_key, extra_value)
self._set_field(
self._EXTRA_PREFIX + extra_key, extra_value, check_conflict=False)
continue
if value is not None and not isinstance(value, list):
self._set_field(key, value)
Expand Down Expand Up @@ -371,8 +373,6 @@ def as_dict(self, flatten: bool = True) -> dict[str, list[TagImage]]:
"""Return a dictionary representation of the tag images."""
images: dict[str, list[TagImage]] = {}
for key, value in self.__dict__.items():
if key.startswith('_'):
continue
if flatten and key == 'extra':
for extra_key, extra_values in value.items():
if extra_key in images:
Expand Down

0 comments on commit 398d41b

Please sign in to comment.