beets/test/test_report.py
2026-01-05 11:52:59 +01:00

143 lines
3.8 KiB
Python

import pytest
from beets.library import Item
from beetsplug.report import ReportPlugin
# --- Fixtures ---
@pytest.fixture
def library(tmp_path):
"""Create a temporary empty Beets library."""
from beets.library import Library
lib_path = tmp_path / "beets.db"
lib = Library(str(lib_path))
return lib
def add_item(
lib,
title="Test",
artist="Artist",
album="Album",
genre="Genre",
year=2000,
length=180,
bitrate=320,
):
"""Add a single Item to the test library."""
item = Item(
path=f"/tmp/{title}.mp3",
title=title,
artist=artist,
album=album,
genre=genre,
year=year,
length=length,
bitrate=bitrate,
)
lib.add(item)
# --- Tests ---
def test_empty_library(capsys, library):
"""Test empty library: should output message without crashing."""
plugin = ReportPlugin()
plugin._run_report(library, None, [])
captured = capsys.readouterr()
assert "Your Beets library is empty." in captured.out
def test_single_item(capsys, library):
"""Test library with a single track."""
add_item(
library,
title="Single Track",
artist="Solo Artist",
genre="Indie",
year=2019,
)
plugin = ReportPlugin()
plugin._run_report(library, None, [])
captured = capsys.readouterr()
# --- Basic statistics ---
assert "Tracks:" in captured.out
assert "Albums:" in captured.out
assert "Artists:" in captured.out
assert "Genres:" in captured.out
# --- Wrapped-style insights ---
assert "Top artist:" in captured.out
assert "Solo Artist" in captured.out
assert "Top genre:" in captured.out
assert "Indie" in captured.out
assert "Top decade:" in captured.out
assert "10s" in captured.out
assert "Top year:" in captured.out
assert "2019" in captured.out
def test_multiple_items(capsys, library):
"""Test library with multiple tracks from different decades and genres."""
add_item(library, "Track1", "Artist A", "Album X", "Rock", 1995)
add_item(library, "Track2", "Artist A", "Album X", "Rock", 1995)
add_item(library, "Track3", "Artist B", "Album Y", "Pop", 2002)
add_item(library, "Track4", "Artist C", "Album Z", "Electronic", 2018)
plugin = ReportPlugin()
plugin._run_report(library, None, [])
captured = capsys.readouterr()
# --- Basic stats ---
assert "Tracks:" in captured.out and "4" in captured.out
assert "Albums:" in captured.out and "3" in captured.out
assert "Artists:" in captured.out and "3" in captured.out
assert "Genres:" in captured.out and "3" in captured.out
# --- Wrapped-style insights ---
assert "Top artist:" in captured.out and "Artist A" in captured.out
assert "Top genre:" in captured.out and "Rock" in captured.out
assert "Top decade:" in captured.out and "90s" in captured.out
assert "Top year:" in captured.out and "1995" in captured.out
# --- Decade distribution ---
assert "90s" in captured.out
assert "00s" in captured.out
assert "10s" in captured.out
def test_missing_metadata(capsys, library):
"""Test library with missing tags, length, and bitrate."""
add_item(
library,
"Track1",
"Artist",
"Album",
None,
2000,
length=200,
bitrate=256,
)
add_item(
library,
"Track2",
"Artist",
"Album",
"Rock",
None,
length=180,
bitrate=None,
)
plugin = ReportPlugin()
plugin._run_report(library, None, [])
captured = capsys.readouterr()
# --- Check missing metadata counts ---
assert "Missing genre" in captured.out
assert "1" in captured.out # At least one missing genre
assert "Missing year" in captured.out
assert "1" in captured.out # At least one missing year