コード例 #1
0
    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)
コード例 #2
0
    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)
コード例 #3
0
 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)
コード例 #4
0
 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)
コード例 #5
0
 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)
コード例 #6
0
 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)
コード例 #7
0
 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)
コード例 #8
0
 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)
コード例 #9
0
 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)
コード例 #10
0
 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)
コード例 #11
0
 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)
コード例 #12
0
 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)
コード例 #13
0
    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')
コード例 #14
0
 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)
コード例 #15
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'))
コード例 #16
0
 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])
コード例 #17
0
 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')
コード例 #18
0
 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')
コード例 #19
0
 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')
コード例 #20
0
 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')
コード例 #21
0
 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')
コード例 #22
0
 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')
コード例 #23
0
 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')
コード例 #24
0
 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')
コード例 #25
0
    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)
コード例 #26
0
 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'))
コード例 #27
0
 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)
コード例 #28
0
    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())
コード例 #29
0
 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'))
コード例 #30
0
    def test_question_saved_albums_only_one_correct_answer(self):
        """
        question_saved_albums() should create a question, even if there
        is only one available album that can be correct, as long as
        there are enough albums in "top_tracks" to fill the incorrect
        choices.
        """
        u = UserData(self.session)

        artists = create_artists(3)
        json_add_field(artists,
                       'name', ['Cassius', 'Benjamin', 'James'],
                       arr=True)

        albums = create_albums(4)
        json_add_name(albums, 'Country Album ')
        json_add_to_field(albums[0:2], 'artists', artists[0])
        json_add_to_field(albums[2:4], 'artists', artists[1:3], arr=True)
        json_add_to_field(albums, 'images', create_image())

        u._saved_albums = [albums[0]]

        u._music_taste = create_tracks(3)
        json_add_field(u._music_taste, 'album', albums[1:], arr=True)

        quiz = Quiz.objects.create(user_id='cassius')
        q = question_saved_albums(quiz, u)

        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(u._saved_albums[0]['name'], c.primary_text)
        self.assertEqual(u._saved_albums[0]['artists'][0]['name'],
                         c.secondary_text)

        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
            found = False
            for t in u._music_taste:
                if t['album']['name'] == title and t['album']['artists'][0][
                        'name'] == artist:
                    found = True
            self.assertTrue(found)