feat(discovery): ListenBrainz similarity source
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import requests
|
||||
|
||||
from lyra_worker.similarity._listenbrainz import ListenBrainzSource
|
||||
from lyra_worker.similarity.base import SimilarArtist
|
||||
|
||||
|
||||
def _resp(payload, status=200):
|
||||
r = MagicMock()
|
||||
r.status_code = status
|
||||
r.json.return_value = payload
|
||||
r.raise_for_status.side_effect = (
|
||||
None if status < 400 else requests.HTTPError(f"{status}"))
|
||||
return r
|
||||
|
||||
|
||||
def test_similar_artists_parses_and_drops_self():
|
||||
payload = [
|
||||
{"artist_mbid": "seed", "name": "Seed", "score": 100},
|
||||
{"artist_mbid": "c1", "name": "Cand One", "score": 42},
|
||||
{"artist_mbid": "c2", "name": "Cand Two", "score": 7},
|
||||
]
|
||||
with patch("lyra_worker.similarity._listenbrainz.requests.get", return_value=_resp(payload)):
|
||||
out = ListenBrainzSource().similar_artists("seed")
|
||||
assert out == [SimilarArtist("c1", "Cand One", 42.0), SimilarArtist("c2", "Cand Two", 7.0)]
|
||||
|
||||
|
||||
def test_similar_artists_raises_on_http_error():
|
||||
with patch("lyra_worker.similarity._listenbrainz.requests.get", return_value=_resp([], status=500)):
|
||||
try:
|
||||
ListenBrainzSource().similar_artists("seed")
|
||||
assert False, "expected HTTPError"
|
||||
except requests.HTTPError:
|
||||
pass
|
||||
|
||||
|
||||
def test_health_true_on_2xx_false_on_exception():
|
||||
with patch("lyra_worker.similarity._listenbrainz.requests.get", return_value=_resp([], status=200)):
|
||||
assert ListenBrainzSource().health() is True
|
||||
with patch("lyra_worker.similarity._listenbrainz.requests.get",
|
||||
side_effect=requests.ConnectionError("down")):
|
||||
assert ListenBrainzSource().health() is False
|
||||
Reference in New Issue
Block a user