Files
unshackle/tests/core/test_music_extract.py
T

174 lines
4.9 KiB
Python

"""Unit tests for the shared music helpers in unshackle.core.music.extract."""
from __future__ import annotations
from typing import Any
import pytest
from unshackle.core.music.extract import (
build_music_from_songs,
classify_release_kind,
duration_seconds,
first_number,
first_text,
)
from unshackle.core.titles.music import Music, Song
class DummyService:
"""Stand-in service class; Song only requires a type, never an instance."""
def make_song(
*,
track: int = 1,
disc: int = 1,
year: int = 2020,
album: str = "Album",
artist: str = "Artist",
name: str = "Song",
album_artist: str | None = None,
total_tracks: int | None = None,
total_discs: int | None = None,
artwork_url: str | None = None,
data: Any | None = None,
) -> Song:
return Song(
id_=f"{album}-{disc}-{track}",
service=DummyService,
name=name,
artist=artist,
album=album,
track=track,
disc=disc,
year=year,
album_artist=album_artist,
total_tracks=total_tracks,
total_discs=total_discs,
artwork_url=artwork_url,
data=data,
)
@pytest.mark.parametrize(
("values", "default", "expected"),
[
((None, "", " hello "), "", "hello"),
(("", None), "fallback", "fallback"),
(({"name": "Track"},), "", "Track"),
(({"title": "T"},), "", "T"),
(({"description": "D"},), "", "D"),
((["a", "", "b"],), "", "a, b"),
((123,), "", "123"),
((), "def", "def"),
],
)
def test_first_text(values: tuple[Any, ...], default: str, expected: str) -> None:
assert first_text(*values, default=default) == expected
@pytest.mark.parametrize(
("values", "expected"),
[
((None, "", "12"), 12.0),
(("3.5",), 3.5),
(("abc", 7), 7.0),
((None, ""), None),
((), None),
((10**400, "Infinity", float("nan"), 7), 7.0),
],
)
def test_first_number(values: tuple[Any, ...], expected: float | None) -> None:
assert first_number(*values) == expected
@pytest.mark.parametrize(
("value", "expected"),
[
(250, 250.0),
(250000, 250.0), # treated as milliseconds
("180", 180.0),
(None, None),
("nope", None),
],
)
def test_duration_seconds(value: Any, expected: float | None) -> None:
assert duration_seconds(value) == expected
@pytest.mark.parametrize(
("raw_kind", "count", "expected"),
[
("single", 1, "single"),
("single", 3, "ep"), # multi-track "single" -> EP
("EP", None, "ep"),
("Extended Play", None, "ep"),
("ep-single", 1, "single"),
("ep-single", 4, "ep"),
("album", None, "album"),
("compilation", None, "compilation"),
("Live Recording", None, "live"),
("download", None, "download"),
("playlist", None, "playlist"),
("other", None, "other"),
("totally-unknown", None, "album"),
("", None, "album"),
],
)
def test_classify_release_kind(raw_kind: str, count: float | None, expected: str) -> None:
assert classify_release_kind(raw_kind, count) == expected
def test_build_music_from_songs() -> None:
songs = [
make_song(
track=1,
disc=1,
year=2019,
album="Greatest",
artist="Band",
album_artist="The Band",
total_tracks=2,
total_discs=1,
artwork_url="http://art/1.jpg",
data={"duration": 200},
),
make_song(
track=2,
disc=1,
year=2019,
album="Greatest",
artist="Band",
album_artist="The Band",
total_tracks=2,
total_discs=1,
data={"duration": 100},
),
]
music = build_music_from_songs(songs, kind="album")
assert isinstance(music, Music)
assert music.kind == "album"
assert music.title == "Greatest"
assert music.artist == "The Band" # album_artist preferred over artist
assert music.year == 2019
assert music.total_tracks == 2
assert music.total_discs == 1
assert music.total_duration == 300
assert music.artwork_url == "http://art/1.jpg"
def test_build_music_from_songs_of_different_releases_takes_no_fact_from_a_song() -> None:
songs = [
make_song(album="A", album_artist="Band A", total_tracks=30, artwork_url="http://art/a.jpg"),
make_song(track=12, disc=4, album="B", album_artist="Band B", total_discs=4, data={"duration": 60}),
]
music = build_music_from_songs(songs, kind="playlist")
assert (music.title, music.artist, music.year, music.artwork_url) == (None, None, None, None)
assert (music.total_tracks, music.total_discs, music.total_duration) == (2, None, 60)
named = build_music_from_songs(songs, kind="playlist", title="My Mix", artist="Various")
assert str(named) == "Various - My Mix"