improve test coverage

This commit is contained in:
sigma67 2022-12-12 15:02:10 +01:00
commit 4594a748e3
No known key found for this signature in database
GPG key ID: 1C314227DF0103D0
3 changed files with 101 additions and 95 deletions

View file

@ -1,6 +1,8 @@
[auth] [auth]
brand_account = 101234229123420379537 brand_account = 101234229123420379537
brand_account_empty = 1123456629123420379537
headers = headers_auth_json_as_string headers = headers_auth_json_as_string
headers_empty = headers_account_with_empty_library_as_json_as_string
headers_file = ./headers_auth.json headers_file = ./headers_auth.json
headers_raw = raw_headers_pasted_from_browser headers_raw = raw_headers_pasted_from_browser

View file

@ -20,6 +20,8 @@ class TestYTMusic(unittest.TestCase):
cls.yt = YTMusic(requests_session=False) cls.yt = YTMusic(requests_session=False)
cls.yt_auth = YTMusic(config['auth']['headers_file']) cls.yt_auth = YTMusic(config['auth']['headers_file'])
cls.yt_brand = YTMusic(config['auth']['headers'], config['auth']['brand_account']) cls.yt_brand = YTMusic(config['auth']['headers'], config['auth']['brand_account'])
cls.yt_empty = YTMusic(config['auth']['headers_empty'],
config['auth']['brand_account_empty'])
def test_init(self): def test_init(self):
self.assertRaises(Exception, YTMusic, "{}") self.assertRaises(Exception, YTMusic, "{}")
@ -232,6 +234,9 @@ class TestYTMusic(unittest.TestCase):
playlists = self.yt_auth.get_library_playlists(None) playlists = self.yt_auth.get_library_playlists(None)
self.assertGreaterEqual(len(playlists), config.getint('limits', 'library_playlists')) self.assertGreaterEqual(len(playlists), config.getint('limits', 'library_playlists'))
playlists = self.yt_empty.get_library_playlists()
self.assertEqual(len(playlists), 0)
def test_get_library_songs(self): def test_get_library_songs(self):
self.assertRaises(Exception, self.yt_auth.get_library_songs, None, True) self.assertRaises(Exception, self.yt_auth.get_library_songs, None, True)
songs = self.yt_auth.get_library_songs(100) songs = self.yt_auth.get_library_songs(100)
@ -240,6 +245,8 @@ class TestYTMusic(unittest.TestCase):
self.assertGreaterEqual(len(songs), config.getint('limits', 'library_songs')) self.assertGreaterEqual(len(songs), config.getint('limits', 'library_songs'))
songs = self.yt_auth.get_library_songs(order='a_to_z') songs = self.yt_auth.get_library_songs(order='a_to_z')
self.assertGreaterEqual(len(songs), 25) self.assertGreaterEqual(len(songs), 25)
songs = self.yt_empty.get_library_songs()
self.assertEqual(len(songs), 0)
def test_get_library_albums(self): def test_get_library_albums(self):
albums = self.yt_auth.get_library_albums(100) albums = self.yt_auth.get_library_albums(100)
@ -252,6 +259,8 @@ class TestYTMusic(unittest.TestCase):
self.assertGreater(len(albums), 50) self.assertGreater(len(albums), 50)
albums = self.yt_brand.get_library_albums(100, order='recently_added') albums = self.yt_brand.get_library_albums(100, order='recently_added')
self.assertGreater(len(albums), 50) self.assertGreater(len(albums), 50)
albums = self.yt_empty.get_library_albums()
self.assertEqual(len(albums), 0)
def test_get_library_artists(self): def test_get_library_artists(self):
artists = self.yt_auth.get_library_artists(50) artists = self.yt_auth.get_library_artists(50)
@ -260,6 +269,8 @@ class TestYTMusic(unittest.TestCase):
self.assertGreater(len(artists), 40) self.assertGreater(len(artists), 40)
artists = self.yt_brand.get_library_artists(limit=None) artists = self.yt_brand.get_library_artists(limit=None)
self.assertGreater(len(artists), config.getint('limits', 'library_artists')) self.assertGreater(len(artists), config.getint('limits', 'library_artists'))
artists = self.yt_empty.get_library_artists()
self.assertEqual(len(artists), 0)
def test_get_library_subscriptions(self): def test_get_library_subscriptions(self):
artists = self.yt_brand.get_library_subscriptions(50) artists = self.yt_brand.get_library_subscriptions(50)
@ -268,10 +279,14 @@ class TestYTMusic(unittest.TestCase):
self.assertGreater(len(artists), 20) self.assertGreater(len(artists), 20)
artists = self.yt_brand.get_library_subscriptions(limit=None) artists = self.yt_brand.get_library_subscriptions(limit=None)
self.assertGreater(len(artists), config.getint('limits', 'library_subscriptions')) self.assertGreater(len(artists), config.getint('limits', 'library_subscriptions'))
artists = self.yt_empty.get_library_subscriptions()
self.assertEqual(len(artists), 0)
def test_get_liked_songs(self): def test_get_liked_songs(self):
songs = self.yt_brand.get_liked_songs(200) songs = self.yt_brand.get_liked_songs(200)
self.assertGreater(len(songs['tracks']), 100) self.assertGreater(len(songs['tracks']), 100)
songs = self.yt_empty.get_liked_songs()
self.assertEqual(songs['trackCount'], 0)
def test_get_history(self): def test_get_history(self):
songs = self.yt_auth.get_history() songs = self.yt_auth.get_history()
@ -383,12 +398,7 @@ class TestYTMusic(unittest.TestCase):
results = self.yt_auth.get_library_upload_songs(50, order='z_to_a') results = self.yt_auth.get_library_upload_songs(50, order='z_to_a')
self.assertGreater(len(results), 25) self.assertGreater(len(results), 25)
# songs = self.yt_auth.get_library_upload_songs(None) results = self.yt_empty.get_library_upload_songs(100)
# self.assertEqual(len(songs), 1000)
@unittest.skip("Must not have any uploaded songs to pass")
def test_get_library_upload_songs_empty(self):
results = self.yt_auth.get_library_upload_songs(100)
self.assertEqual(len(results), 0) self.assertEqual(len(results), 0)
def test_get_library_upload_albums(self): def test_get_library_upload_albums(self):
@ -398,9 +408,7 @@ class TestYTMusic(unittest.TestCase):
albums = self.yt_auth.get_library_upload_albums(None) albums = self.yt_auth.get_library_upload_albums(None)
self.assertGreaterEqual(len(albums), config.getint('limits', 'library_upload_albums')) self.assertGreaterEqual(len(albums), config.getint('limits', 'library_upload_albums'))
@unittest.skip("Must not have any uploaded albums to pass") results = self.yt_empty.get_library_upload_albums(100)
def test_get_library_upload_albums_empty(self):
results = self.yt_auth.get_library_upload_albums(100)
self.assertEqual(len(results), 0) self.assertEqual(len(results), 0)
def test_get_library_upload_artists(self): def test_get_library_upload_artists(self):
@ -410,9 +418,7 @@ class TestYTMusic(unittest.TestCase):
results = self.yt_auth.get_library_upload_artists(50, order='recently_added') results = self.yt_auth.get_library_upload_artists(50, order='recently_added')
self.assertGreaterEqual(len(results), 25) self.assertGreaterEqual(len(results), 25)
@unittest.skip("Must not have any uploaded artsts to pass") results = self.yt_empty.get_library_upload_artists(100)
def test_get_library_upload_artists_empty(self):
results = self.yt_auth.get_library_upload_artists(100)
self.assertEqual(len(results), 0) self.assertEqual(len(results), 0)
def test_upload_song(self): def test_upload_song(self):

