refactor: lazy load credentials for testing

This commit is contained in:
2026-04-06 07:20:23 +02:00
parent a8335d9920
commit 9b04160783
8 changed files with 110 additions and 19 deletions
+10
View File
@@ -1,3 +1,13 @@
import pytest
from lrx_cli.config import enable_debug
enable_debug()
@pytest.fixture
def no_credentials(monkeypatch):
"""Clear all credential env vars so only anonymous fetchers are active."""
monkeypatch.delenv("SPOTIFY_SP_DC", raising=False)
monkeypatch.delenv("QQ_MUSIC_API_URL", raising=False)
monkeypatch.delenv("MUSIXMATCH_USERTOKEN", raising=False)
+16
View File
@@ -0,0 +1,16 @@
import os
import pytest
requires_spotify = pytest.mark.skipif(
not os.environ.get("SPOTIFY_SP_DC"),
reason="requires SPOTIFY_SP_DC",
)
requires_qq_music = pytest.mark.skipif(
not os.environ.get("QQ_MUSIC_API_URL"),
reason="requires QQ_MUSIC_API_URL",
)
requires_musixmatch_token = pytest.mark.skipif(
not os.environ.get("MUSIXMATCH_USERTOKEN"),
reason="requires MUSIXMATCH_USERTOKEN",
)
+39 -1
View File
@@ -5,6 +5,11 @@ from dataclasses import replace
from lrx_cli.fetchers import FetcherMethodType
from lrx_cli.models import TrackMeta
from lrx_cli.core import LrcManager
from tests.marks import (
requires_spotify,
requires_qq_music,
requires_musixmatch_token,
)
SAMPLE_SPOTIFY_TRACK: TrackMeta = TrackMeta(
title="One Last Kiss",
@@ -94,13 +99,46 @@ def test_cache_search_fetcher_prefer_better_match(lrc_manager: LrcManager):
("lrclib", False),
("lrclib-search", False),
("netease", False),
("spotify", True), # requires auth
("qqmusic", True), # requires api
],
)
def test_anonymous_remote_fetchers(
lrc_manager: LrcManager, method: FetcherMethodType, expect_fail: bool
no_credentials,
lrc_manager: LrcManager,
method: FetcherMethodType,
expect_fail: bool,
):
_fetch_and_assert(lrc_manager, method, expect_fail)
@pytest.mark.network
@requires_spotify
def test_spotify_fetcher(lrc_manager: LrcManager):
_fetch_and_assert(lrc_manager, "spotify")
@pytest.mark.network
@requires_qq_music
def test_qqmusic_fetcher(lrc_manager: LrcManager):
_fetch_and_assert(lrc_manager, "qqmusic")
@pytest.mark.network
def test_musixmatch_anonymous_fetcher(no_credentials, lrc_manager: LrcManager):
# These fetchers should be tested in a single test to share the same usertoken
# Otherwise the second may fail due to rate limits
_fetch_and_assert(lrc_manager, "musixmatch", expect_fail=False)
_fetch_and_assert(lrc_manager, "musixmatch-spotify", expect_fail=False)
@pytest.mark.network
@requires_musixmatch_token
def test_musixmatch_fetcher(lrc_manager: LrcManager):
_fetch_and_assert(lrc_manager, "musixmatch")
_fetch_and_assert(lrc_manager, "musixmatch-spotify")
def test_local_fetcher(lrc_manager: LrcManager):
# Since this not a local track
_fetch_and_assert(lrc_manager, "local", True)