66 lines
2.0 KiB
Python
66 lines
2.0 KiB
Python
from lyra_worker.adapters.youtube import YouTubeAdapter
|
|
from lyra_worker.types import Candidate, MBTarget, Quality
|
|
|
|
TARGET = MBTarget(artist="Radiohead", album="In Rainbows", track_count=10)
|
|
|
|
|
|
class FakeYtClient:
|
|
def __init__(self, results=None, fail=False):
|
|
self._results = results if results is not None else [
|
|
{"source_ref": "yt:playlist:abc", "title": "In Rainbows", "artist": "Radiohead", "track_count": 10}
|
|
]
|
|
self._fail = fail
|
|
self.downloaded = []
|
|
|
|
def search_album(self, artist, album):
|
|
return self._results
|
|
|
|
def download(self, source_ref, dest, on_progress):
|
|
if self._fail:
|
|
raise RuntimeError("network boom")
|
|
on_progress(1.0)
|
|
self.downloaded.append((source_ref, dest))
|
|
return {"track_count": 10, "path": dest}
|
|
|
|
|
|
def test_name_and_tier():
|
|
a = YouTubeAdapter(FakeYtClient())
|
|
assert a.name == "youtube"
|
|
assert a.tier == 2
|
|
assert a.health() is True
|
|
|
|
|
|
def test_search_maps_results_to_candidates():
|
|
cands = YouTubeAdapter(FakeYtClient()).search(TARGET)
|
|
assert len(cands) == 1
|
|
c = cands[0]
|
|
assert c.source == "youtube"
|
|
assert c.source_ref == "yt:playlist:abc"
|
|
assert c.matched_artist == "Radiohead"
|
|
assert c.matched_album == "In Rainbows"
|
|
assert c.track_count == 10
|
|
assert c.source_tier == 2
|
|
assert c.quality.lossless is False
|
|
|
|
|
|
def test_search_empty_when_no_results():
|
|
assert YouTubeAdapter(FakeYtClient(results=[])).search(TARGET) == []
|
|
|
|
|
|
def test_download_success():
|
|
client = FakeYtClient()
|
|
cand = YouTubeAdapter(client).search(TARGET)[0]
|
|
seen = []
|
|
result = YouTubeAdapter(client).download(cand, "/tmp/x", seen.append)
|
|
assert result.ok is True
|
|
assert result.track_count == 10
|
|
assert seen and seen[-1] == 1.0
|
|
|
|
|
|
def test_download_failure_returns_not_ok():
|
|
client = FakeYtClient(fail=True)
|
|
cand = YouTubeAdapter(FakeYtClient()).search(TARGET)[0]
|
|
result = YouTubeAdapter(client).download(cand, "/tmp/x", lambda _p: None)
|
|
assert result.ok is False
|
|
assert result.error
|