def test_best_match_good_results(self): # set up some bad results target = "Sure Why not" results = copy.deepcopy(YOTUBE_SEARCH_LIST_RESPONSE) items = results['items'] bad_items = [] for item in items[:-1]: # make sure all but one are bad current_name = target while fuzz.token_set_ratio( target, current_name) >= YOUTUBE_TOKEN_SET_THRESHHOLD: current_name += "a" item['snippet']['title'] = current_name bad_items.append(item) # make one item that's good good_item = items[-1] # don't make it perfect, for some reason pretty_good_title = target + 'a' good_item['snippet']['title'] = pretty_good_title bad_items.append(good_item) fake_client = FakeYoutubeClient() service = YoutubeService(credentials={'ok': True}, client=fake_client) match = service.best_match(target_string=target, search_results=bad_items) self.assertIsInstance(match, TrackInfo) self.assertTrue(match.name, pretty_good_title)
def test_create_playlist_no_channel(self): fake_client = FakeYoutubeClient( expected_responses={'channels_list': { 'items': [] }}) service = YoutubeService(credentials=True, client=fake_client) self.assertEqual(service.create_playlist(playlist_name='foobar'), (False, 'No channels'))
def test_track_info_no_results(self): service = YoutubeService( credentials={'ok': True}, client=FakeYoutubeClient( expected_responses={'videos_list': { 'items': [] }})) self.assertFalse( service.get_track_info_from_link(link=YOUTUBE_LINK_MOBILE))
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 test_create_playlist_dupe_playlist(self): fake_client = FakeYoutubeClient( expected_responses={ 'playlists_list': { 'items': [{ 'snippet': { 'title': self.playlist.name } }] } }) service = YoutubeService(credentials=True, client=fake_client) self.assertTrue( service.create_playlist(playlist_name=self.playlist.name)) self.assertEqual(fake_client.playlist_item_insert_calls, [])
def test_add_track_to_playlist_fails_error(self): def raise_exception(): raise Exception('nope') fake_client = FakeYoutubeClient( expected_responses={ 'playlistItems_list': { 'items': [] }, 'playlistItem_insert': raise_exception }) service = YoutubeService(credentials={'ok': True}, client=fake_client) self.assertEqual( service.add_track_to_playlist(track_info=self.track_info, playlist=self.playlist), (False, 'nope'))
def test_best_match_no_results_over_fuzz_limit(self): # set up some bad results target = "Sure Why not" results = copy.deepcopy(YOTUBE_SEARCH_LIST_RESPONSE) items = results['items'] bad_items = [] for item in items: current_name = item['snippet']['title'] while fuzz.token_set_ratio( target, current_name) >= YOUTUBE_TOKEN_SET_THRESHHOLD: current_name += "a" item['snippet']['title'] = current_name bad_items.append(item) fake_client = FakeYoutubeClient() service = YoutubeService(credentials={'ok': True}, client=fake_client) self.assertIsNone( service.best_match(target_string=target, search_results=bad_items))
def test_add_track_to_playlist_succeeds(self): fake_client = FakeYoutubeClient( expected_responses={'playlistItems_list': { 'items': [] }}) service = YoutubeService(credentials={'ok': True}, client=fake_client) self.assertEqual( service.add_track_to_playlist(track_info=self.track_info, playlist=self.playlist), (True, None)) self.assertEqual( fake_client.playlist_item_insert_calls[0], { 'kind': 'youtube#playlistItem', 'snippet': { 'playlistId': self.playlist.platform_id, 'resourceId': { 'kind': 'youtube#video', 'videoId': self.track_info.track_id, } } })
class YoutubeServiceTestCase(unittest.TestCase): 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 test_get_track_info_from_link_non_youtube_link(self): self.assertFalse( self.service.get_track_info_from_link(link=SPOTIFY_LINK_WEB)) def test_get_track_info_from_link_web(self): ti = self.service.get_track_info_from_link(link=YOUTUBE_LINK_WEB) self.assertTrue(isinstance(ti, TrackInfo)) # cheating here a little bit because I know that the default behavior of this fake client # and wht it will return self.assertEqual(ti.name, "Maroon 5 - This Love (Official Music Video)") def test_get_track_info_from_link_mobile(self): ti = self.service.get_track_info_from_link(link=YOUTUBE_LINK_MOBILE) self.assertTrue(isinstance(ti, TrackInfo)) # cheating here a little bit because I know that the default behavior of this fake client # and wht it will return self.assertEqual(ti.name, "Maroon 5 - This Love (Official Music Video)") def test_track_info_no_results(self): service = YoutubeService( credentials={'ok': True}, client=FakeYoutubeClient( expected_responses={'videos_list': { 'items': [] }})) self.assertFalse( service.get_track_info_from_link(link=YOUTUBE_LINK_MOBILE)) def test_get_track_ids_in_playlist(self): track_ids = self.service.get_track_ids_in_playlist( playlist=self.playlist) #some cheating here because I know what the ids will be from the json fake expected_track_ids = { self.track_info.track_id, } self.assertEqual(track_ids, expected_track_ids) def test_is_track_in_playlist_and_it_is(self): self.assertTrue( self.service.is_track_in_playlist(track_info=self.track_info, playlist=self.playlist)) 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)) def test_add_track_to_playlist_fails_error(self): def raise_exception(): raise Exception('nope') fake_client = FakeYoutubeClient( expected_responses={ 'playlistItems_list': { 'items': [] }, 'playlistItem_insert': raise_exception }) service = YoutubeService(credentials={'ok': True}, client=fake_client) self.assertEqual( service.add_track_to_playlist(track_info=self.track_info, playlist=self.playlist), (False, 'nope')) def test_add_track_to_playlist_fails_duplicate(self): self.assertEqual( self.service.add_track_to_playlist(track_info=self.track_info, playlist=self.playlist), (False, DUPLICATE_TRACK)) def test_add_track_to_playlist_succeeds(self): fake_client = FakeYoutubeClient( expected_responses={'playlistItems_list': { 'items': [] }}) service = YoutubeService(credentials={'ok': True}, client=fake_client) self.assertEqual( service.add_track_to_playlist(track_info=self.track_info, playlist=self.playlist), (True, None)) self.assertEqual( fake_client.playlist_item_insert_calls[0], { 'kind': 'youtube#playlistItem', 'snippet': { 'playlistId': self.playlist.platform_id, 'resourceId': { 'kind': 'youtube#video', 'videoId': self.track_info.track_id, } } }) def test_best_match_no_results_over_fuzz_limit(self): # set up some bad results target = "Sure Why not" results = copy.deepcopy(YOTUBE_SEARCH_LIST_RESPONSE) items = results['items'] bad_items = [] for item in items: current_name = item['snippet']['title'] while fuzz.token_set_ratio( target, current_name) >= YOUTUBE_TOKEN_SET_THRESHHOLD: current_name += "a" item['snippet']['title'] = current_name bad_items.append(item) fake_client = FakeYoutubeClient() service = YoutubeService(credentials={'ok': True}, client=fake_client) self.assertIsNone( service.best_match(target_string=target, search_results=bad_items)) def test_best_match_good_results(self): # set up some bad results target = "Sure Why not" results = copy.deepcopy(YOTUBE_SEARCH_LIST_RESPONSE) items = results['items'] bad_items = [] for item in items[:-1]: # make sure all but one are bad current_name = target while fuzz.token_set_ratio( target, current_name) >= YOUTUBE_TOKEN_SET_THRESHHOLD: current_name += "a" item['snippet']['title'] = current_name bad_items.append(item) # make one item that's good good_item = items[-1] # don't make it perfect, for some reason pretty_good_title = target + 'a' good_item['snippet']['title'] = pretty_good_title bad_items.append(good_item) fake_client = FakeYoutubeClient() service = YoutubeService(credentials={'ok': True}, client=fake_client) match = service.best_match(target_string=target, search_results=bad_items) self.assertIsInstance(match, TrackInfo) self.assertTrue(match.name, pretty_good_title) def test_list_playlists(self): self.assertEqual( [p['snippet']['title'] for p in self.service.list_playlists()], ['Playlist1', 'Playlist2']) def test_create_playlist_no_channel(self): fake_client = FakeYoutubeClient( expected_responses={'channels_list': { 'items': [] }}) service = YoutubeService(credentials=True, client=fake_client) self.assertEqual(service.create_playlist(playlist_name='foobar'), (False, 'No channels')) def test_create_playlist_dupe_playlist(self): fake_client = FakeYoutubeClient( expected_responses={ 'playlists_list': { 'items': [{ 'snippet': { 'title': self.playlist.name } }] } }) service = YoutubeService(credentials=True, client=fake_client) self.assertTrue( service.create_playlist(playlist_name=self.playlist.name)) self.assertEqual(fake_client.playlist_item_insert_calls, []) def test_create_playlist(self): title = 'sure thing' res = self.service.create_playlist(playlist_name=title) expected_res = copy.deepcopy(YOUTUBE_PLAYLIST_INSERT_RESPONSE) expected_res['snippet']['title'] = title expected_res['snippet']['localized']['title'] = title self.assertEqual((True, expected_res), res) self.assertTrue(self.service.client.playlist_insert_calls, [title])