Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unit tests for artists endpoint #5

Merged
merged 1 commit into from
Nov 18, 2023
Merged
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pytunes analyzes [ID3](https://en.wikipedia.org/wiki/ID3) metadata of your MP3 f
```

- Please be patient. It might take a couple of minutes until deployment is complete.
- You should see a log line in your balenaCloud dashboard once pytunes has successfully started:
- You should see a log line in your balenaCloud dashboard once pytunes has started successfully:
```
INFO: Application startup complete.
```
Expand All @@ -51,8 +51,8 @@ pytunes analyzes [ID3](https://en.wikipedia.org/wiki/ID3) metadata of your MP3 f
scp -P 22222 track.mp3 root@<short-uuid>.local:/var/lib/docker/volumes/<APP ID>_pytunes-data/_data/
```
- Alternatively you can also use an SFTP client of your choice like [FileZilla](https://filezilla-project.org/).
- pytunes will automatically start processing audio files as soon as they have been received.
- You should see a log line in your balenaCloud dashboard once an audio file has been processed successfully:
- pytunes will automatically start processing MP3 files as soon as they have been received.
- You should see a log line in your balenaCloud dashboard once an MP3 file has been processed successfully:
```
21:01:35 default: Job OK (a9e5765b-f1f8-4dc6-8ecd-166a2e21f6d1)
```
Expand Down
11 changes: 7 additions & 4 deletions src/pytunes/routers/albums.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
def get_album_playlist(album_id: UUID, request: Request, db: Session = Depends(get_db)):
tracks = crud.get_album_tracks(db, album_id)
data = "\n".join(
[
"#EXTM3U",
"\n".join([track_to_playlist_item(request, track) for track in tracks]),
]
filter(
bool,
[
"#EXTM3U",
"\n".join([track_to_playlist_item(request, track) for track in tracks]),
],
)
)
return Response(content=data, media_type="audio/x-mpegurl")
20 changes: 13 additions & 7 deletions src/pytunes/routers/artists.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ def get_artist_playlist(
filtered_albums = filter(has_tracks, albums)
tracks = crud.get_artist_tracks_without_album(db, artist_id)
data = "\n".join(
[
"#EXTM3U",
"\n".join(
[album_to_playlist_item(request, album) for album in filtered_albums]
),
"\n".join([track_to_playlist_item(request, track) for track in tracks]),
]
filter(
bool,
[
"#EXTM3U",
"\n".join(
[
album_to_playlist_item(request, album)
for album in filtered_albums
]
),
"\n".join([track_to_playlist_item(request, track) for track in tracks]),
],
)
)
return Response(content=data, media_type="audio/x-mpegurl")
4 changes: 2 additions & 2 deletions tests/test_albums.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
@patch(
"pytunes.routers.albums.crud.get_album_tracks",
return_value=[
models.Track(id=uuid4(), name="foo"),
models.Track(id=uuid4(), name="bar"),
models.Track(id=uuid4(), name="track_name_1"),
models.Track(id=uuid4(), name="track_name_2"),
],
)
@patch("pytunes.routers.albums.track_to_playlist_item", return_value="playlist_item")
Expand Down
32 changes: 32 additions & 0 deletions tests/test_artists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from unittest.mock import patch
from uuid import uuid4

from pytunes import models
from pytunes.testing.setup import client


@patch(
"pytunes.routers.artists.crud.get_artist_albums",
return_value=[
models.Album(
name="album_name_1", tracks=[models.Track(id=uuid4(), name="track_name_1")]
),
models.Album(
name="album_name_2", tracks=[models.Track(id=uuid4(), name="track_name_1")]
),
],
)
@patch("pytunes.routers.artists.crud.get_artist_tracks_without_album", return_value=[])
@patch("pytunes.routers.artists.album_to_playlist_item", return_value="playlist_item")
def test_get_artist_playlist(
mock_get_artist_albums,
mock_get_artist_tracks_without_album,
mock_album_to_playlist_item,
):
artist_id = uuid4()

response = client.get(f"/artists/{artist_id}.m3u")

assert response.status_code == 200
assert response.text == "\n".join(["#EXTM3U", "playlist_item", "playlist_item"])
assert response.headers["content-type"] == "audio/x-mpegurl"
51 changes: 44 additions & 7 deletions tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
from unittest.mock import Mock
from uuid import UUID, uuid4

import pytest
from fastapi.datastructures import URL

from pytunes import models
from pytunes.util import sha1_to_uuid, track_to_playlist_item
from pytunes.util import (
album_to_playlist_item,
has_tracks,
sha1_to_uuid,
track_to_playlist_item,
)


def test_sha1_to_uuid():
Expand All @@ -14,18 +20,49 @@ def test_sha1_to_uuid():


def test_track_to_playlist_item():
track_id = uuid4()
track_name = "foo"
artist_name = "bar"
track = models.Track(
id=track_id, name=track_name, artists=[models.Artist(name=artist_name)]
id=uuid4(), name="track_name", artists=[models.Artist(name="artist_name")]
)
track_url = f"http://testserver/tracks/{track_id}.m3u8"
track_url = f"http://testserver/tracks/{track.id}.m3u8"
mock_request = Mock()
mock_request.url_for = Mock(return_value=URL(track_url))

playlist_item = track_to_playlist_item(mock_request, track)

assert playlist_item == "\n".join(
[f"#EXTINF:-1,{artist_name} - {track_name}", track_url]
["#EXTINF:-1,artist_name - track_name", track_url]
)


def test_album_to_playlist_item():
album = models.Album(name="album_name")
album_url = f"http://testserver/album/{album.id}.m3u"
mock_request = Mock()
mock_request.url_for = Mock(return_value=URL(album_url))

playlist_item = album_to_playlist_item(mock_request, album)

assert playlist_item == "\n".join(["#EXTINF:-1,album_name", album_url])


@pytest.mark.parametrize(
"artistOrAlbum,expected",
[
(models.Artist(name="artist_name"), False),
(
models.Artist(
name="artist_name", tracks=[models.Track(id=uuid4(), name="track_name")]
),
True,
),
(models.Album(name="album_name"), False),
(
models.Album(
name="album_name", tracks=[models.Track(id=uuid4(), name="track_name")]
),
True,
),
],
)
def test_has_tracks(artistOrAlbum, expected):
assert has_tracks(artistOrAlbum) == expected