def test_track_open_url_youtube(self): track_id = '123' self.assertEqual( TrackInfo(platform=Platform.YOUTUBE, name="ok", track_id=track_id).track_open_url(), "https://www.youtube.com/watch?v=%s" % track_id) self.assertEqual( TrackInfo(platform=Platform.SPOTIFY, name="ok", track_id=track_id).track_open_url(), "https://open.spotify.com/track/%s" % track_id)
def artists_for_search(self): self.assertEqual( TrackInfo(platform=Platform.YOUTUBE, name='ok', track_id='123', artists=['foo', 'bar']).artists_for_search(), "") self.assertEqual( TrackInfo(platform=Platform.SPOTIFY, name='ok', track_id='123', artists=['foo', 'bar']).artists_for_search(), "foo bar")
def test_description(self): self.assertEqual( TrackInfo(platform=Platform.SPOTIFY, name="ok", track_id='123', artists=["ok"]).description(), "") self.assertEqual( TrackInfo(platform=Platform.YOUTUBE, name="ok", track_id='123', raw_json=YOUTUBE_VIDEOS_LIST_SINGLE_RESPONSE['items'][0] ['snippet']).description(), "It's f****n' Maroon 5!")
def test_track_image_url(self): self.assertEqual( TrackInfo(platform=Platform.YOUTUBE, name="ok", track_id='123', raw_json=YOUTUBE_VIDEOS_LIST_SINGLE_RESPONSE['items'][0] ['snippet']).track_image_url(), "https://i.ytimg.com/vi/XPpTgCho5ZA/default.jpg") self.assertEqual( TrackInfo(platform=Platform.SPOTIFY, name="ok", track_id='123', raw_json=SPOTIFY_TRACK_RESP).track_image_url(), "https://i.scdn.co/image/6ecbb6e0db1a5093bc58169b87beb19d2947ebdd")
def test_channel_title(self): self.assertEqual( TrackInfo(platform=Platform.YOUTUBE, name="ok", track_id='123', raw_json=YOUTUBE_VIDEOS_LIST_SINGLE_RESPONSE['items'][0] ['snippet']).channel_title(), "Maroon5VEVO")
def search_and_add_to_playlists(origin, platform, channel): """ 1. Search for track info based on target 2. add to same platform playlists """ platform = Platform.from_string(platform) playlists = Playlist.query.filter_by(channel_id=channel, platform=platform).all() if not playlists: return True # see if we were passed a TrackInfo.__dict__ if 'platform' in origin: origin = TrackInfo(**origin) if isinstance(origin, TrackInfo): best_match = fuzzy_search_from_track_info(track_info=origin) else: best_match = fuzzy_search_from_string( track_name=origin.get('track_name'), artist=origin.get('artist'), platform=platform) if not best_match: msg_payload = SlackMessageFormatter.format_failed_search_results_message( origin=origin, target_platform=platform) msg_payload.update({'channel': channel}) SlackMessageFormatter.post_message(payload=msg_payload) return True successes, failures = add_track_to_playlists(track_info=best_match, playlists=playlists) # send message payload = SlackMessageFormatter.format_add_track_results_message( origin=origin, track_info=best_match, successes=successes, failures=failures) payload.update({'channel': channel}) SlackMessageFormatter.post_message(payload=payload) return True
def test_track_name_for_display(self): track_name = "This Love [%s] (%s)" % (BAD_WORDS[0], BAD_WORDS[1]) artists = ["Maroon 5", "Kabir"] self.assertEqual( TrackInfo(platform=Platform.SPOTIFY, name=track_name, track_id='123', artists=artists).track_name_for_display(), "%s - %s" % (track_name, ", ".join(artists)))
def test_sanitized_track_name(self): base_str = "Maroon 5 (YES) [123]" for word in BAD_WORDS: base_str += " %s" % word self.assertEqual( TrackInfo(platform=Platform.YOUTUBE, track_id='123', name=base_str).sanitized_track_name().strip(), 'Maroon 5'.strip())
def test_track_name_for_comparison(self): # should sanitize title and append artists without commas track_name = "This Love [%s] (%s)" % (BAD_WORDS[0], BAD_WORDS[1]) artists = ["Maroon 5", "Kabir"] self.assertEqual( TrackInfo(platform=Platform.SPOTIFY, name=track_name, track_id='123', artists=artists).track_name_for_comparison(), "%s %s" % (track_name, " ".join(artists)))
def test_artists_display_name(self): track_info_no_artist = TrackInfo(platform=Platform.YOUTUBE, name='ok', track_id='123') self.assertEqual(track_info_no_artist.artists_display_name(), '') track_info_list_of_artists = TrackInfo(platform=Platform.YOUTUBE, name='ok', track_id='123', artists=['foo', 'bar']) self.assertEqual(track_info_list_of_artists.artists_display_name(), "foo, bar") track_info_artist_string = TrackInfo(platform=Platform.YOUTUBE, name='ok', track_id='123', artists='foo bar') self.assertEqual(track_info_artist_string.artists_display_name(), 'foo bar')
def setUp(self): super(YoutubeServiceTestCase, self).setUp() self.service = YoutubeService(credentials={'ok': True}, client=FakeYoutubeClient()) self.playlist = Playlist(name='yt', channel_id='123', platform=Platform.YOUTUBE, platform_id='abc123', user_id=1) self.track_info = TrackInfo( name="Maroon 5 - This Love (Official Music Video)", platform=Platform.YOUTUBE, track_id="XPpTgCho5ZA")
def setUp(self): super(SpotifyServiceTestCase, self).setUp() self.fake_client = FakeSpotifyClient() self.service = SpotifyService(credentials={'ok': True}, client=self.fake_client) self.playlist = Playlist(name="Spot", platform=Platform.SPOTIFY, platform_id='abc123', user_id=1, channel_id='123') self.track_info = TrackInfo( name="This Love", artists=[a['name'] for a in SPOTIFY_TRACK_RESP['artists']], platform=Platform.SPOTIFY, track_id="aaa")
import copy import json from unittest.mock import patch from tests.base import DatabaseTestBase from src.constants import Platform from src.message_formatters import SlackMessageFormatter from src.models import Credential, Playlist, User from src.music_services import TrackInfo from src.tasks import add_link_to_playlists, search_and_add_to_playlists YT_TRACK_INFO = TrackInfo(name="Maroon 5 - This Love", platform=Platform.YOUTUBE, track_id='abc123') SP_TRACK_INFO = TrackInfo(name="This Love", artists="Maroon 5", track_id="def456", platform=Platform.SPOTIFY) class TaskTestBase(DatabaseTestBase): def setUp(self): super(TaskTestBase, self).setUp() self.user = User(name='tester', slack_id='abc123') self.user.is_service_user = True self.user.save() self.yt_creds = Credential(platform=Platform.YOUTUBE, credentials=json.dumps( {'access_token': True}),
def test_is_track_in_playlist_and_it_is_not(self): self.assertFalse( self.service.is_track_in_playlist(track_info=TrackInfo( name='nah', platform=Platform.YOUTUBE, track_id='nah'), playlist=self.playlist))