Files
lintunes/tests/test_round100.py
T

151 lines
5.0 KiB
Python

"""Round 100: the rating column is never narrower than five stars."""
from PyQt6.QtGui import QFontMetrics
from lintunes.gui.track_table import TrackTableView, rating_min_width
from lintunes.models.playlist import PlaylistSettings
def _view(widths):
view = TrackTableView(playlist_mode=False)
view.apply_settings(PlaylistSettings(
visible_columns=["name", "rating", "date_added"], sort_column="name",
column_widths=widths))
return view
def test_saved_narrow_width_is_widened(qapp):
view = _view({"rating": 20})
least = rating_min_width(QFontMetrics(view.font()))
assert view.columnWidth(view.model_.fields.index("rating")) == least
def test_dragging_narrower_snaps_back_and_saves_the_minimum(qapp):
view = _view({})
col = view.model_.fields.index("rating")
saved = []
view.column_width_changed.connect(lambda f, w: saved.append((f, w)))
view.horizontalHeader().resizeSection(col, 10)
least = rating_min_width(QFontMetrics(view.font()))
assert view.columnWidth(col) == least
assert saved == [("rating", least)]
def test_wider_widths_are_left_alone(qapp):
view = _view({"rating": 200})
assert view.columnWidth(view.model_.fields.index("rating")) == 200
# ---- DJ stems: a cache that survives a tag edit, held to a size ----
import os
import shutil
import subprocess
import time
import pytest
from lintunes.dj import stems
@pytest.fixture
def cache(tmp_path, monkeypatch):
monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path / "cache"))
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path / "config"))
return tmp_path
def _mp3(path):
subprocess.run(["ffmpeg", "-v", "error", "-y", "-f", "lavfi", "-i",
"sine=f=440:d=2", str(path)], check=True)
return str(path)
def _make(folder, size=1):
folder.mkdir(parents=True, exist_ok=True)
for name in stems.STEMS:
(folder / f"{name}.flac").write_bytes(b"f" * size)
needs_ffmpeg = pytest.mark.skipif(shutil.which("ffmpeg") is None,
reason="needs the ffmpeg CLI")
@needs_ffmpeg
def test_writing_the_bpm_tag_keeps_the_split(cache):
from mutagen.id3 import ID3, TBPM
song = _mp3(cache / "a.mp3")
_make(stems.cache_root() / stems.cache_key(song) / "htdemucs")
tags = ID3()
tags.add(TBPM(encoding=3, text=["128"]))
tags.save(song) # what loading a song with no BPM does
os.utime(song, (time.time() + 60, time.time() + 60))
assert stems.cached(song) is not None
@needs_ffmpeg
def test_a_split_under_the_old_path_key_is_moved_to_the_audio_key(cache):
song = _mp3(cache / "a.mp3")
_make(stems.cache_root() / stems._path_key(song) / "htdemucs")
assert stems.cached(song) is not None
assert (stems.cache_root() / stems.cache_key(song) / "htdemucs").is_dir()
assert not (stems.cache_root() / stems._path_key(song)).exists()
@needs_ffmpeg
def test_the_hash_runs_once_per_version_of_a_file(cache, monkeypatch):
song = _mp3(cache / "a.mp3")
calls = []
real = stems.audio_hash
monkeypatch.setattr(stems, "audio_hash",
lambda p: calls.append(p) or real(p))
first = stems.cache_key(song)
assert stems.cache_key(song) == first
assert len(calls) == 1
def test_unreadable_audio_falls_back_to_the_path_key(cache):
song = cache / "a.mp3"
song.write_bytes(b"x")
assert stems.cache_key(str(song)) == stems._path_key(str(song))
def test_prune_drops_the_songs_used_longest_ago(cache):
root = stems.cache_root()
now = time.time()
for i, name in enumerate(["old", "mid", "new"]):
_make(root / name / "htdemucs", size=400 * 1024) # 800 KiB a song
os.utime(root / name, (now - 100 + i, now - 100 + i))
removed = stems.prune(limit_gb=2 * 800 / 1024 ** 2) # room for two
assert removed == 1
assert sorted(p.name for p in root.iterdir()) == ["mid", "new"]
stems.prune(limit_gb=0, keep="mid")
assert [p.name for p in root.iterdir()] == ["mid"]
def test_the_limit_comes_from_config_json(cache):
from lintunes.config import save_config
assert stems.cache_limit_gb() == 1.0
save_config({stems.CACHE_LIMIT_KEY: 2.5})
assert stems.cache_limit_gb() == 2.5
assert stems.cache_limit_gb({stems.CACHE_LIMIT_KEY: "junk"}) == 1.0
def test_preferences_saves_the_limit_to_config_json(qapp, cache, monkeypatch):
from lintunes.config import load_config
from lintunes.gui.preferences_dialog import PreferencesDialog
from lintunes.preferences import Preferences
from tests.test_round40 import _FakeLastFm
pruned = []
monkeypatch.setattr(stems, "prune", lambda limit: pruned.append(limit))
dialog = PreferencesDialog(Preferences(cache), _FakeLastFm())
assert dialog._dj_cache_edit.text() == "1"
dialog._dj_cache_edit.setText("3.5")
dialog._save_stem_cache_limit()
assert load_config()[stems.CACHE_LIMIT_KEY] == 3.5
for _ in range(50):
if pruned:
break
time.sleep(0.01)
assert pruned == [3.5]
dialog.deleteLater()