2018-05-22 19:10:48 +00:00
|
|
|
from funkwhale_api.music import serializers
|
|
|
|
|
|
|
|
|
|
|
|
def test_artist_album_serializer(factories, to_api_date):
|
2018-06-09 13:36:16 +00:00
|
|
|
track = factories["music.Track"]()
|
2018-05-22 19:10:48 +00:00
|
|
|
album = track.album
|
|
|
|
album = album.__class__.objects.with_tracks_count().get(pk=album.pk)
|
|
|
|
expected = {
|
2018-06-09 13:36:16 +00:00
|
|
|
"id": album.id,
|
|
|
|
"mbid": str(album.mbid),
|
|
|
|
"title": album.title,
|
|
|
|
"artist": album.artist.id,
|
|
|
|
"creation_date": to_api_date(album.creation_date),
|
|
|
|
"tracks_count": 1,
|
|
|
|
"cover": album.cover.url,
|
|
|
|
"release_date": to_api_date(album.release_date),
|
2018-05-22 19:10:48 +00:00
|
|
|
}
|
|
|
|
serializer = serializers.ArtistAlbumSerializer(album)
|
|
|
|
|
|
|
|
assert serializer.data == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_artist_with_albums_serializer(factories, to_api_date):
|
2018-06-09 13:36:16 +00:00
|
|
|
track = factories["music.Track"]()
|
2018-05-22 19:10:48 +00:00
|
|
|
artist = track.artist
|
|
|
|
artist = artist.__class__.objects.with_albums().get(pk=artist.pk)
|
|
|
|
album = list(artist.albums.all())[0]
|
|
|
|
|
|
|
|
expected = {
|
2018-06-09 13:36:16 +00:00
|
|
|
"id": artist.id,
|
|
|
|
"mbid": str(artist.mbid),
|
|
|
|
"name": artist.name,
|
|
|
|
"creation_date": to_api_date(artist.creation_date),
|
|
|
|
"albums": [serializers.ArtistAlbumSerializer(album).data],
|
2018-05-22 19:10:48 +00:00
|
|
|
}
|
|
|
|
serializer = serializers.ArtistWithAlbumsSerializer(artist)
|
|
|
|
assert serializer.data == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_album_track_serializer(factories, to_api_date):
|
2018-06-09 13:36:16 +00:00
|
|
|
tf = factories["music.TrackFile"]()
|
2018-05-22 19:10:48 +00:00
|
|
|
track = tf.track
|
|
|
|
|
|
|
|
expected = {
|
2018-06-09 13:36:16 +00:00
|
|
|
"id": track.id,
|
|
|
|
"artist": track.artist.id,
|
|
|
|
"album": track.album.id,
|
|
|
|
"mbid": str(track.mbid),
|
|
|
|
"title": track.title,
|
|
|
|
"position": track.position,
|
|
|
|
"creation_date": to_api_date(track.creation_date),
|
|
|
|
"files": [serializers.TrackFileSerializer(tf).data],
|
2018-05-22 19:10:48 +00:00
|
|
|
}
|
|
|
|
serializer = serializers.AlbumTrackSerializer(track)
|
|
|
|
assert serializer.data == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_track_file_serializer(factories, to_api_date):
|
2018-06-09 13:36:16 +00:00
|
|
|
tf = factories["music.TrackFile"]()
|
2018-05-22 19:10:48 +00:00
|
|
|
|
|
|
|
expected = {
|
2018-06-09 13:36:16 +00:00
|
|
|
"id": tf.id,
|
|
|
|
"path": tf.path,
|
|
|
|
"source": tf.source,
|
|
|
|
"filename": tf.filename,
|
|
|
|
"mimetype": tf.mimetype,
|
|
|
|
"track": tf.track.pk,
|
|
|
|
"duration": tf.duration,
|
|
|
|
"mimetype": tf.mimetype,
|
|
|
|
"bitrate": tf.bitrate,
|
|
|
|
"size": tf.size,
|
2018-05-22 19:10:48 +00:00
|
|
|
}
|
|
|
|
serializer = serializers.TrackFileSerializer(tf)
|
|
|
|
assert serializer.data == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_album_serializer(factories, to_api_date):
|
2018-06-09 13:36:16 +00:00
|
|
|
track1 = factories["music.Track"](position=2)
|
|
|
|
track2 = factories["music.Track"](position=1, album=track1.album)
|
2018-05-22 19:10:48 +00:00
|
|
|
album = track1.album
|
|
|
|
expected = {
|
2018-06-09 13:36:16 +00:00
|
|
|
"id": album.id,
|
|
|
|
"mbid": str(album.mbid),
|
|
|
|
"title": album.title,
|
|
|
|
"artist": serializers.ArtistSimpleSerializer(album.artist).data,
|
|
|
|
"creation_date": to_api_date(album.creation_date),
|
|
|
|
"cover": album.cover.url,
|
|
|
|
"release_date": to_api_date(album.release_date),
|
|
|
|
"tracks": serializers.AlbumTrackSerializer([track2, track1], many=True).data,
|
2018-05-22 19:10:48 +00:00
|
|
|
}
|
|
|
|
serializer = serializers.AlbumSerializer(album)
|
|
|
|
|
|
|
|
assert serializer.data == expected
|
|
|
|
|
|
|
|
|
|
|
|
def test_track_serializer(factories, to_api_date):
|
2018-06-09 13:36:16 +00:00
|
|
|
tf = factories["music.TrackFile"]()
|
2018-05-22 19:10:48 +00:00
|
|
|
track = tf.track
|
|
|
|
|
|
|
|
expected = {
|
2018-06-09 13:36:16 +00:00
|
|
|
"id": track.id,
|
|
|
|
"artist": serializers.ArtistSimpleSerializer(track.artist).data,
|
|
|
|
"album": serializers.TrackAlbumSerializer(track.album).data,
|
|
|
|
"mbid": str(track.mbid),
|
|
|
|
"title": track.title,
|
|
|
|
"position": track.position,
|
|
|
|
"creation_date": to_api_date(track.creation_date),
|
|
|
|
"lyrics": track.get_lyrics_url(),
|
|
|
|
"files": [serializers.TrackFileSerializer(tf).data],
|
2018-05-22 19:10:48 +00:00
|
|
|
}
|
|
|
|
serializer = serializers.TrackSerializer(track)
|
|
|
|
assert serializer.data == expected
|