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_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_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_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_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_average_release_date(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 = ['1954-10-02', '1998-04-04', '2020-01-10', '2005-12-25'] 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 = int((1954 + 1998 + 2020 + 2005) / len(dates)) quiz = Quiz.objects.create(user_id='Cassius') question = question_average_release_date(quiz, u) self.assertLessEqual(question.slider_min, 1954) self.assertGreaterEqual(question.slider_max, 2020) self.assertEqual(question.answer, avg)
def test_question_acousticness(self): """ question_acousticness() should return a question that asks what percentage of the user's music taste is acoustic. """ u = UserData(None) u._music_taste = [] acousticnesses = [0.52, 0.12, 0.25, 0.983, 0.253, 0.534, 0.635] 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) percentage = int(100 * 4 / 7) 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, percentage)
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_danceability_all_0(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._music_taste = create_tracks(3) json_add_field(u._music_taste, 'energy', 0) json_add_field(u._music_taste, 'danceability', 0) 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, 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_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_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_playlist_tracks(self): """ question_playlist_tracks() should return a question asking which tracks are in one of the user's playlists. """ u = UserData(None) artists = create_artists(3) json_add_field(artists, 'name', ['Cash', 'Ben', 'Julia'], arr=True) album = create_albums(1) json_add_to_field(album, 'images', create_image()) tracks = create_tracks(15) json_add_name(tracks, 't') json_add_to_field(tracks[0:5], 'artists', artists[0]) json_add_to_field(tracks[5:10], 'artists', artists[1]) json_add_to_field(tracks[10:15], 'artists', artists[2]) json_add_field(tracks, 'album', album[0]) u._playlists = create_playlists(3) json_add_name(u._playlists, 'playlist') json_add_field(u._playlists, 'public', True) playlist_add_track(u._playlists[0], tracks[0:5]) playlist_add_track(u._playlists[1], tracks[5:10]) playlist_add_track(u._playlists[2], tracks[10:15]) u._music_taste = [ u._playlists[0]['tracks']['items'][4]['track'], u._playlists[1]['tracks']['items'][4]['track'], u._playlists[2]['tracks']['items'][4]['track'], ] tracks = create_tracks(4, id=15) json_add_name(tracks, 't4') artist = create_artists(1, id=3) json_add_field(artist, 'name', 'Velma') json_add_to_field(tracks, 'artists', artist[0]) json_add_field(tracks, 'album', album[0]) u._music_taste.extend(tracks) 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()) found = False for p in u._playlists: if p['name'] in question.text: found = True self.assertTrue(found) artist_name = question.answers()[0].secondary_text for a in question.answers(): self.assertEqual(a.secondary_text, artist_name) self.assertEqual(a.image_url, '200url') for a in question.incorrect_answers(): self.assertNotEqual(a.secondary_text, artist_name) self.assertEqual(a.image_url, '200url')