def _test_question_top_track_not_enough_choices(self, time_range): """ question_top_track() should return None if there are not enough tracks to fill the choices. This method is called by the above 3 to test with different time ranges. """ u = UserData(self.session) artists = create_artists(3) json_add_field(artists, 'name', ['Cash', 'Ben', 'Cassius']) albums = create_albums(3) json_add_to_field(albums, 'images', create_image()) u._top_tracks[time_range] = create_albums(3) json_add_to_field(u._top_tracks[time_range], 'artists', artists, arr=True) json_add_field(u._top_tracks[time_range], 'album', albums[0]) quiz = Quiz.objects.create(user_id='cassius') q = question_top_track(quiz, u, time_range) self.assertIsNone(q)
def test_question_saved_tracks_no_valid_incorrect_choices(self): """ question_saved_tracks() should return None if there are not enough tracks from "top_tracks" to fill incorrect choices. """ u = UserData(self.session) artists = create_artists(3) json_add_field(artists, 'name', ['Cassius', 'Benjamin', 'James']) albums = create_albums(1) json_add_to_field(albums, 'images', create_image()) tracks = create_tracks(3) json_add_name(tracks, 'Country Track ') json_add_to_field(tracks, 'artists', artists, arr=True) json_add_field(tracks, 'album', albums[0]) u._saved_tracks = [tracks[0]] u._music_taste = tracks[1:] quiz = Quiz.objects.create(user_id='cassius') q = question_saved_tracks(quiz, u) self.assertIsNone(q)
def test_get_music_taste_exists(self): """ If _music_taste is not None, music_taste() should return it. """ u = UserData(None) test_data = ['test', 'hello'] u._music_taste = test_data self.assertEqual(u.music_taste(), test_data)
def test_get_saved_albums_exists(self): """ If _saved_albums is not None, saved_albums() should return it. """ u = UserData(None) test_data = ['hello', 'goodbye'] u._saved_albums = test_data self.assertEqual(u.saved_albums(), test_data)
def test_get_playlists_exists(self): """ If _playlists is not None, playlists() should return it. """ u = UserData(None) test_data = ['test', 'hello'] u._playlists = test_data self.assertEqual(u.playlists(), test_data)
def test_get_followed_artists_exists(self): """ If _followed_artists is not None, followed_artists() should return it. """ u = UserData(None) test_data = ['hello', 'goodbye'] u._followed_artists = test_data self.assertEqual(u.followed_artists(), test_data)
def test_get_top_genres_shortterm_exists(self): """ If _top_genres['short_term'] is not None, top_genres('short_term') should return it. """ u = UserData(None) test_data = [['hello', 'there'], ['goodbye', 'now']] u._top_genres['short_term'] = test_data self.assertEqual(u.top_genres('short_term'), test_data)
def test_get_top_tracks_shortterm_exists(self): """ If _top_tracks['short_term'] is not None, top_tracks('short_term') should return it. """ u = UserData(None) test_data = ['hello', 'goodbye'] u._top_tracks['short_term'] = test_data self.assertEqual(u.top_tracks('short_term'), test_data)
def test_get_top_artists_mediumterm_exists(self): """ If _top_artists['medium_term'] is not None, top_artists('medium_term') should return it. """ u = UserData(None) test_data = ['hello', 'goodbye'] u._top_artists['medium_term'] = test_data self.assertEqual(u.top_artists('medium_term'), test_data)
def test_get_recently_played_exists(self): """ If _recently_played is not None, recently_played() should return it. """ u = UserData(None) test_data = ['test', 'hello'] u._recently_played = test_data self.assertEqual(u.recently_played(), test_data)
def test_question_saved_tracks(self): """ question_saved_tracks() should return a question about the user's saved tracks. """ u = UserData(self.session) artists = create_artists(11) json_add_field(artists, 'name', [ 'Cash', 'Ben', 'Cassius', 'Benjamin', 'James', 'Jim', 'John', 'Lucy', 'Lewis', 'Lucifer', 'Lewd' ], arr=True) albums = create_albums(1) json_add_to_field(albums, 'images', create_image()) u._saved_tracks = create_tracks(7) json_add_name(u._saved_tracks, 'Country Track ') json_add_to_field(u._saved_tracks, 'artists', artists[0:7], arr=True) json_add_field(u._saved_tracks, 'album', albums[0]) u._music_taste = create_tracks(4, id=7) json_add_name(u._music_taste, 'Rock Track ') json_add_to_field(u._music_taste, 'artists', artists[7:11], arr=True) json_add_field(u._music_taste, 'album', albums[0]) quiz = Quiz.objects.create(user_id='cassius') q = question_saved_tracks(quiz, u) self.assertEqual(q.choices.count(), 4) self.assertGreater(q.answers().count(), 0) self.assertLessEqual(q.answers().count(), 4) self.assertEqual(q.incorrect_answers().count(), 4 - q.answers().count()) for c in q.answers(): title = c.primary_text artist = c.secondary_text found = False for t in u._saved_tracks: if t['name'] == title and t['artists'][0]['name'] == artist: found = True self.assertTrue(found) self.assertEqual(c.image_url, '200url') for c in q.incorrect_answers(): title = c.primary_text artist = c.secondary_text found = False for t in u._music_taste: if t['name'] == title and t['artists'][0]['name'] == artist: found = True self.assertTrue(found) self.assertEqual(c.image_url, '200url')
def test_get_personal_data_exists(self): """ If _personal_data is not None, personal_data() should return it. """ u = UserData(None) test_data = { 'test': 'hello' } u._personal_data = test_data self.assertEqual(u.personal_data(), test_data)
def test_get_music_taste_with_audio_features_exists(self): """ If _music_taste has audio features in its data, music_taste_with_audio_features() should return it. """ u = UserData(None) test_data = [{'id': 5, 'energy': 0}, {'id': 2, 'energy': 5}] u._music_taste = test_data self.assertEqual(u.music_taste_with_audio_features(), test_data)
def test_get_playlists_detailed_exists(self): """ If _playlists has detailed playlist data, playlists_detailed() should return it. """ u = UserData(None) test_data = [{'followers': 0}, {'followers': 5}] u._playlists = test_data self.assertEqual(u.playlists_detailed(), test_data)
def test_get_playlist_with_tracks_exists(self): """ If the playlist with the given ID exists in _playlists and it already has a list of tracks, get_playlist_with_tracks() should return that playlist data. """ u = UserData(None) test_data = [{'id': '0', 'tracks': {'items': []}}] u._playlists = test_data self.assertEqual(u.get_playlist_with_tracks('0'), test_data[0])
def test_compile_personal_data(self): """ _compile_personal_data() should request data about the user's personal data from Spotify and store it in the _personal_data variable. """ u = UserData(self.session) data = u.personal_data() self.assertIsNotNone(data.get('followers')) self.assertIsNotNone(data.get('display_name'))
def test_compile_saved_albums(self): """ _compile_saved_albums() should request data about the user's saved albums from Spotify and store it in the _saved_albums variable. """ u = UserData(self.session) data = u.saved_albums() self.assertGreaterEqual(len(data), 5) for a in data: self.assertEqual(a['type'], 'album')
def test_compile_top_artists_shortterm(self): """ _compile_top_tracks('short_term') should request data about the user's top tracks from Spotify and store it in the _top_tracks['short_term'] variable. """ u = UserData(self.session) data = u.top_artists('short_term') self.assertGreaterEqual(len(data), 10) for a in data: self.assertEqual(a['type'], 'artist')
def test_compile_top_artists_longterm(self): """ _compile_top_artists('long_term') should request data about the user's top artists from Spotify and store it in the _top_artists['long_term'] variable. """ u = UserData(self.session) data = u.top_artists('long_term') self.assertEqual(len(data), 50) for a in data: self.assertEqual(a['type'], 'artist')
def test_compile_music_taste(self): """ _compile_music_taste() should request compile data about the user's music taste from Spotify and store it in the _music_taste variable. """ u = UserData(self.session) data = u.music_taste() self.assertGreaterEqual(len(data), 50) for t in data: self.assertEqual(t['type'], 'track')
def test_compile_recently_played(self): """ _compile_recently_played() should request data about the user's recently played tracks from Spotify and store it in the _saved_albums variable. """ u = UserData(self.session) data = u.recently_played() self.assertEqual(len(data), 50) for t in data: self.assertEqual(t['type'], 'track')
def test_compile_followed_artists(self): """ _compile_followed_artists() should request data about the user's followed artists from Spotify and store it in the _followed_artists variable. """ u = UserData(self.session) data = u.followed_artists() self.assertGreaterEqual(len(data), 2) for a in data: self.assertEqual(a['type'], 'artist')
def test_compile_top_tracks_mediumterm(self): """ _compile_top_tracks('medium_term') should request data about the user's top tracks from Spotify and store it in the _top_tracks['medium_term'] variable. """ u = UserData(self.session) data = u.top_tracks('medium_term') self.assertEqual(len(data), 50) for t in data: self.assertEqual(t['type'], 'track')
def test_compile_playlists(self): """ _compile_playlists() should request compile data about the user's playlists from Spotify and store it in the _playlists variable. """ u = UserData(self.session) data = u.playlists() self.assertGreaterEqual(len(data), 5) for p in data: self.assertEqual(p['type'], 'playlist')
def _test_question_top_track(self, time_range): """ question_top_track() should return a question about the user's top track. This function is the test that is run by the three above methods, with different time_ranges. """ u = UserData(self.session) artists = create_artists(7) json_add_field( artists, 'name', ['Cash', 'Ben', 'Cassius', 'Benjamin', 'James', 'Jim', 'John'], arr=True) albums = create_albums(1) json_add_to_field(albums, 'images', create_image()) u._top_tracks[time_range] = create_tracks(7) json_add_name(u._top_tracks[time_range], 'Country Track ') json_add_to_field(u._top_tracks[time_range], 'artists', artists, arr=True) json_add_field(u._top_tracks[time_range], 'album', albums[0]) quiz = Quiz.objects.create(user_id='cassius') q = question_top_track(quiz, u, time_range) self.assertEqual(q.choices.count(), 4) self.assertEqual(q.answers().count(), 1) self.assertEqual(q.incorrect_answers().count(), 3) c = q.answers()[0] self.assertEqual(c.primary_text, 'Country Track 0') self.assertEqual(c.secondary_text, 'Cash') for c in q.choices.all(): self.assertEqual(c.image_url, '200url') for c in q.incorrect_answers(): title = c.primary_text artist = c.secondary_text self.assertNotEqual(title, 'Country Track 0') self.assertNotEqual(artist, 'Cash') found = False for t in u._top_tracks[time_range]: if t['name'] == title and t['artists'][0]['name'] == artist: found = True self.assertTrue(found)
def test_compile_playlist_details(self): """ compile_playlist_details() should request detailed data about the user's playlists from Spotify and store it in the _playlists variable. """ u = UserData(self.session) data = u.playlists_detailed() self.assertGreaterEqual(len(data), 5) for p in data: self.assertEqual(p['type'], 'playlist') self.assertIsNotNone(p.get('followers'))
def test_compile_top_genres_shortterm(self): """ _compile_top_genres('short_term') should request data about the user's top genres from Spotify and store it in the _top_genres['short_term'] variable. """ u = UserData(self.session) data = u.top_genres('short_term') self.assertGreaterEqual(len(data), 10) for gl in data: self.assertIsInstance(gl, list) for g in gl: self.assertIsInstance(g, str)
def test_bad_time_range(self): """ _top_artists, _top_tracks, _top_genres are dictionaries where the keys are the three different time ranges that Spotify uses: long_term, medium_term, short_term. This ensures that the get functions raise an exception if the dictionary key is not one of these three ranges. """ u = UserData(self.session) self.assertRaises(spotify.SpotifyRequestException, u.top_artists, 'asdf') self.assertRaises(spotify.SpotifyRequestException, u.top_tracks, 'asdf') self.assertRaises(spotify.SpotifyRequestException, u.top_genres, 'asdf')
def test_get_playlist_with_tracks_no_playlist(self): """ get_playlist_with_tracks() must be passed the ID of one of the user's playlists. If it isnt', it should return None and not change the _playlist variable. """ u = UserData(self.session) playlists = u.playlists().copy() data = u.get_playlist_with_tracks('0') self.assertIsNone(data) self.assertCountEqual(playlists, u.playlists())
def test_compile_audio_features(self): """ _compile_audio_features() should request data about audio features of the user's music taste from Spotify and store it in the _music_taste variable. """ u = UserData(self.session) data = u.music_taste_with_audio_features() self.assertGreaterEqual(len(data), 50) for t in data: self.assertEqual(t['type'], 'track') self.assertIsNotNone(t.get('energy')) self.assertIsNotNone(t.get('danceability')) self.assertIsNotNone(t.get('valence'))