Skip to content

Commit

Permalink
Merge pull request #8 from Dr-Blank/test-for-lyrics-publish
Browse files Browse the repository at this point in the history
Improve coverage
  • Loading branch information
Dr-Blank authored Nov 2, 2023
2 parents 81c886d + 77ec387 commit 02d1a71
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 26 deletions.
39 changes: 26 additions & 13 deletions tests/test_api_with_mock.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import os
import warnings
from unittest.mock import Mock

import pytest
from requests import HTTPError, Response

from lrclib.api import BASE_URL, ENDPOINTS, LrcLibAPI
from lrclib.exceptions import (APIError, IncorrectPublishTokenError,
NotFoundError, RateLimitError, ServerError)
from lrclib.models import (CryptographicChallenge, Lyrics, LyricsMinimal,
SearchResult)
from lrclib.cryptographic_challenge_solver import CryptoChallengeSolver
from lrclib.exceptions import (
APIError,
IncorrectPublishTokenError,
NotFoundError,
RateLimitError,
ServerError,
)
from lrclib.models import (
CryptographicChallenge,
Lyrics,
LyricsMinimal,
SearchResult,
)


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -175,13 +186,18 @@ def test_publish_lyrics(api: LrcLibAPI) -> None:
session_mock = Mock()
session_mock.request.return_value.json.return_value = {"status": "success"}

# Mock the obtain_publish_token method of the LrcLibAPI instance
api_mock = Mock()
api_mock.obtain_publish_token.return_value = "test_publish_token"
# Mock the challenge
request_challenge = Mock()
simple_challenge = CryptographicChallenge("test_prefix", "0f")
solution = CryptoChallengeSolver.solve(
simple_challenge.prefix,
simple_challenge.target,
num_threads=os.cpu_count() or 1,
)
request_challenge.return_value = simple_challenge

# Set the session object and obtain_publish_token method of the LrcLibAPI instance to the mock objects
api.session = session_mock
api._obtain_publish_token = api_mock.obtain_publish_token
api.request_challenge = request_challenge

# Call the publish_lyrics method
result = api.publish_lyrics(
Expand All @@ -193,14 +209,11 @@ def test_publish_lyrics(api: LrcLibAPI) -> None:
synced_lyrics="test_synced_lyrics",
)

# Check that the obtain_publish_token method was called
api_mock.obtain_publish_token.assert_called_once()

# Check that the session's request method was called with the correct arguments
session_mock.request.assert_called_once_with(
"POST",
BASE_URL + ENDPOINTS["publish"],
headers={"X-Publish-Token": "test_publish_token"},
headers={"X-Publish-Token": f"{simple_challenge.prefix}:{solution}"},
json={
"trackName": "test_track_name",
"artistName": "test_artist_name",
Expand Down
10 changes: 10 additions & 0 deletions tests/test_crypto_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

from lrclib.cryptographic_challenge_solver import (
CryptoChallengeSolver,
Solution,
is_nonce_valid,
find_nonce,
)

easy_target_hex = (
Expand All @@ -16,6 +18,14 @@ def random_prefix() -> str:
return "".join(random.choice(string.ascii_letters) for _ in range(10))


def test_find_nonce() -> None:
prefix = random_prefix()
target_hex = easy_target_hex
nonce = find_nonce(prefix, bytes.fromhex(target_hex))
assert isinstance(nonce, Solution)
assert is_nonce_valid(prefix, nonce.nonce, bytes.fromhex(target_hex))


def test_solve_random() -> None:
prefix = random_prefix()
target_hex = easy_target_hex
Expand Down
File renamed without changes.
32 changes: 19 additions & 13 deletions tests/test_api_models.py → tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
CryptographicChallenge,
)

minimized_lyrics = {
minimal_lyrics = {
"id": 123,
"name": "test_name",
"trackName": "test_track_name",
Expand All @@ -23,34 +23,34 @@
}

full_lyrics = {
**minimized_lyrics,
**minimal_lyrics,
"lang": "test_lang",
"isrc": "test_isrc",
"spotifyId": "test_spotify_id",
"releaseDate": "2023-08-10T00:00:00Z",
}

sample_search_result = [minimized_lyrics, minimized_lyrics]
sample_search_result = [minimal_lyrics, minimal_lyrics]


def test_lyrics_minimal_from_dict() -> None:
"""Test the LyricsMinimal.from_dict method"""
# Create a LyricsMinimal object from a dictionary
lyrics = LyricsMinimal.from_dict(minimized_lyrics)
lyrics = LyricsMinimal.from_dict(minimal_lyrics)

# Check that the LyricsMinimal object has the expected type
assert isinstance(lyrics, LyricsMinimal)

# Check that the LyricsMinimal object has the expected attributes
assert lyrics.id == minimized_lyrics["id"]
assert lyrics.name == minimized_lyrics["name"]
assert lyrics.track_name == minimized_lyrics["trackName"]
assert lyrics.artist_name == minimized_lyrics["artistName"]
assert lyrics.album_name == minimized_lyrics["albumName"]
assert lyrics.duration == minimized_lyrics["duration"]
assert lyrics.instrumental == minimized_lyrics["instrumental"]
assert lyrics.plain_lyrics == minimized_lyrics["plainLyrics"]
assert lyrics.synced_lyrics == minimized_lyrics["syncedLyrics"]
assert lyrics.id == minimal_lyrics["id"]
assert lyrics.name == minimal_lyrics["name"]
assert lyrics.track_name == minimal_lyrics["trackName"]
assert lyrics.artist_name == minimal_lyrics["artistName"]
assert lyrics.album_name == minimal_lyrics["albumName"]
assert lyrics.duration == minimal_lyrics["duration"]
assert lyrics.instrumental == minimal_lyrics["instrumental"]
assert lyrics.plain_lyrics == minimal_lyrics["plainLyrics"]
assert lyrics.synced_lyrics == minimal_lyrics["syncedLyrics"]


def test_lyrics_from_dict() -> None:
Expand All @@ -73,6 +73,12 @@ def test_lyrics_from_dict() -> None:
assert lyrics.release_date == datetime(2023, 8, 10, 0, 0, 0)


def test_lyrics_from_dict_with_proper_release_date() -> None:
lyrics = Lyrics.from_dict(full_lyrics)
lyrics_proper = Lyrics(**lyrics.__dict__)
assert lyrics_proper.release_date == datetime(2023, 8, 10, 0, 0, 0)


def test_error_response_from_dict() -> None:
error_response = ErrorResponse.from_dict(
{"error": "test_error", "message": "test_message"}
Expand Down

0 comments on commit 02d1a71

Please sign in to comment.