| 1 |
from typing import Any, ClassVar |
| 2 |
from unittest.mock import ANY, Mock, call, patch |
| 3 |
|
| 4 |
from beets import util |
| 5 |
from beets.library import Item |
| 6 |
from beets.test.helper import PluginTestHelper |
| 7 |
from beetsplug.mpdstats import MPDStats, mpd_config |
| 8 |
|
| 9 |
|
| 10 |
class TestMPDStats(PluginTestHelper): |
| 11 |
plugin = "mpdstats" |
| 12 |
|
| 13 |
def test_update_rating(self): |
| 14 |
item = Item(title="title", path="", id=1) |
| 15 |
item.add(self.lib) |
| 16 |
|
| 17 |
log = Mock() |
| 18 |
mpdstats = MPDStats(self.lib, log) |
| 19 |
|
| 20 |
assert not mpdstats.update_rating(item, True) |
| 21 |
assert not mpdstats.update_rating(None, True) |
| 22 |
|
| 23 |
def test_get_item(self): |
| 24 |
item_path = util.normpath("/foo/bar.flac") |
| 25 |
item = Item(title="title", path=item_path, id=1) |
| 26 |
item.add(self.lib) |
| 27 |
|
| 28 |
log = Mock() |
| 29 |
mpdstats = MPDStats(self.lib, log) |
| 30 |
|
| 31 |
assert str(mpdstats.get_item(item_path)) == str(item) |
| 32 |
assert mpdstats.get_item("/some/non-existing/path") is None |
| 33 |
assert "item not found:" in log.info.call_args[0][0] |
| 34 |
|
| 35 |
STATUSES: ClassVar[list[dict[str, Any]]] = [ |
| 36 |
{"state": "pause"}, |
| 37 |
{"state": "play", "songid": 1, "time": "0:1"}, |
| 38 |
{"state": "stop"}, |
| 39 |
] |
| 40 |
EVENTS = [["player"]] * (len(STATUSES) - 1) + [KeyboardInterrupt] |
| 41 |
item_path = util.normpath("/foo/bar.flac") |
| 42 |
songid = 1 |
| 43 |
|
| 44 |
@patch( |
| 45 |
"beetsplug.mpdstats.MPDClientWrapper", |
| 46 |
return_value=Mock( |
| 47 |
**{ |
| 48 |
"events.side_effect": EVENTS, |
| 49 |
"status.side_effect": STATUSES, |
| 50 |
"currentsong.return_value": (item_path, songid), |
| 51 |
} |
| 52 |
), |
| 53 |
) |
| 54 |
def test_run_mpdstats(self, mpd_mock): |
| 55 |
item = Item(title="title", path=self.item_path, id=1) |
| 56 |
item.add(self.lib) |
| 57 |
|
| 58 |
log = Mock() |
| 59 |
try: |
| 60 |
MPDStats(self.lib, log).run() |
| 61 |
except KeyboardInterrupt: |
| 62 |
pass |
| 63 |
|
| 64 |
log.info.assert_has_calls( |
| 65 |
[call("pause"), call("playing {}", ANY), call("stop")] |
| 66 |
) |
| 67 |
|
| 68 |
@patch("beetsplug.mpdstats.MPDStats.run") |
| 69 |
def test_cli_options_override_config(self, run_mock): |
| 70 |
self.run_command( |
| 71 |
"mpdstats", |
| 72 |
"--host", |
| 73 |
"somehost", |
| 74 |
"--port", |
| 75 |
"5000", |
| 76 |
"--password", |
| 77 |
"secret", |
| 78 |
) |
| 79 |
|
| 80 |
assert mpd_config["host"].as_str() == "somehost" |
| 81 |
assert mpd_config["port"].get(int) == 5000 |
| 82 |
assert mpd_config["password"].as_str() == "secret" |
| 83 |
run_mock.assert_called_once_with() |