streamrip/tests/test_qobuz_client.py

84 lines
2.5 KiB
Python
Raw Permalink Normal View History

2024-01-14 07:57:16 +01:00
import hashlib
2023-10-22 04:18:06 +02:00
import logging
import os
2023-10-22 04:18:06 +02:00
import pytest
from util import arun
2023-10-22 04:18:06 +02:00
2023-12-26 21:20:34 +01:00
from streamrip.client.downloadable import BasicDownloadable
from streamrip.client.qobuz import QobuzClient
from streamrip.config import Config
from streamrip.exceptions import MissingCredentialsError
2023-10-22 04:18:06 +02:00
logger = logging.getLogger("streamrip")
2024-01-14 07:57:16 +01:00
@pytest.fixture(scope="session")
def qobuz_client():
config = Config.defaults()
config.session.qobuz.email_or_userid = os.environ["QOBUZ_EMAIL"]
config.session.qobuz.password_or_token = hashlib.md5(
os.environ["QOBUZ_PASSWORD"].encode("utf-8"),
).hexdigest()
if "QOBUZ_APP_ID" in os.environ and "QOBUZ_SECRETS" in os.environ:
config.session.qobuz.app_id = os.environ["QOBUZ_APP_ID"]
config.session.qobuz.secrets = os.environ["QOBUZ_SECRETS"].split(",")
client = QobuzClient(config)
arun(client.login())
yield client
arun(client.session.close())
2023-10-22 04:18:06 +02:00
def test_client_raises_missing_credentials():
c = Config.defaults()
2023-12-26 21:20:34 +01:00
with pytest.raises(MissingCredentialsError):
2023-10-22 04:18:06 +02:00
arun(QobuzClient(c).login())
@pytest.mark.skipif(
"QOBUZ_EMAIL" not in os.environ, reason="Qobuz credentials not found in env."
)
2024-01-14 07:57:16 +01:00
def test_client_get_metadata(qobuz_client):
meta = arun(qobuz_client.get_metadata("s9nzkwg2rh1nc", "album"))
2023-10-22 04:18:06 +02:00
assert meta["title"] == "I Killed Your Dog"
assert len(meta["tracks"]["items"]) == 16
assert meta["maximum_bit_depth"] == 24
@pytest.mark.skipif(
"QOBUZ_EMAIL" not in os.environ, reason="Qobuz credentials not found in env."
)
2024-01-14 07:57:16 +01:00
def test_client_get_downloadable(qobuz_client):
d = arun(qobuz_client.get_downloadable("19512574", 3))
2023-10-22 04:18:06 +02:00
assert isinstance(d, BasicDownloadable)
assert d.extension == "flac"
assert isinstance(d.url, str)
assert "https://" in d.url
@pytest.mark.skipif(
"QOBUZ_EMAIL" not in os.environ, reason="Qobuz credentials not found in env."
)
2024-01-14 07:57:16 +01:00
def test_client_search_limit(qobuz_client):
res = qobuz_client.search("album", "rumours", limit=5)
2023-10-22 04:18:06 +02:00
total = 0
for r in arun(res):
2023-10-22 04:18:06 +02:00
total += len(r["albums"]["items"])
assert total == 5
@pytest.mark.skipif(
"QOBUZ_EMAIL" not in os.environ, reason="Qobuz credentials not found in env."
)
2024-01-14 07:57:16 +01:00
def test_client_search_no_limit(qobuz_client):
# Setting no limit has become impossible because `limit: int` now
2024-01-14 07:57:16 +01:00
res = qobuz_client.search("album", "rumours", limit=10000)
2023-10-22 04:18:06 +02:00
correct_total = 0
total = 0
for r in arun(res):
2023-10-22 04:18:06 +02:00
total += len(r["albums"]["items"])
correct_total = max(correct_total, r["albums"]["total"])
assert total == correct_total