funkwhale/api/tests/music/test_music.py

161 wiersze
5.8 KiB
Python
Czysty Zwykły widok Historia

2018-06-10 08:55:16 +00:00
import datetime
2018-09-22 12:29:30 +00:00
from funkwhale_api.federation import utils as federation_utils
from funkwhale_api.music import models
def test_can_create_artist_from_api(artists, mocker, db):
mocker.patch(
2018-06-09 13:36:16 +00:00
"musicbrainzngs.search_artists",
return_value=artists["search"]["adhesive_wombat"],
)
artist = models.Artist.create_from_api(query="Adhesive wombat")
2018-06-09 13:36:16 +00:00
data = models.Artist.api.search(query="Adhesive wombat")["artist-list"][0]
2018-06-09 13:36:16 +00:00
assert int(data["ext:score"]), 100
assert data["id"], "62c3befb-6366-4585-b256-809472333801"
assert artist.mbid, data["id"]
assert artist.name, "Adhesive Wombat"
2018-09-22 12:29:30 +00:00
assert artist.fid == federation_utils.full_url(
"/federation/music/artists/{}".format(artist.uuid)
)
def test_can_create_album_from_api(artists, albums, mocker, db):
mocker.patch(
2018-06-09 13:36:16 +00:00
"funkwhale_api.musicbrainz.api.releases.search",
return_value=albums["search"]["hypnotize"],
)
mocker.patch(
2018-06-09 13:36:16 +00:00
"funkwhale_api.musicbrainz.api.artists.get", return_value=artists["get"]["soad"]
)
album = models.Album.create_from_api(
query="Hypnotize", artist="system of a down", type="album"
)
data = models.Album.api.search(
query="Hypnotize", artist="system of a down", type="album"
)["release-list"][0]
assert album.mbid, data["id"]
assert album.title, "Hypnotize"
assert album.release_date, datetime.date(2005, 1, 1)
2018-06-09 13:36:16 +00:00
assert album.artist.name, "System of a Down"
assert album.artist.mbid, data["artist-credit"][0]["artist"]["id"]
2018-09-22 12:29:30 +00:00
assert album.fid == federation_utils.full_url(
"/federation/music/albums/{}".format(album.uuid)
)
def test_can_create_track_from_api(artists, albums, tracks, mocker, db):
mocker.patch(
2018-06-09 13:36:16 +00:00
"funkwhale_api.musicbrainz.api.artists.get",
return_value=artists["get"]["adhesive_wombat"],
)
mocker.patch(
2018-06-09 13:36:16 +00:00
"funkwhale_api.musicbrainz.api.releases.get",
return_value=albums["get"]["marsupial"],
)
mocker.patch(
2018-06-09 13:36:16 +00:00
"funkwhale_api.musicbrainz.api.recordings.search",
return_value=tracks["search"]["8bitadventures"],
)
track = models.Track.create_from_api(query="8-bit adventure")
2018-06-09 13:36:16 +00:00
data = models.Track.api.search(query="8-bit adventure")["recording-list"][0]
assert int(data["ext:score"]) == 100
assert data["id"] == "9968a9d6-8d92-4051-8f76-674e157b6eed"
assert track.mbid == data["id"]
assert track.artist.pk is not None
2018-06-09 13:36:16 +00:00
assert str(track.artist.mbid) == "62c3befb-6366-4585-b256-809472333801"
assert track.artist.name == "Adhesive Wombat"
assert str(track.album.mbid) == "a50d2a81-2a50-484d-9cb4-b9f6833f583e"
assert track.album.title == "Marsupial Madness"
2018-09-22 12:29:30 +00:00
assert track.fid == federation_utils.full_url(
"/federation/music/tracks/{}".format(track.uuid)
)
def test_can_create_track_from_api_with_corresponding_tags(
2018-06-09 13:36:16 +00:00
artists, albums, tracks, mocker, db
):
mocker.patch(
2018-06-09 13:36:16 +00:00
"funkwhale_api.musicbrainz.api.artists.get",
return_value=artists["get"]["adhesive_wombat"],
)
mocker.patch(
2018-06-09 13:36:16 +00:00
"funkwhale_api.musicbrainz.api.releases.get",
return_value=albums["get"]["marsupial"],
)
mocker.patch(
2018-06-09 13:36:16 +00:00
"funkwhale_api.musicbrainz.api.recordings.get",
return_value=tracks["get"]["8bitadventures"],
)
track = models.Track.create_from_api(id="9968a9d6-8d92-4051-8f76-674e157b6eed")
expected_tags = ["techno", "good-music"]
track_tags = [tag.slug for tag in track.tags.all()]
for tag in expected_tags:
assert tag in track_tags
2018-06-09 13:36:16 +00:00
def test_can_get_or_create_track_from_api(artists, albums, tracks, mocker, db):
mocker.patch(
2018-06-09 13:36:16 +00:00
"funkwhale_api.musicbrainz.api.artists.get",
return_value=artists["get"]["adhesive_wombat"],
)
mocker.patch(
2018-06-09 13:36:16 +00:00
"funkwhale_api.musicbrainz.api.releases.get",
return_value=albums["get"]["marsupial"],
)
mocker.patch(
2018-06-09 13:36:16 +00:00
"funkwhale_api.musicbrainz.api.recordings.search",
return_value=tracks["search"]["8bitadventures"],
)
track = models.Track.create_from_api(query="8-bit adventure")
2018-06-09 13:36:16 +00:00
data = models.Track.api.search(query="8-bit adventure")["recording-list"][0]
assert int(data["ext:score"]) == 100
assert data["id"] == "9968a9d6-8d92-4051-8f76-674e157b6eed"
assert track.mbid == data["id"]
assert track.artist.pk is not None
2018-06-09 13:36:16 +00:00
assert str(track.artist.mbid) == "62c3befb-6366-4585-b256-809472333801"
assert track.artist.name == "Adhesive Wombat"
2018-06-09 13:36:16 +00:00
track2, created = models.Track.get_or_create_from_api(mbid=data["id"])
assert not created
assert track == track2
def test_album_tags_deduced_from_tracks_tags(factories, django_assert_num_queries):
2018-06-09 13:36:16 +00:00
tag = factories["taggit.Tag"]()
album = factories["music.Album"]()
factories["music.Track"].create_batch(5, album=album, tags=[tag])
2018-06-09 13:36:16 +00:00
album = models.Album.objects.prefetch_related("tracks__tags").get(pk=album.pk)
with django_assert_num_queries(0):
assert tag in album.tags
def test_artist_tags_deduced_from_album_tags(factories, django_assert_num_queries):
2018-06-09 13:36:16 +00:00
tag = factories["taggit.Tag"]()
album = factories["music.Album"]()
artist = album.artist
factories["music.Track"].create_batch(5, album=album, tags=[tag])
2018-06-09 13:36:16 +00:00
artist = models.Artist.objects.prefetch_related("albums__tracks__tags").get(
pk=artist.pk
)
with django_assert_num_queries(0):
assert tag in artist.tags
def test_can_download_image_file_for_album(binary_cover, mocker, factories):
mocker.patch(
2018-06-09 13:36:16 +00:00
"funkwhale_api.musicbrainz.api.images.get_front", return_value=binary_cover
)
# client._api.get_image_front('55ea4f82-b42b-423e-a0e5-290ccdf443ed')
2018-06-09 13:36:16 +00:00
album = factories["music.Album"](mbid="55ea4f82-b42b-423e-a0e5-290ccdf443ed")
album.get_image()
album.save()
assert album.cover.file.read() == binary_cover