Пример #1
0
    def test_context_data__top(self):
        contest = self.create_contest(detail_columns=Contest.TOP,
                                      allow_downvotes=False)
        video1 = self.create_video(name='video1')
        video2 = self.create_video(name='video2')
        video3 = self.create_video(name='video3')
        cv1 = self.create_contestvideo(contest, video1, upvotes=5)
        self.create_contestvideo(contest, video2, upvotes=10)
        self.create_contestvideo(contest, video3, upvotes=3)

        view = ContestDetailView()
        view.object = contest

        context_data = view.get_context_data(object=contest)
        self.assertEqual(list(context_data['top_videos']),
                         [video2, video1, video3])
        self.assertTrue('random_videos' not in context_data)
        self.assertTrue('new_videos' not in context_data)

        # Downvotes should be ignored if they're disallowed.  By adding 6 down
        # votes to the video with 5 votes, if the down votes are counted at all
        # that video will be in the wrong place.
        self.create_votes(cv1, 6, are_up=False)
        context_data = view.get_context_data(object=contest)
        self.assertEqual(list(context_data['top_videos']),
                         [video2, video1, video3])

        # ... and taken into account otherwise.
        contest.allow_downvotes = True
        context_data = view.get_context_data(object=contest)
        self.assertEqual(list(context_data['top_videos']),
                         [video2, video3, video1])
Пример #2
0
    def test_context_data__top(self):
        contest = self.create_contest(detail_columns=Contest.TOP,
                                      allow_downvotes=False)
        video1 = self.create_video(name='video1')
        video2 = self.create_video(name='video2')
        video3 = self.create_video(name='video3')
        cv1 = self.create_contestvideo(contest, video1, upvotes=5)
        self.create_contestvideo(contest, video2, upvotes=10)
        self.create_contestvideo(contest, video3, upvotes=3)

        view = ContestDetailView()
        view.object = contest

        context_data = view.get_context_data(object=contest)
        self.assertEqual(list(context_data['top_videos']),
                         [video2, video1, video3])
        self.assertTrue('random_videos' not in context_data)
        self.assertTrue('new_videos' not in context_data)

        # Downvotes should be ignored if they're disallowed.  By adding 6 down
        # votes to the video with 5 votes, if the down votes are counted at all
        # that video will be in the wrong place.
        self.create_votes(cv1, 6, are_up=False)
        context_data = view.get_context_data(object=contest)
        self.assertEqual(list(context_data['top_videos']),
                         [video2, video1, video3])

        # ... and taken into account otherwise.
        contest.allow_downvotes = True
        context_data = view.get_context_data(object=contest)
        self.assertEqual(list(context_data['top_videos']),
                         [video2, video3, video1])
Пример #3
0
    def test_context_data__random(self):
        contest = self.create_contest(detail_columns=Contest.RANDOM)
        video1 = self.create_video(name='video1')
        video2 = self.create_video(name='video2')
        video3 = self.create_video(name='video3')
        self.create_contestvideo(contest, video1)
        self.create_contestvideo(contest, video2)
        self.create_contestvideo(contest, video3)

        view = ContestDetailView()
        view.object = contest

        context_data = view.get_context_data(object=contest)
        self.assertTrue('random_videos' in context_data)
        self.assertTrue('new_videos' not in context_data)
        self.assertTrue('top_videos' not in context_data)

        # Try to test whether the videos are randomly arranged.
        random = list(context_data['random_videos'])
        contexts = [view.get_context_data(object=contest) for i in xrange(10)]
        self.assertTrue(
            any([random != list(c['random_videos']) for c in contexts]))
Пример #4
0
    def test_context_data__new(self):
        contest = self.create_contest(detail_columns=Contest.NEW)
        # MySQL times are only accurate to one second, so make sure the times
        # are different by a whole second.
        now = datetime.datetime.now()
        second = datetime.timedelta(seconds=1)
        video1 = self.create_video(name='video1',
                                   when_approved=now - second * 2)
        video2 = self.create_video(name='video2', when_approved=now - second)
        video3 = self.create_video(name='video3', when_approved=now)
        self.create_contestvideo(contest, video1)
        self.create_contestvideo(contest, video2)
        self.create_contestvideo(contest, video3)

        view = ContestDetailView()
        view.object = contest

        context_data = view.get_context_data(object=contest)
        self.assertEqual(list(context_data['new_videos']),
                         [video3, video2, video1])
        self.assertTrue('random_videos' not in context_data)
        self.assertTrue('top_videos' not in context_data)
Пример #5
0
    def test_context_data__random(self):
        contest = self.create_contest(detail_columns=Contest.RANDOM)
        video1 = self.create_video(name='video1')
        video2 = self.create_video(name='video2')
        video3 = self.create_video(name='video3')
        self.create_contestvideo(contest, video1)
        self.create_contestvideo(contest, video2)
        self.create_contestvideo(contest, video3)

        view = ContestDetailView()
        view.object = contest

        context_data = view.get_context_data(object=contest)
        self.assertTrue('random_videos' in context_data)
        self.assertTrue('new_videos' not in context_data)
        self.assertTrue('top_videos' not in context_data)

        # Try to test whether the videos are randomly arranged.
        random = list(context_data['random_videos'])
        contexts = [view.get_context_data(object=contest)
                    for i in xrange(10)]
        self.assertTrue(any([random != list(c['random_videos'])
                             for c in contexts]))
Пример #6
0
    def test_context_data__new(self):
        contest = self.create_contest(detail_columns=Contest.NEW)
        # MySQL times are only accurate to one second, so make sure the times
        # are different by a whole second.
        now = datetime.datetime.now()
        second = datetime.timedelta(seconds=1)
        video1 = self.create_video(name='video1',
                                   when_approved=now - second * 2)
        video2 = self.create_video(name='video2',
                                   when_approved=now - second)
        video3 = self.create_video(name='video3',
                                   when_approved=now)
        self.create_contestvideo(contest, video1)
        self.create_contestvideo(contest, video2)
        self.create_contestvideo(contest, video3)

        view = ContestDetailView()
        view.object = contest

        context_data = view.get_context_data(object=contest)
        self.assertEqual(list(context_data['new_videos']),
                         [video3, video2, video1])
        self.assertTrue('random_videos' not in context_data)
        self.assertTrue('top_videos' not in context_data)