コード例 #1
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)
コード例 #2
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)
コード例 #3
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)
コード例 #4
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')
コード例 #5
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)