Example #1
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)
    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)
    def test_question_saved_albums_no_valid_incorrect_choices(self):
        """
        question_saved_albums() should return None if there are not
        enough albums from "top_tracks" to fill incorrect choices.
        """
        u = UserData(self.session)
        artists = create_artists(3)
        json_add_field(artists,
                       'name', ['Cassius', 'Benjamin', 'James'],
                       arr=True)
        albums = create_albums(3)
        json_add_name(albums, 'Country Album ')
        json_add_to_field(albums, 'artists', artists, arr=True)

        u._saved_albums = albums
        u._music_taste = create_tracks(3)
        json_add_field(u._music_taste, 'album', albums, arr=True)

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

        self.assertIsNone(q)
    def test_question_saved_albums(self):
        """
        question_saved_albums() should return a question about the
        user's saved albums.
        """
        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)

        u._saved_albums = create_albums(7)
        json_add_name(u._saved_albums, 'Country Album ')
        json_add_to_field(u._saved_albums, 'artists', artists[0:7], arr=True)
        json_add_to_field(u._saved_albums, 'images', create_image())

        u._music_taste = create_tracks(8)
        albums = create_albums(4, id=7)
        json_add_name(albums, 'Rock Album ')
        json_add_to_field(albums, 'images', create_image())
        json_add_to_field(albums, 'artists', artists[7:11], arr=True)

        json_add_field(u._music_taste[0:4], 'album', albums, arr=True)
        json_add_field(u._music_taste[4:8],
                       'album',
                       u._saved_albums[2:6],
                       arr=True)

        quiz = Quiz.objects.create(user_id='cassius')

        q = question_saved_albums(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 a in u._saved_albums:
                if a['name'] == title and a['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['album']['name'] == title and t['album']['artists'][0][
                        'name'] == artist:
                    found = True
            self.assertTrue(found)
            self.assertEqual(c.image_url, '200url')