def test_question_playlist_tracks_not_enough_tracks(self): """ question_playlist_tracks() should return None when there are no available public playlists with at least 4 tracks. """ u = UserData(None) artists = create_artists(3) json_add_field(artists, 'name', ['Cash', 'Ben', 'Julia']) albums = create_albums(1) json_add_to_field(albums, 'images', create_image()) tracks = create_tracks(13) json_add_name(tracks, 't') json_add_field(tracks, 'album', albums[0]) json_add_to_field(tracks[0:3], 'artists', artists[0]) json_add_to_field(tracks[3:6], 'artists', artists[1]) json_add_to_field(tracks[6:13], 'artists', artists[2]) u._playlists = create_playlists(4) json_add_field(u._playlists[0:3], 'public', True) json_add_field(u._playlists[3:4], 'public', False) playlist_add_track(u._playlists[0], tracks[0:3]) playlist_add_track(u._playlists[1], tracks[3:6]) playlist_add_track(u._playlists[2], tracks[6:9]) playlist_add_track(u._playlists[3], tracks[9:13]) quiz = Quiz.objects.create(user_id='Cassius') question = question_playlist_tracks(quiz, u) self.assertIsNone(question)
def test_question_average_release_date_min_max_too_close(self): """ question_average_release_date() should return a question that asks the average release year of the user's music taste. """ u = UserData(None) u._music_taste = [] dates = ['2017', '2013', '2014', '2016', '2015'] u._music_taste = create_tracks(len(dates)) json_add_field(u._music_taste, 'energy', 0) albums = create_albums(len(dates)) json_add_field(albums, 'release_date', dates, arr=True) json_add_field(u._music_taste, 'album', albums, arr=True) avg = 2015 quiz = Quiz.objects.create(user_id='Cassius') question = question_average_release_date(quiz, u) curr_year = datetime.datetime.now().year self.assertEqual(question.slider_min, 2008) self.assertEqual(question.slider_max, curr_year) self.assertEqual(question.answer, avg)
def test_question_danceability_all_1(self): """ question_danceability() should return a question that asks how danceable the user's music taste is, from 0 to 100. """ u = UserData(None) u = UserData(None) u._music_taste = create_tracks(3) json_add_field(u._music_taste, 'energy', 0) json_add_field(u._music_taste, 'danceability', 1) quiz = Quiz.objects.create(user_id='Cassius') question = question_danceability(quiz, u) self.assertEqual(question.slider_min, 0) self.assertEqual(question.slider_max, 100) self.assertEqual(question.answer, 100)
def test_question_playlist_tracks_just_enough_tracks(self): """ question_playlist_tracks() should work when there is only one public playlists with at least 4 tracks. """ u = UserData(None) artists = create_artists(4) json_add_field(artists, 'name', ['Cash', 'Ben', 'Julia', 'Jim'], arr=True) albums = create_albums(1) json_add_to_field(albums, 'images', create_image()) tracks = create_tracks(13) json_add_name(tracks, 't') json_add_field(tracks, 'album', albums[0]) json_add_to_field(tracks[0:3], 'artists', artists[0]) json_add_to_field(tracks[3:6], 'artists', artists[1]) json_add_to_field(tracks[6:9], 'artists', artists[2]) json_add_to_field(tracks[9:13], 'artists', artists[3]) u._playlists = create_playlists(4) json_add_name(u._playlists, 'playlist') json_add_field(u._playlists, 'public', True) playlist_add_track(u._playlists[0], tracks[0:3]) playlist_add_track(u._playlists[1], tracks[3:6]) playlist_add_track(u._playlists[2], tracks[6:9]) playlist_add_track(u._playlists[3], tracks[9:13]) artists = create_artists(1, id=4) json_add_field(artists, 'name', 'Velma') u._music_taste = create_tracks(4, id=13) json_add_name(u._music_taste, 'track') json_add_to_field(u._music_taste, 'artists', artists[0]) json_add_field(u._music_taste, 'album', albums[0]) quiz = Quiz.objects.create(user_id='Cassius') question = question_playlist_tracks(quiz, u) self.assertGreaterEqual(question.answers().count(), 1) self.assertLessEqual(question.answers().count(), 4) self.assertEqual(question.incorrect_answers().count(), 4 - question.answers().count()) self.assertIn('playlist3', question.text) for a in question.answers(): self.assertEqual(a.secondary_text, 'Jim') self.assertEqual(a.image_url, '200url') for a in question.incorrect_answers(): self.assertEqual(a.secondary_text, 'Velma') self.assertEqual(a.image_url, '200url')
def test_question_user_followers_no_followers(self): """ question_user_followers() should return None if the user has no followers. """ u = UserData(None) u._personal_data = {'followers': {'total': 0}} quiz = Quiz.objects.create(user_id='Cassius') question = question_user_followers(quiz, u) self.assertIsNone(question)
def test_question_user_followers(self): """ question_user_followers() creates a slider question asking how many followers the user has. """ u = UserData(None) u._personal_data = {'followers': {'total': 8}} quiz = Quiz.objects.create(user_id='Cassius') question = question_user_followers(quiz, u) self.assertGreaterEqual(question.slider_min, 0) self.assertLessEqual(question.slider_max, 18) self.assertEqual(question.answer, 8)
def test_question_user_followers_no_negative_options(self): """ question_user_followers() should return a question with a non-negative minimum range value. """ u = UserData(None) u._personal_data = {'followers': {'total': 1}} quiz = Quiz.objects.create(user_id='Cassius') question = question_user_followers(quiz, u) self.assertEqual(question.slider_min, 0) self.assertLessEqual(question.slider_max, 11) self.assertEqual(question.answer, 1)
def test_question_duration_real_request(self): """ question_duration() should return a question that asks what the average length of a song in the user's music taste is. This tests the question with real Spotify data. """ u = UserData(self.session) quiz = Quiz.objects.create(user_id='Cassius') question = question_duration(quiz, u) self.assertLessEqual(question.slider_min, question.answer - 40) self.assertGreaterEqual(question.slider_max, question.answer + 40) self.assertGreaterEqual(question.answer, 30) self.assertLessEqual(question.answer, 7 * 60)
def test_question_danceability_real_request(self): """ question_danceability() should return a question that asks how danceable the user's music taste is, from 0 to 100. This tests the question with real Spotify data. """ u = UserData(self.session) quiz = Quiz.objects.create(user_id='Cassius') question = question_danceability(quiz, u) self.assertEqual(question.slider_min, 0) self.assertEqual(question.slider_max, 100) self.assertGreaterEqual(question.answer, 0) self.assertLessEqual(question.answer, 100)
def test_question_user_followers_real_request(self): """ question_user_followers() creates a slider question asking how many followers the user has. This tests the question with real Spotify data. """ u = UserData(self.session) quiz = Quiz.objects.create(user_id='Cassius') question = question_user_followers(quiz, u) self.assertGreaterEqual(question.slider_min, 0) self.assertGreaterEqual(question.slider_min, question.answer - 10) self.assertLessEqual(question.slider_max, question.answer + 10) self.assertGreaterEqual(question.answer, 0)
def test_question_average_release_date_real_request(self): """ question_average_release_date() should return a question that asks the average release year of the user's music taste. This tests the question with real Spotify data. """ u = UserData(self.session) quiz = Quiz.objects.create(user_id='Cassius') question = question_average_release_date(quiz, u) self.assertGreaterEqual(question.slider_min, 1900) self.assertLessEqual(question.slider_max, 2050) self.assertGreaterEqual(question.answer, 1900) self.assertLessEqual(question.answer, 2050)
def test_question_explicitness_real_request(self): """ question_explicitness() should create a question that asks what percentage of the user's music taste is explicit. Tests this question with real Spotify data. """ u = UserData(self.session) quiz = Quiz.objects.create(user_id='Cassius') question = question_explicitness(quiz, u) self.assertEqual(question.slider_min, 0) self.assertEqual(question.slider_max, 100) self.assertGreaterEqual(question.answer, 0) self.assertLessEqual(question.answer, 100)
def test_question_energy_real_request(self): """ question_energy() should return a question that asks how energetic the user's music taste is, on a scale of 0 to 100. This tests that the question works with real Spotify data. """ u = UserData(self.session) quiz = Quiz.objects.create(user_id='Cassius') question = question_energy(quiz, u) self.assertEqual(question.slider_min, 0) self.assertEqual(question.slider_max, 100) self.assertGreaterEqual(question.answer, 0) self.assertLessEqual(question.answer, 100)
def test_question_music_popularity_real_request(self): """ question_music_popularity() should return a question that asks the average popularity of the user's music taste. This tests the question with real Spotify data. """ u = UserData(self.session) quiz = Quiz.objects.create(user_id='Cassius') question = question_music_popularity(quiz, u) self.assertEqual(question.slider_min, 0) self.assertEqual(question.slider_max, 100) self.assertLessEqual(question.answer, 100) self.assertGreaterEqual(question.answer, 0)
def test_question_popular_playlist_real_request(self): """ question_popular_playlist() should return a question asking which playlist is the user's most popular one, by number of followers. This tests the question with real Spotify data. """ u = UserData(self.session) quiz = Quiz.objects.create(user_id='Cassius') question = question_popular_playlist(quiz, u) self.assertEqual(question.answers().count(), 1) self.assertEqual(question.incorrect_answers().count(), 3) for c in question.choices.all(): self.assertIsNotNone(c.image_url)
def test_question_energy_avg_1(self): """ question_energy() should return a question that asks how energetic the user's music taste is, on a scale of 0 to 100. This tests it when the energy average is 1. """ u = UserData(None) u._music_taste = create_tracks(4) json_add_field(u._music_taste, 'energy', 1) quiz = Quiz.objects.create(user_id='Cassius') question = question_energy(quiz, u) self.assertEqual(question.slider_min, 0) self.assertEqual(question.slider_max, 100) self.assertEqual(question.answer, 100)
def test_question_explicitness_none_explicit(self): """ question_explicitness() should create a question that asks what percentage of the user's music taste is explicit. Tests when none of the user's music taste is explicit. """ u = UserData(None) u._music_taste = create_tracks(4) json_add_field(u._music_taste, 'explicit', 'false') quiz = Quiz.objects.create(user_id='Cassius') question = question_explicitness(quiz, u) self.assertEqual(question.slider_min, 0) self.assertEqual(question.slider_max, 100) self.assertEqual(question.answer, 0)
def test_question_happiness_all_sad(self): """ question_happiness() should return a question that asks how happy the user's music taste is, from 0 to 100. """ u = UserData(None) u._music_taste = create_tracks(4) json_add_field(u._music_taste, 'energy', 0) json_add_field(u._music_taste, 'valence', 0) quiz = Quiz.objects.create(user_id='Cassius') question = question_happiness(quiz, u) self.assertEqual(question.slider_min, 0) self.assertEqual(question.slider_max, 100) self.assertEqual(question.answer, 0)
def test_question_playlist_tracks_real_request(self): """ question_playlist_tracks() should return a question asking which tracks are in one of the user's playlists. This tests the question with real Spotify data. """ u = UserData(self.session) quiz = Quiz.objects.create(user_id='Cassius') question = question_playlist_tracks(quiz, u) self.assertLessEqual(question.answers().count(), 4) self.assertGreaterEqual(question.answers().count(), 1) self.assertEqual(question.incorrect_answers().count(), 4 - question.answers().count()) for c in question.choices.all(): self.assertIsNotNone(c.image_url)
def test_question_popular_playlist_0_followers(self): """ question_popular_playlists() should return None when the maximum follower count is 0. """ u = UserData(None) u._playlists = [] u._playlists = create_playlists(3) json_add_name(u._playlists, 'p') json_add_field(u._playlists[0:3], 'public', True) json_add_field(u._playlists[3:6], 'public', False) json_add_field(u._playlists, 'followers', create_followers(0)) json_add_to_field(u._playlists, 'images', create_image()) quiz = Quiz.objects.create(user_id='Cassius') question = question_popular_playlist(quiz, u) self.assertIsNone(question)
def test_question_energy(self): """ question_energy() should return a question that asks how energetic the user's music taste is, on a scale of 0 to 100. """ u = UserData(None) energies = [0.52, 0.12, 0.25, 0.983, 0.253, 0.534, 0.235] u._music_taste = create_tracks(len(energies)) json_add_field(u._music_taste, 'energy', energies, arr=True) avg = int(100 * sum(energies) / len(energies)) quiz = Quiz.objects.create(user_id='Cassius') question = question_energy(quiz, u) self.assertEqual(question.slider_min, 0) self.assertEqual(question.slider_max, 100) self.assertEqual(question.answer, avg)
def test_question_acousticness_none_acoustic(self): """ question_acousticness() should return a question that asks what percentage of the user's music taste is acoustic. """ u = UserData(None) acousticnesses = [0.26, 0.33, 0.47, 0.12] u._music_taste = create_tracks(len(acousticnesses)) json_add_field(u._music_taste, 'energy', 0) json_add_field(u._music_taste, 'acousticness', acousticnesses, arr=True) quiz = Quiz.objects.create(user_id='Cassius') question = question_acousticness(quiz, u) self.assertEqual(question.slider_min, 0) self.assertEqual(question.slider_max, 100) self.assertEqual(question.answer, 0)
def test_question_music_popularity_avg_100(self): """ question_music_popularity() should return a question that asks the average popularity of the user's music taste. """ u = UserData(None) u._music_taste = [] u._music_taste = create_tracks(3) json_add_field(u._music_taste, 'energy', 0) json_add_field(u._music_taste, 'popularity', 100) avg = 100 quiz = Quiz.objects.create(user_id='Cassius') question = question_music_popularity(quiz, u) self.assertEqual(question.slider_min, 0) self.assertEqual(question.slider_max, 100) self.assertEqual(question.answer, avg)
def test_question_duration_close_to_0(self): """ question_duration() should return a question that asks what the average length of a song in the user's music taste is. """ u = UserData(None) u._music_taste = [] durations = [30 * 1000, 50 * 1000, 20 * 1000, 40 * 1000] u._music_taste = create_tracks(len(durations)) json_add_field(u._music_taste, 'energy', 0) json_add_field(u._music_taste, 'duration_ms', durations, arr=True) avg = int((sum(durations) / len(durations)) / 1000) quiz = Quiz.objects.create(user_id='Cassius') question = question_duration(quiz, u) self.assertEqual(question.slider_min, 0) self.assertGreaterEqual(question.slider_max, avg + 40) self.assertEqual(question.answer, avg)
def test_question_happiness(self): """ question_happiness() should return a question that asks how happy the user's music taste is, from 0 to 100. """ u = UserData(None) u._music_taste = [] happinesses = [0.52, 0.12, 0.25, 0.983, 0.253, 0.534, 0.635] u._music_taste = create_tracks(len(happinesses)) json_add_field(u._music_taste, 'energy', 0) json_add_field(u._music_taste, 'valence', happinesses, arr=True) avg = int(100 * sum(happinesses) / len(happinesses)) quiz = Quiz.objects.create(user_id='Cassius') question = question_happiness(quiz, u) self.assertEqual(question.slider_min, 0) self.assertEqual(question.slider_max, 100) self.assertEqual(question.answer, avg)
def test_question_music_popularity(self): """ question_music_popularity() should return a question that asks the average popularity of the user's music taste. """ u = UserData(None) u._music_taste = [] popularities = [50, 99, 0, 14, 25, 73] u._music_taste = create_tracks(len(popularities)) json_add_field(u._music_taste, 'energy', 0) json_add_field(u._music_taste, 'popularity', popularities, arr=True) avg = int(sum(popularities) / len(popularities)) quiz = Quiz.objects.create(user_id='Cassius') question = question_music_popularity(quiz, u) self.assertEqual(question.slider_min, 0) self.assertEqual(question.slider_max, 100) self.assertEqual(question.answer, avg)
def test_question_popular_playlist_none_followers(self): """ question_popular_playlists() should return None when there aren't enough playlists with non-None follower counts. """ u = UserData(None) u._playlists = [] follower_counts = [4, 2, 5, None] u._playlists = create_playlists(len(follower_counts)) json_add_name(u._playlists, 'p') json_add_field(u._playlists, 'public', True) json_add_field(u._playlists, 'followers', [create_followers(f) for f in follower_counts], arr=True) json_add_to_field(u._playlists, 'images', create_image()) quiz = Quiz.objects.create(user_id='Cassius') question = question_popular_playlist(quiz, u) self.assertIsNone(question)
def test_question_popular_playlist(self): """ question_popular_playlist() should return a question asking which playlist is the user's most popular one, by number of followers. """ u = UserData(None) u._playlists = [] follower_counts = [8, 2, 4, 7, 1, 0, 6] u._playlists = create_playlists(len(follower_counts)) json_add_name(u._playlists, 'p') json_add_field(u._playlists, 'public', True) json_add_field(u._playlists, 'followers', [create_followers(f) for f in follower_counts], arr=True) json_add_to_field(u._playlists, 'images', create_image()) follower_counts = follower_counts[1:] quiz = Quiz.objects.create(user_id='Cassius') question = question_popular_playlist(quiz, u) self.assertEqual(question.answers().count(), 1) self.assertEqual(question.answers()[0].primary_text, 'p0') self.assertEqual(question.incorrect_answers().count(), 3) for c in question.incorrect_answers(): found = False for p in u._playlists: if c.primary_text == p['name']: found = True break self.assertTrue(found) for c in question.choices.all(): self.assertEqual(c.image_url, '200url')
def test_question_popular_playlist_not_enough_public_playlists(self): """ question_popular_playlist() should return None when there are not enough public playlists to form a question with. """ u = UserData(None) u._playlists = [] follower_counts = [8, 2, 4, 1, 2, 3] u._playlists = create_playlists(6) json_add_name(u._playlists, 'p') json_add_field(u._playlists[0:3], 'public', True) json_add_field(u._playlists[3:6], 'public', False) json_add_field(u._playlists, 'followers', [create_followers(f) for f in follower_counts], arr=True) json_add_to_field(u._playlists, 'images', create_image()) quiz = Quiz.objects.create(user_id='Cassius') question = question_popular_playlist(quiz, u) self.assertIsNone(question)
def test_question_popular_playlist_not_enough_less_than_max(self): """ question_popular_playlists() should return None when there are fewer than 3 playlists that have a follower count less than the max number of followers. """ u = UserData(None) u._playlists = [] follower_counts = [5, 5, 1, 2] u._playlists = create_playlists(len(follower_counts)) json_add_name(u._playlists, 'p') json_add_field(u._playlists, 'public', True) json_add_field(u._playlists, 'followers', [create_followers(f) for f in follower_counts], arr=True) json_add_to_field(u._playlists, 'images', create_image()) quiz = Quiz.objects.create(user_id='Cassius') question = question_popular_playlist(quiz, u) self.assertIsNone(question)