Files
lintunes/tests/test_round132.py
T

136 lines
4.9 KiB
Python

"""Round 132: songs a playlist already has are skipped quietly.
Adding them no longer stops everything with a modal prompt: only the new
songs go in, and the status bar says so for ten seconds with an Add Anyway
button (gui/duplicate_notice.py). ``ask_on_duplicates`` brings the prompt
back.
"""
from unittest.mock import patch
import pytest
from lintunes.gui import duplicate_notice, playlist_ops
from lintunes.gui.playlist_ops import add_tracks_with_dup_check, full_add
from lintunes.library_manager import LibraryManager
from lintunes.models.library import Library
from lintunes.models.playlist import Playlist
from lintunes.models.track import Track
from lintunes.preferences import Preferences
@pytest.fixture
def window(qapp, tmp_path):
from lintunes.gui.main_window import MainWindow
library = Library(tracks={tid: Track(track_id=tid, name=f"t{tid}")
for tid in range(1, 6)})
library.playlists["PID"] = Playlist(name="Mix", persistent_id="PID",
track_ids=[1, 2])
manager = LibraryManager(library, tmp_path)
win = MainWindow(manager, Preferences(tmp_path))
win.resize(1000, 700)
win.show()
qapp.processEvents()
yield win
win.duplicate_notice.dismiss()
win.close()
def _playlist(win):
return win._manager.library.playlists["PID"]
def _no_dialog():
return patch.object(playlist_ops, "_resolve_duplicates",
side_effect=AssertionError("asked"))
def test_full_add_matches_the_manager():
assert full_add([1, 2], [3, 1], None) == [1, 2, 3, 1]
assert full_add([1, 2], [3, 1], 1) == [1, 3, 1, 2]
assert full_add([1, 2], [3], 9) == [1, 2, 3]
def test_messages():
assert duplicate_notice.message("Mix", 1, 2) == (
"1 song was already in “Mix” — only the new ones were added")
assert duplicate_notice.message("Mix", 3, 0) == (
"3 songs were already in “Mix” — nothing was added")
def test_only_new_songs_are_added_and_the_status_bar_says_so(window):
with _no_dialog():
added = add_tracks_with_dup_check(
window._manager, window._playlist_view, "PID", [3, 1, 4])
assert added == [3, 4]
assert _playlist(window).track_ids == [1, 2, 3, 4]
notice = window.duplicate_notice
assert notice.isVisible()
assert "only the new ones" in notice.label.text()
def test_add_anyway_makes_what_adding_everything_would_have(window):
with _no_dialog():
add_tracks_with_dup_check(
window._manager, window._playlist_view, "PID", [3, 1, 4],
position=1)
assert _playlist(window).track_ids == [1, 3, 4, 2]
window.duplicate_notice.button.click()
assert _playlist(window).track_ids == [1, 3, 1, 4, 2]
assert not window.duplicate_notice.isVisible()
# One undo takes back the duplicates, the next the rest.
window._manager.undo_stack.undo()
assert _playlist(window).track_ids == [1, 3, 4, 2]
window._manager.undo_stack.undo()
assert _playlist(window).track_ids == [1, 2]
def test_add_anyway_after_another_edit_only_appends_the_duplicates(window):
with _no_dialog():
add_tracks_with_dup_check(
window._manager, window._playlist_view, "PID", [1, 3])
window._manager.add_tracks_to_playlist("PID", [5])
window.duplicate_notice.button.click()
assert _playlist(window).track_ids == [1, 2, 3, 5, 1]
def test_all_duplicates_adds_nothing_but_still_offers(window):
with _no_dialog():
added = add_tracks_with_dup_check(
window._manager, window._playlist_view, "PID", [2])
assert added == []
assert _playlist(window).track_ids == [1, 2]
assert "nothing was added" in window.duplicate_notice.label.text()
window.duplicate_notice.button.click()
assert _playlist(window).track_ids == [1, 2, 2]
def test_it_fades_away_after_ten_seconds(window, qapp):
notice = window.duplicate_notice
with _no_dialog():
add_tracks_with_dup_check(
window._manager, window._playlist_view, "PID", [1])
assert notice._timer.isActive()
assert notice._timer.interval() == duplicate_notice.SHOW_MS == 10_000
notice._timer.timeout.emit() # ten seconds pass
assert notice._fade.state() == notice._fade.State.Running
notice._fade.setCurrentTime(duplicate_notice.FADE_MS)
qapp.processEvents()
assert not notice.isVisible()
def test_the_pref_brings_the_prompt_back(window):
window._prefs.set("ask_on_duplicates", True)
with patch.object(playlist_ops, "_resolve_duplicates",
return_value=[3]) as asked:
added = add_tracks_with_dup_check(
window._manager, window._playlist_view, "PID", [1, 3])
asked.assert_called_once()
assert added == [3]
assert not window.duplicate_notice.isVisible()
def test_quiet_is_the_default():
from lintunes.preferences import DEFAULTS
assert DEFAULTS["ask_on_duplicates"] is False