Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions streamrip/client/deezer.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, config: Config):
self.client = deezer.Deezer()
self.logged_in = False
self.config = config.session.deezer
self._album_cache = {}

async def login(self):
# Used for track downloads
Expand Down Expand Up @@ -89,12 +90,16 @@ async def get_track(self, item_id: str) -> dict:
return item

async def get_album(self, item_id: str) -> dict:
if item_id in self._album_cache:
logger.info(f"Deezer album cache hit for album ID: {item_id}")
return self._album_cache[item_id]
album_metadata, album_tracks = await asyncio.gather(
asyncio.to_thread(self.client.api.get_album, item_id),
asyncio.to_thread(self.client.api.get_album_tracks, item_id),
)
album_metadata["tracks"] = album_tracks["data"]
album_metadata["track_total"] = len(album_tracks["data"])
self._album_cache[item_id] = album_metadata
return album_metadata

async def get_playlist(self, item_id: str) -> dict:
Expand Down
17 changes: 17 additions & 0 deletions tests/test_deezer.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,23 @@ def test_deezer_no_fallback_when_disabled(mock_deezer_client):
with pytest.raises(NonStreamableError, match="The requested quality 2 is not available and fallback is disabled"):
arun(mock_deezer_client.get_downloadable("123", quality=2))

def test_deezer_album_cache(mock_deezer_client):
"""Unit test: verify get_album results are cached and retrieved on subsequent calls"""
mock_deezer_client.client.api.get_album.return_value = {"id": "album_123", "title": "Test Album", "genres": {"data": []}}
mock_deezer_client.client.api.get_album_tracks.return_value = {"data": []}

# Call get_album twice
res1 = arun(mock_deezer_client.get_album("album_123"))
res2 = arun(mock_deezer_client.get_album("album_123"))

# Assert return values are identical
assert res1 == res2
assert res1["title"] == "Test Album"

# Assert api call was made exactly once
assert mock_deezer_client.client.api.get_album.call_count == 1
assert mock_deezer_client.client.api.get_album_tracks.call_count == 1

# ===== INTEGRATION TEST =====

@pytest.mark.skipif(
Expand Down