68 lines
2.2 KiB
Python
68 lines
2.2 KiB
Python
from fastapi.testclient import TestClient
|
|
from app.main import app
|
|
|
|
c = TestClient(app)
|
|
|
|
|
|
def test_album_detail_mocked(monkeypatch):
|
|
from app.api import album as al
|
|
|
|
async def fake_details(album_id):
|
|
return {
|
|
"id": album_id,
|
|
"title": "Rumours",
|
|
"artist": "Fleetwood Mac",
|
|
"cover": "https://cdn-images.dzcdn.net/cover.jpg",
|
|
"release_date": "1977-02-04",
|
|
"tracks": [
|
|
{"id": "1", "title": "Second Hand News", "track_number": 1, "duration": 172},
|
|
{"id": "2", "title": "Dreams", "track_number": 2, "duration": 257},
|
|
],
|
|
}
|
|
|
|
monkeypatch.setattr(al, "get_album_details", fake_details)
|
|
r = c.get("/api/album/123")
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["title"] == "Rumours"
|
|
assert len(body["tracks"]) == 2
|
|
assert body["tracks"][0]["track_number"] == 1
|
|
|
|
|
|
def test_album_track_mapping_unit():
|
|
from app.services.streamrip_service import _map_track_items
|
|
|
|
items = [
|
|
{"id": 11, "title": "A", "track_position": 2, "disk_number": 1, "duration": 200,
|
|
"preview": "http://x", "explicit_lyrics": True,
|
|
"artist": {"name": "X"}, "album": {"cover_big": "http://c"}},
|
|
{"id": 12, "title": "B"}, # sparse -> defaults
|
|
]
|
|
tracks = _map_track_items(items)
|
|
assert tracks[0]["id"] == "11"
|
|
assert tracks[0]["track_number"] == 2
|
|
assert tracks[0]["preview_url"] == "http://x"
|
|
assert tracks[0]["explicit"] is True
|
|
assert tracks[1]["track_number"] == 2 # fallback: position in list
|
|
assert tracks[1]["duration"] is None
|
|
|
|
|
|
def test_fetch_album_meta_falls_back_to_embedded_tracks():
|
|
import asyncio
|
|
|
|
from app.services.streamrip_service import _fetch_album_meta
|
|
|
|
class FakeApi:
|
|
def get_album(self, _id):
|
|
return {"id": _id, "title": "X", "tracks": {"data": [{"id": 1, "title": "T"}]}}
|
|
|
|
def get_album_tracks(self, _id):
|
|
raise Exception("no data")
|
|
|
|
class FakeClient:
|
|
class client:
|
|
api = FakeApi()
|
|
|
|
meta = asyncio.run(_fetch_album_meta(FakeClient(), "5"))
|
|
assert meta["tracks"] == [{"id": 1, "title": "T"}]
|