mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-01 21:45:10 +00:00
228 lines
8.5 KiB
Python
228 lines
8.5 KiB
Python
from typing import Any
|
|
|
|
import jsonpickle
|
|
import pytest
|
|
|
|
from tests.lang_selection.test_kind_only_selection import FakeService, run
|
|
from unshackle.core.console import console
|
|
from unshackle.core.music.renderer import MusicRenderer
|
|
from unshackle.core.titles.music import Album, Music, Song
|
|
|
|
|
|
class DummyService:
|
|
pass
|
|
|
|
|
|
def song(number: int, album: str, album_artist: str | None, **fields: Any) -> Song:
|
|
values: dict[str, Any] = {
|
|
"artist": f"Song Artist {number}",
|
|
"year": 2000 + number,
|
|
"genre": f"Genre {number}",
|
|
"label": f"Label {number}",
|
|
"data": {"release_date": f"200{number}-10-14", "quality": "FLAC 24-bit/96kHz", "duration": 60},
|
|
**fields,
|
|
}
|
|
return Song(
|
|
f"song-{number}", DummyService, f"Name {number}", album=album, album_artist=album_artist, track=number, **values
|
|
)
|
|
|
|
|
|
def mixed(**facts: Any) -> Album:
|
|
"""Songs of two releases that share no fact with each other."""
|
|
return Album([song(1, "Album 1", "Album Artist 1", explicit=True), song(2, "Album 2", "Album Artist 2")], **facts)
|
|
|
|
|
|
def header(music: Music) -> list[tuple[str, str]]:
|
|
grid = MusicRenderer().render_header(music)
|
|
assert grid is not None
|
|
labels, values = (list(column.cells) for column in grid.columns)
|
|
return [(str(label), str(value)) for label, value in zip(labels, values, strict=True)]
|
|
|
|
|
|
OWN_FACTS = {"kind": "playlist", "title": "Own Title", "artist": "Own Artist", "year": 1990, "total_tracks": 9}
|
|
OWN_ROWS = [
|
|
("Title", "Own Title"),
|
|
("Artist", "Own Artist"),
|
|
("Type", "Playlist Explicit"),
|
|
("Year", "1990"),
|
|
("Tracks", "9"),
|
|
("Length", "2m 00s"),
|
|
]
|
|
|
|
|
|
def test_a_collection_of_different_releases_shows_only_its_own_facts() -> None:
|
|
assert header(mixed(**OWN_FACTS)) == OWN_ROWS
|
|
assert str(mixed(**OWN_FACTS)) == "Own Artist - Own Title (1990)"
|
|
|
|
|
|
def test_a_collection_of_different_releases_shows_the_date_and_quality_set_on_it() -> None:
|
|
music = mixed(**OWN_FACTS)
|
|
vars(music).update(release_date="1990-02-03", quality="MP3")
|
|
rows = dict(header(music))
|
|
assert (rows["Released"], rows["Quality"]) == ("February 3, 1990", "MP3")
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("facts", "line"),
|
|
[
|
|
({}, "2 songs"),
|
|
({"total_tracks": 9}, "2 songs"),
|
|
({"title": "Own Title"}, "Own Title"),
|
|
({"artist": "Own Artist"}, "Own Artist"),
|
|
({"year": 1990}, "(1990)"),
|
|
({"title": "Own Title", "year": 1990}, "Own Title (1990)"),
|
|
({"title": "Own Title", "artist": "Own Artist"}, "Own Artist - Own Title"),
|
|
],
|
|
)
|
|
def test_a_fact_that_a_collection_of_different_releases_lacks_has_no_row_and_no_placeholder(
|
|
facts: dict[str, Any], line: str
|
|
) -> None:
|
|
assert str(mixed(**facts)) == line
|
|
shown = [label for label, _ in header(mixed(**facts)) if label not in ("Type", "Tracks", "Length")]
|
|
assert shown == [label for label in ("Title", "Artist", "Year") if label.lower() in facts]
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"other", [("Album 1", "Album Artist 2"), ("Album 2", "Album Artist 1"), ("Album 1", None)], ids=str
|
|
)
|
|
def test_the_album_and_its_artist_together_make_one_release(other: tuple[str, str | None]) -> None:
|
|
music = Album([song(1, "Album 1", "Album Artist 1"), song(2, *other)], title="Own Title")
|
|
assert [label for label, _ in header(music)] == ["Title", "Type", "Tracks", "Length"]
|
|
assert str(music) == "Own Title"
|
|
|
|
|
|
@pytest.mark.parametrize(("album_artist", "artist"), [("Album Artist", "Album Artist"), (None, "Song Artist 1")])
|
|
def test_a_collection_of_one_release_shows_the_facts_of_its_first_song(album_artist: str | None, artist: str) -> None:
|
|
music = Album([song(n, "Album 1", album_artist) for n in (1, 2)], kind="playlist")
|
|
assert header(music) == [
|
|
("Title", "Album 1"),
|
|
("Artist", artist),
|
|
("Type", "Playlist"),
|
|
("Released", "October 14, 2001"),
|
|
("Year", "2001"),
|
|
("Tracks", "2"),
|
|
("Length", "2m 00s"),
|
|
("Quality", "Hi-Res Lossless"),
|
|
("Genre", "Genre 1"),
|
|
("Label", "Label 1"),
|
|
]
|
|
assert str(music) == f"{artist} - Album 1 (2001)"
|
|
assert str(Album([song(1, "Album 1", album_artist, year=None)])) == f"{artist} - Album 1 (?)"
|
|
|
|
|
|
def test_a_collection_of_one_release_shows_its_own_facts_before_those_of_a_song() -> None:
|
|
facts = {**OWN_FACTS, "total_discs": 3, "total_duration": 4000}
|
|
music = Album([song(n, "Album 1", "Album Artist", explicit=True) for n in (1, 2)], **facts)
|
|
vars(music).update(released="1990-02-03", quality="MP3")
|
|
assert dict(header(music)) == {
|
|
"Title": "Own Title",
|
|
"Artist": "Own Artist",
|
|
"Type": "Playlist Explicit",
|
|
"Released": "February 3, 1990",
|
|
"Year": "1990",
|
|
"Tracks": "9",
|
|
"Discs": "3",
|
|
"Length": "1h 06m 40s",
|
|
"Quality": "MP3",
|
|
"Genre": "Genre 1",
|
|
"Label": "Label 1",
|
|
}
|
|
assert str(music) == "Own Artist - Own Title (1990)"
|
|
|
|
|
|
@pytest.mark.parametrize("nested", [False, True])
|
|
def test_the_first_song_of_one_release_gives_the_quality_flags(nested: bool) -> None:
|
|
data = {"lossless": True, "hires": True, "release_date": "2001-10-14"}
|
|
music = Album([song(1, "Album 1", None, data={"metadata": data} if nested else data)])
|
|
assert (dict(header(music))["Quality"], dict(header(music))["Released"]) == ("Hi-Res Lossless", "October 14, 2001")
|
|
|
|
|
|
def test_a_blank_album_of_one_release_keeps_its_row() -> None:
|
|
music = Album([song(1, " ", "Album Artist")])
|
|
assert str(music) == "Album Artist - (2001)"
|
|
assert header(music)[0] == ("Title", "")
|
|
|
|
|
|
@pytest.mark.parametrize("duration", [10**400, float("inf"), "Infinity"], ids=["huge", "infinite", "text"])
|
|
def test_a_song_duration_too_large_to_convert_does_not_count(duration: Any) -> None:
|
|
music = Album([song(1, "Album 1", None), song(2, "Album 1", None, data={"duration": duration})])
|
|
assert dict(header(music))["Length"] == "1m 00s"
|
|
with console.capture() as out:
|
|
console.print(music.tree())
|
|
assert "Name 2" in out.get()
|
|
|
|
|
|
@pytest.mark.parametrize("field", ["title", "artist", "kind"])
|
|
def test_a_fact_is_text_and_not_markup(field: str) -> None:
|
|
with console.capture() as out:
|
|
console.print(MusicRenderer().render_header(mixed(**{field: "[red]x[/y] :cd:"})))
|
|
assert "[red]x[/y] :cd:" in out.get().lower()
|
|
|
|
|
|
def test_a_collection_from_the_title_cache_shows_only_its_own_facts() -> None:
|
|
restored = jsonpickle.loads(jsonpickle.dumps(mixed(**OWN_FACTS)))
|
|
assert header(restored) == OWN_ROWS
|
|
assert str(restored) == "Own Artist - Own Title (1990)"
|
|
|
|
|
|
@pytest.mark.parametrize("field", ["year", "total_tracks", "total_discs", "total_duration"])
|
|
def test_a_collection_number_is_never_a_bool(field: str) -> None:
|
|
with pytest.raises(TypeError, match=f"Expected {field} to be an int, not True"):
|
|
Album([], **{field: True})
|
|
|
|
|
|
def test_a_total_duration_too_large_to_convert_is_shown_as_given() -> None:
|
|
assert MusicRenderer.format_total_duration(10**400) == str(10**400)
|
|
|
|
|
|
NOT_MARKUP = pytest.mark.parametrize("name", ["Live [red]set[/red]", "Live [/x]", "Live :cd:", "Live \\"])
|
|
|
|
|
|
@NOT_MARKUP
|
|
def test_the_title_line_shows_the_collection_as_given(
|
|
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, name: str
|
|
) -> None:
|
|
service = FakeService({})
|
|
service.get_titles = lambda: mixed(artist="Own Artist", title=name) # type: ignore[assignment]
|
|
|
|
with console.capture() as out:
|
|
run(monkeypatch, caplog, {}, service=service, list_titles=True)
|
|
|
|
assert f" Album: Own Artist - {name} " in out.get()
|
|
|
|
|
|
@NOT_MARKUP
|
|
def test_the_track_line_shows_the_song_as_given(
|
|
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, name: str
|
|
) -> None:
|
|
music = mixed()
|
|
music[0].name = name
|
|
service = FakeService({1: {}})
|
|
tracks = service.get_tracks(service.get_titles()[0])
|
|
service.get_titles = lambda: music # type: ignore[assignment]
|
|
service.get_tracks = lambda title: tracks # type: ignore[method-assign]
|
|
|
|
with console.capture() as out:
|
|
run(monkeypatch, caplog, {}, service=service)
|
|
|
|
assert f" Track 01: {name} " in out.get()
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("label", "hires"),
|
|
[
|
|
("FLAC 24-bit/96kHz", True),
|
|
("24-bit / 96 kHz", True),
|
|
("FLAC 16-bit/88.2kHz", True),
|
|
("96k", True),
|
|
("FLAC 16-bit/44.1kHz", False),
|
|
("Lossless", False),
|
|
("256 kb/s AAC", False),
|
|
("MP3 320 kb/s", False),
|
|
("320kbps", False),
|
|
("AAC 96 kbit/s", False),
|
|
],
|
|
)
|
|
def test_is_hires_quality_reads_sample_rate_not_bitrate(label: str, hires: bool) -> None:
|
|
assert MusicRenderer.is_hires_quality(label) is hires
|