1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
from quodlibet import config, app
from quodlibet.browsers.albums import AlbumList
from quodlibet.formats import AudioFile
from quodlibet.util.collection import Album
from quodlibet.util.dprint import print_d
from tests import init_fake_app, destroy_fake_app
from tests.plugin import PluginTestCase
A1S1 = AudioFile(
{
"album": "greatness",
"title": "excellent",
"artist": "fooman",
"~#lastplayed": 1234,
"~#rating": 0.75,
}
)
A1S2 = AudioFile(
{
"album": "greatness",
"title": "superlative",
"artist": "fooman",
"~#lastplayed": 1234,
"~#rating": 1.0,
}
)
A1 = Album(A1S1)
A1.songs = {A1S1, A1S2}
A2S1 = AudioFile(
{"album": "mediocrity", "title": "blah", "artist": "fooman", "~#lastplayed": 1234}
)
A2S2 = AudioFile(
{"album": "mediocrity", "title": "meh", "artist": "fooman", "~#lastplayed": 1234}
)
A2 = Album(A2S1)
A2.songs = {A2S1, A2S2}
A3S1 = AudioFile(
{
"album": "disappointment",
"title": "shameful",
"artist": "poorman",
"~#lastplayed": 2345,
"~#rating": 0.25,
}
)
A3S2 = AudioFile(
{
"album": "disappointment",
"title": "zero",
"artist": "poorman",
"~#lastplayed": 2345,
"~#rating": 0.0,
}
)
A3S3 = AudioFile(
{
"album": "disappointment",
"title": "lame",
"artist": "poorman",
"~#lastplayed": 0,
"~#rating": 0.25,
}
)
A3 = Album(A3S1)
A3.songs = {A3S1, A3S2, A3S3}
for song in [A1S1, A1S2, A2S1, A2S2, A3S1, A3S2, A3S3]:
song["~#length"] = 100
class TRandomAlbum(PluginTestCase):
"""Some basic tests for the random album plugin algorithm"""
WEIGHTS = {
"rating": 0,
"added": 0,
"laststarted": 0,
"lastplayed": 0,
"length": 0,
"skipcount": 0,
"playcount": 0,
}
def setUp(self):
config.init()
init_fake_app()
app.player.paused = False
# Only album browsers are supported currently
app.library.clear()
app.window.browser = AlbumList(app.library)
self.plugin = self.plugins["Random Album Playback"].cls()
self.albums = [A1, A2, A3]
def tearDown(self):
app.window.browser.destroy()
destroy_fake_app()
config.quit()
def get_winner(self, albums):
print_d(f"Weights: {self.plugin.weights} ")
scores = self.plugin._score(albums)
print_d(f"Scores: {scores}")
if not scores:
return None
return max(scores)[1]
def test_empty_integration_weighted(self):
# See issue #2756
self.plugin.use_weights = True
assert not self.plugin.plugin_on_song_started(None)
def test_empty_integration(self):
# See issue #2756
self.plugin.use_weights = False
assert not self.plugin.plugin_on_song_started(None)
def test_score_rating(self):
weights = self.plugin.weights = self.WEIGHTS.copy()
weights["rating"] = 1
self.assertEqual(A1, self.get_winner(self.albums))
def test_score_length(self):
weights = self.plugin.weights = self.WEIGHTS.copy()
weights["length"] = 1
self.assertEqual(A3, self.get_winner(self.albums))
def test_score_lastplayed(self):
weights = self.plugin.weights = self.WEIGHTS.copy()
weights["lastplayed"] = 1
self.assertEqual(A3, self.get_winner(self.albums))
def test_score_lastplayed_added(self):
weights = self.plugin.weights = self.WEIGHTS.copy()
weights["lastplayed"] = 1
# No data here
weights["added"] = 1
self.assertEqual(A3, self.get_winner(self.albums))
def test_score_mixed(self):
print_d("Starting.")
weights = self.plugin.weights = self.WEIGHTS.copy()
weights["length"] = 1
weights["lastplayed"] = 2
weights["rating"] = 1
# A3 is #3 rating, #1 in lastplayed, #1 in length
self.assertEqual(A3, self.get_winner(self.albums))
weights["lastplayed"] = 1
weights["rating"] = 2
weights["length"] = 0.5
# A1 is #1 for Rating, #2 for lastplayed, #2 or 3 length
self.assertEqual(A1, self.get_winner(self.albums))
|