| 1 |
import os |
| 2 |
from pathlib import Path |
| 3 |
from unittest.mock import Mock, patch |
| 4 |
|
| 5 |
from beets import library, util |
| 6 |
from beets.test import _common |
| 7 |
from beets.test.helper import PluginTestCase, PluginTestHelper |
| 8 |
from beetsplug.ipfs import IPFSPlugin |
| 9 |
|
| 10 |
|
| 11 |
@patch("beets.util.command_output", Mock()) |
| 12 |
class IPFSPluginTest(PluginTestCase): |
| 13 |
plugin = "ipfs" |
| 14 |
|
| 15 |
def test_stored_hashes(self): |
| 16 |
test_album = self.mk_test_album() |
| 17 |
ipfs = IPFSPlugin() |
| 18 |
added_albums = ipfs.ipfs_added_albums(self.lib, self.lib.path) |
| 19 |
with added_albums.music_dir_context(): |
| 20 |
added_album = added_albums.get_album(1) |
| 21 |
assert added_album.ipfs == test_album.ipfs |
| 22 |
found = False |
| 23 |
want_item = test_album.items()[2] |
| 24 |
for check_item in added_album.items(): |
| 25 |
if check_item.get("ipfs", with_album=False): |
| 26 |
ipfs_item = want_item.filepath.name |
| 27 |
want_path = ( |
| 28 |
Path("/ipfs").resolve() / test_album.ipfs / ipfs_item |
| 29 |
) |
| 30 |
assert check_item.filepath == want_path |
| 31 |
assert ( |
| 32 |
check_item.get("ipfs", with_album=False) |
| 33 |
== want_item.ipfs |
| 34 |
) |
| 35 |
assert check_item.title == want_item.title |
| 36 |
found = True |
| 37 |
assert found |
| 38 |
|
| 39 |
def test_get_remote_lib_accepts_library_path(self): |
| 40 |
self.lib.path = self.temp_path / "library.db" |
| 41 |
remote_dir = self.temp_path / "remotes" |
| 42 |
remote_dir.mkdir() |
| 43 |
|
| 44 |
remote_lib = library.Library(remote_dir / "joined.db") |
| 45 |
remote_lib._close() |
| 46 |
|
| 47 |
ipfs = IPFSPlugin() |
| 48 |
with ipfs.remote_lib(self.lib) as added_lib: |
| 49 |
assert added_lib.path == remote_dir / "joined.db" |
| 50 |
|
| 51 |
def mk_test_album(self): |
| 52 |
items = [_common.item() for _ in range(3)] |
| 53 |
items[0].title = "foo bar" |
| 54 |
items[0].artist = "1one" |
| 55 |
items[0].album = "baz" |
| 56 |
items[0].year = 2001 |
| 57 |
items[0].comp = True |
| 58 |
items[1].title = "baz qux" |
| 59 |
items[1].artist = "2two" |
| 60 |
items[1].album = "baz" |
| 61 |
items[1].year = 2002 |
| 62 |
items[1].comp = True |
| 63 |
items[2].title = "beets 4 eva" |
| 64 |
items[2].artist = "3three" |
| 65 |
items[2].album = "foo" |
| 66 |
items[2].year = 2003 |
| 67 |
items[2].comp = False |
| 68 |
items[2].ipfs = "QmfM9ic5LJj7V6ecozFx1MkSoaaiq3PXfhJoFvyqzpLXSk" |
| 69 |
|
| 70 |
for item in items: |
| 71 |
self.lib.add(item) |
| 72 |
|
| 73 |
album = self.lib.add_album(items) |
| 74 |
album.ipfs = "QmfM9ic5LJj7V6ecozFx1MkSoaaiq3PXfhJoFvyqzpLXSf" |
| 75 |
album.store(inherit=False) |
| 76 |
|
| 77 |
return album |
| 78 |
|
| 79 |
|
| 80 |
class TestIPFSPlay(PluginTestHelper): |
| 81 |
plugin = "ipfs" |
| 82 |
db_on_disk = True |
| 83 |
|
| 84 |
def test_ipfs_play(self, monkeypatch): |
| 85 |
"""Test that ipfs successfully calls PlayPlugin's play method.""" |
| 86 |
# do not attempt to actually play the music |
| 87 |
monkeypatch.setattr("beetsplug.play.play", lambda *_: None) |
| 88 |
|
| 89 |
# we need some music to play |
| 90 |
self.add_album_fixture() |
| 91 |
|
| 92 |
# create remote lib in the expected place, |
| 93 |
# see IPFSPlugin._remote_libs_path |
| 94 |
remote_dir = Path(os.fsdecode(self.lib.path)).parent / "remotes" |
| 95 |
remote_dir.mkdir() |
| 96 |
util.copy(self.lib.path, remote_dir / "joined.db") |
| 97 |
|
| 98 |
# check that we can play without any errors |
| 99 |
IPFSPlugin().ipfs_play(self.lib, None, []) |