Skip to content

Commit

Permalink
Refactorize the tests for file downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
kutu-dev committed Sep 30, 2023
1 parent 8e91ab6 commit c466c1f
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 139 deletions.
4 changes: 3 additions & 1 deletion src/knuckles/media_retrieval.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ def hls(self, id: str) -> str:
def get_captions(self) -> None:
...

def get_cover_art(self, id: str, file_or_directory_path: Path, size: int) -> Path:
def get_cover_art(
self, id: str, file_or_directory_path: Path, size: int | None = None
) -> Path:
"""Calls the "getCoverArt" endpoint of the API.
:param id: The id of the cover art to download.
Expand Down
122 changes: 41 additions & 81 deletions tests/api/test_media_retrieval.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
from pathlib import Path
from typing import Any


from responses import Response
import responses
from knuckles import Subsonic

from tests.mocks.media_retrieval import MockDownload
from tests.mocks.media_retrieval import FileMetadata


def test_stream(subsonic: Subsonic, song: dict[str, Any]) -> None:
Expand All @@ -18,58 +18,44 @@ def test_stream(subsonic: Subsonic, song: dict[str, Any]) -> None:

@responses.activate
def test_download_with_a_given_filename(
subsonic: Subsonic,
mock_download: Response,
tmp_path: Path,
output_song_filename: str,
placeholder_data: str,
mock_download_file: MockDownload,
subsonic: Subsonic,
song: dict[str, Any],
song_content_type: str,
download_metadata: FileMetadata,
) -> None:
responses.add(
mock_download_file("download", {"id": song["id"]}, tmp_path, song_content_type)
)
responses.add(mock_download)

download_path = subsonic.media_retrieval.download(
song["id"], tmp_path / output_song_filename
song["id"], tmp_path / download_metadata.output_filename
)

# Check if the file data has been unaltered
with open(tmp_path / output_song_filename, "r") as file:
with open(tmp_path / download_metadata.output_filename, "r") as file:
assert placeholder_data == file.read()

assert download_path == tmp_path / output_song_filename
assert download_path == tmp_path / download_metadata.output_filename


@responses.activate
def test_download_without_a_given_filename(
subsonic: Subsonic,
mock_download: Response,
tmp_path: Path,
default_song_filename: str,
placeholder_data: str,
mock_download_file: MockDownload,
subsonic: Subsonic,
song: dict[str, Any],
song_content_type: str,
download_metadata: FileMetadata,
) -> None:
responses.add(
mock_download_file(
"download",
{"id": song["id"]},
tmp_path,
song_content_type,
headers={
"Content-Disposition": f'attachment; filename="{default_song_filename}"'
},
)
)
responses.add(mock_download)

download_path = subsonic.media_retrieval.download(song["id"], tmp_path)

# Check if the file data has been unaltered
with open(tmp_path / default_song_filename, "r") as file:
with open(tmp_path / download_metadata.default_filename, "r") as file:
assert placeholder_data == file.read()

assert download_path == tmp_path / default_song_filename
assert download_path == tmp_path / download_metadata.default_filename


def test_hls(subsonic: Subsonic, song: dict[str, Any]) -> None:
Expand All @@ -81,113 +67,87 @@ def test_hls(subsonic: Subsonic, song: dict[str, Any]) -> None:

@responses.activate
def test_get_cover_art_with_a_given_filename(
subsonic: Subsonic,
mock_cover_art: Response,
tmp_path: Path,
output_cover_art_filename: str,
placeholder_data: str,
mock_download_file: MockDownload,
subsonic: Subsonic,
song: dict[str, Any],
cover_art_content_type: str,
cover_art_metadata: FileMetadata,
cover_art_size: int,
) -> None:
responses.add(
mock_download_file(
"getCoverArt",
{"id": song["coverArt"], "size": cover_art_size},
tmp_path,
cover_art_content_type,
)
)
responses.add(mock_cover_art)

download_path = subsonic.media_retrieval.get_cover_art(
song["coverArt"], tmp_path / output_cover_art_filename, cover_art_size
song["coverArt"], tmp_path / cover_art_metadata.output_filename, cover_art_size
)

# Check if the file data has been unaltered
with open(tmp_path / output_cover_art_filename, "r") as file:
with open(tmp_path / cover_art_metadata.output_filename, "r") as file:
assert placeholder_data == file.read()

assert download_path == tmp_path / output_cover_art_filename
assert download_path == tmp_path / cover_art_metadata.output_filename


@responses.activate
def test_get_cover_art_without_a_given_filename(
subsonic: Subsonic,
mock_cover_art: Response,
tmp_path: Path,
default_cover_art_filename: str,
placeholder_data: str,
mock_download_file: MockDownload,
subsonic: Subsonic,
song: dict[str, Any],
cover_art_content_type: str,
cover_art_size: int,
cover_art_metadata: FileMetadata,
) -> None:
responses.add(
mock_download_file(
"getCoverArt",
{"id": song["coverArt"], "size": cover_art_size},
tmp_path,
cover_art_content_type,
)
)
responses.add(mock_cover_art)

download_path = subsonic.media_retrieval.get_cover_art(
song["coverArt"], tmp_path, cover_art_size
)

# Check if the file data has been unaltered
with open(tmp_path / default_cover_art_filename, "r") as file:
with open(tmp_path / cover_art_metadata.default_filename, "r") as file:
assert placeholder_data == file.read()

assert download_path == tmp_path / default_cover_art_filename
assert download_path == tmp_path / cover_art_metadata.default_filename


@responses.activate
def test_get_avatar_with_a_given_filename(
subsonic: Subsonic,
mock_avatar: Response,
tmp_path: Path,
output_avatar_filename: str,
placeholder_data: str,
mock_download_file: MockDownload,
subsonic: Subsonic,
username: str,
avatar_content_type: str,
avatar_metadata: FileMetadata,
) -> None:
responses.add(
mock_download_file(
"getAvatar", {"username": username}, tmp_path, avatar_content_type
)
)
responses.add(mock_avatar)

download_path = subsonic.media_retrieval.get_avatar(
username, tmp_path / output_avatar_filename
username, tmp_path / avatar_metadata.output_filename
)

# Check if the file data has been unaltered
with open(tmp_path / output_avatar_filename, "r") as file:
with open(tmp_path / avatar_metadata.output_filename, "r") as file:
assert placeholder_data == file.read()

assert download_path == tmp_path / output_avatar_filename
assert download_path == tmp_path / avatar_metadata.output_filename


@responses.activate
def test_get_avatar_without_a_given_filename(
subsonic: Subsonic,
mock_avatar: Response,
tmp_path: Path,
default_avatar_filename: str,
placeholder_data: str,
mock_download_file: MockDownload,
subsonic: Subsonic,
username: str,
avatar_content_type: str,
avatar_metadata: FileMetadata,
) -> None:
responses.add(
mock_download_file(
"getAvatar", {"username": username}, tmp_path, avatar_content_type
)
)
responses.add(mock_avatar)

download_path = subsonic.media_retrieval.get_avatar(username, tmp_path)

# Check if the file data has been unaltered
with open(tmp_path / default_avatar_filename, "r") as file:
with open(tmp_path / avatar_metadata.default_filename, "r") as file:
assert placeholder_data == file.read()

assert download_path == tmp_path / default_avatar_filename
assert download_path == tmp_path / avatar_metadata.default_filename
Loading

0 comments on commit c466c1f

Please sign in to comment.