View file

@ -12,89 +12,87 @@ def parse_playlist_items(results, menu_entries: List[List] = None):
continue continue
data = result[MRLIR] data = result[MRLIR]
try: videoId = setVideoId = None
videoId = setVideoId = None like = None
like = None feedback_tokens = None
feedback_tokens = None
# if the item has a menu, find its setVideoId # if the item has a menu, find its setVideoId
if 'menu' in data: if 'menu' in data:
for item in nav(data, MENU_ITEMS): for item in nav(data, MENU_ITEMS):
if 'menuServiceItemRenderer' in item: if 'menuServiceItemRenderer' in item:
menu_service = nav(item, MENU_SERVICE) menu_service = nav(item, MENU_SERVICE)
if 'playlistEditEndpoint' in menu_service: if 'playlistEditEndpoint' in menu_service:
setVideoId = menu_service['playlistEditEndpoint']['actions'][0][ setVideoId = menu_service['playlistEditEndpoint']['actions'][0][
'setVideoId'] 'setVideoId']
videoId = menu_service['playlistEditEndpoint']['actions'][0][ videoId = menu_service['playlistEditEndpoint']['actions'][0][
'removedVideoId'] 'removedVideoId']
if TOGGLE_MENU in item: if TOGGLE_MENU in item:
feedback_tokens = parse_song_menu_tokens(item) feedback_tokens = parse_song_menu_tokens(item)
# if item is not playable, the videoId was retrieved above # if item is not playable, the videoId was retrieved above
if nav(data, PLAY_BUTTON, none_if_absent=True) != None: if nav(data, PLAY_BUTTON, none_if_absent=True) is not None:
if 'playNavigationEndpoint' in nav(data, PLAY_BUTTON): if 'playNavigationEndpoint' in nav(data, PLAY_BUTTON):
videoId = nav(data, videoId = nav(data,
PLAY_BUTTON)['playNavigationEndpoint']['watchEndpoint']['videoId'] PLAY_BUTTON)['playNavigationEndpoint']['watchEndpoint']['videoId']
if 'menu' in data: if 'menu' in data:
like = nav(data, MENU_LIKE_STATUS, True) like = nav(data, MENU_LIKE_STATUS, True)
title = get_item_text(data, 0) title = get_item_text(data, 0)
if title == 'Song deleted': if title == 'Song deleted':
continue continue
artists = parse_song_artists(data, 1) artists = parse_song_artists(data, 1)
album = parse_song_album(data, 2) album = parse_song_album(data, 2)
duration = None duration = None
if 'fixedColumns' in data: if 'fixedColumns' in data:
if 'simpleText' in get_fixed_column_item(data, 0)['text']: if 'simpleText' in get_fixed_column_item(data, 0)['text']:
duration = get_fixed_column_item(data, 0)['text']['simpleText'] duration = get_fixed_column_item(data, 0)['text']['simpleText']
else: else:
duration = get_fixed_column_item(data, 0)['text']['runs'][0]['text'] duration = get_fixed_column_item(data, 0)['text']['runs'][0]['text']
thumbnails = None thumbnails = None
if 'thumbnail' in data: if 'thumbnail' in data:
thumbnails = nav(data, THUMBNAILS) thumbnails = nav(data, THUMBNAILS)
isAvailable = True isAvailable = True
if 'musicItemRendererDisplayPolicy' in data: if 'musicItemRendererDisplayPolicy' in data:
isAvailable = data[ isAvailable = data[
'musicItemRendererDisplayPolicy'] != 'MUSIC_ITEM_RENDERER_DISPLAY_POLICY_GREY_OUT' 'musicItemRendererDisplayPolicy'] != 'MUSIC_ITEM_RENDERER_DISPLAY_POLICY_GREY_OUT'
isExplicit = nav(data, BADGE_LABEL, True) is not None isExplicit = nav(data, BADGE_LABEL, True) is not None
videoType = nav(data, MENU_ITEMS + [0, 'menuNavigationItemRenderer', 'navigationEndpoint'] + NAVIGATION_VIDEO_TYPE, True) videoType = nav(
data, MENU_ITEMS + [0, 'menuNavigationItemRenderer', 'navigationEndpoint']
+ NAVIGATION_VIDEO_TYPE, True)
song = { song = {
'videoId': videoId, 'videoId': videoId,
'title': title, 'title': title,
'artists': artists, 'artists': artists,
'album': album, 'album': album,
'likeStatus': like, 'likeStatus': like,
'thumbnails': thumbnails, 'thumbnails': thumbnails,
'isAvailable': isAvailable, 'isAvailable': isAvailable,
'isExplicit': isExplicit, 'isExplicit': isExplicit,
'videoType': videoType 'videoType': videoType
} }
if duration: if duration:
song['duration'] = duration song['duration'] = duration
song['duration_seconds'] = parse_duration(duration) song['duration_seconds'] = parse_duration(duration)
if setVideoId: if setVideoId:
song['setVideoId'] = setVideoId song['setVideoId'] = setVideoId
if feedback_tokens: if feedback_tokens:
song['feedbackTokens'] = feedback_tokens song['feedbackTokens'] = feedback_tokens
if menu_entries: if menu_entries:
for menu_entry in menu_entries: for menu_entry in menu_entries:
song[menu_entry[-1]] = nav(data, MENU_ITEMS + menu_entry) song[menu_entry[-1]] = nav(data, MENU_ITEMS + menu_entry)
songs.append(song) songs.append(song)
except Exception as e:
print("Item " + str(count) + ": " + str(e))
return songs return songs