Example #1
0
 def test_get_urls(self):
     cat = mixer.blend('notorhot.CandidateCategory', slug='cat-slug')
     self.assertEqual(cat.get_absolute_url(), '/cat-slug/')
     self.assertEqual(cat.get_leaderboard_url(), '/cat-slug/leaders/')
     
     cand = mixer.blend('notorhot.Candidate', slug='cand-slug', category=cat)
     self.assertEqual(cand.get_absolute_url(), '/candidate/cat-slug/cand-slug/')
Example #2
0
 def test_for_category(self):
     cat1 = mixer.blend('notorhot.CandidateCategory')
     cat2 = mixer.blend('notorhot.CandidateCategory')
     mixer.cycle(3).blend('notorhot.Candidate', category=cat1)
     mixer.cycle(2).blend('notorhot.Candidate', category=cat2)
     
     self.assertEqual(Candidate.objects.for_category(cat1).count(), 3)
Example #3
0
 def test_save(self):
     self.assertEqual(Competition.objects.count(), 0)
     
     cat1 = mixer.blend('notorhot.CandidateCategory')
     cat2 = mixer.blend('notorhot.CandidateCategory')
     cands = mixer.cycle(3).blend('notorhot.Candidate', category=cat1, 
         is_enabled=True, challenges=0)
             
     comp = Competition(**{
         'left': cands[0],
         'right': cands[1],
     })
     
     comp.save()
     
     self.assertEqual(Competition.objects.count(), 1)
     self.assertEqual(cands[0].challenges, 1)
     self.assertEqual(cands[1].challenges, 1)
     self.assertEqual(comp.category, cat1)
     self.assertIsNone(comp.winner)
     self.assertIsNone(comp.winning_side)
     self.assertIsNone(comp.date_voted)
     self.assertIsNotNone(comp.date_presented)    
     
     # make sure saving again doesn't set any new values
     comp.save()
     
     self.assertEqual(Competition.objects.count(), 1)
     self.assertEqual(cands[0].challenges, 1)
     self.assertEqual(cands[1].challenges, 1)
     self.assertEqual(comp.category, cat1)
     self.assertIsNone(comp.winner)
     self.assertIsNone(comp.winning_side)
     self.assertIsNone(comp.date_voted)
     self.assertIsNotNone(comp.date_presented)    
Example #4
0
    def test_save(self):
        self.instantiate_tracker()
    
        write_in = mixer.blend(SimpleWriteIn, status=None,
            date_submitted=None)
        self.assertEqual(write_in.status, SimpleWriteIn.STATUS.SUBMITTED)
        self.assertIsNotNone(write_in.date_submitted)
        self.assertIsNone(write_in.date_processed)
            
        # check setting date_processed when status changes
        write_in.status = SimpleWriteIn.STATUS.ACCEPTED
        write_in.save()
        self.assertIsNotNone(write_in.date_processed)

        # well, technically, we should never be creating instances that are 
        # already accepted / rejected, but this is useful for testing a few 
        # other things
        write_in = mixer.blend(SimpleWriteIn, 
            status=SimpleWriteIn.STATUS.ACCEPTED)
        self.assertEqual(write_in.status, SimpleWriteIn.STATUS.ACCEPTED)
        self.assertIsNone(write_in.date_processed)
        
        write_in.status = None
        write_in.save()
        # preserve previous value on clearing pre-existing instance's status
        self.assertEqual(write_in.status, SimpleWriteIn.STATUS.ACCEPTED)
        self.assertIsNone(write_in.date_processed)
Example #5
0
 def test_init(self):
     if hasattr(SimpleWriteIn, 'submission_tracker'):
         self.fail(u"Submission tracker should not exist before __init__ is"
             u"called.")
     
     mixer.blend(SimpleWriteIn)
     self.assertIsNotNone(getattr(SimpleWriteIn, 'submission_tracker', None))
Example #6
0
 def test_get_categories(self):
     cat1 = mixer.blend('notorhot.CandidateCategory')
     cat2 = mixer.blend('notorhot.CandidateCategory')
     cat3 = mixer.blend('notorhot.CandidateCategory', is_public=False)
     view = self.make_view('get')
     
     view_cats = view.get_categories()
     self.assertItemsEqual(view_cats, (cat1, cat2))
Example #7
0
    def test_non_public(self):
        cat = mixer.blend('notorhot.CandidateCategory', slug='cat-slug', 
            is_public=False)
        cand = mixer.blend('notorhot.Candidate', category=cat, name='Alpha', 
            slug='alpha')

        response = self.client.get('/candidate/cat-slug/alpha/')
        
        self.assertEqual(response.status_code, 404)
Example #8
0
 def test_non_public(self):
     cat = mixer.blend('notorhot.CandidateCategory', slug='cat-slug', 
         is_public=False)
     comp = mixer.blend('notorhot.Competition', category=cat, id=1)
     
     response = self.client.post('/vote/1/', follow=False,
         data={ 'winner': Competition.SIDES.LEFT, })
         
     self.assertEqual(response.status_code, 404)
Example #9
0
    def test_clean(self):
        cat1 = mixer.blend('notorhot.CandidateCategory')
        cat2 = mixer.blend('notorhot.CandidateCategory')
        cands = mixer.cycle(3).blend('notorhot.Candidate', category=cat1, 
            is_enabled=True)
        cand_cat2 = mixer.blend('notorhot.Candidate', category=cat2, 
            is_enabled=True)
        
        try:
            comp = Competition(**{
                'left': cands[0],
                'right': cands[1],
            })
            comp.clean()
        except ValidationError:
            self.fail(u"Competition with no winner or category set should clean "
                u"without trouble.")

        try:
            comp = Competition(**{
                'left': cands[0],
                'right': cands[1],
                'winner': cands[0],
                'category': cat1,
            })
            comp.clean()
        except ValidationError:
            self.fail(u"Competition with winner and category matching candidates "
                u"shoudl clean without trouble")

        # winner in same category but difft competition
        with self.assertRaises(ValidationError):
            comp = Competition(**{
                'left': cands[0],
                'right': cands[1],
                'winner': cands[2],
            })
            comp.clean()

        # category doesn't match candidates
        with self.assertRaises(ValidationError):
            comp = Competition(**{
                'left': cands[0],
                'right': cands[1],
                'category': cat2,
            })
            comp.clean()

        # candidates from different categories
        with self.assertRaises(ValidationError):
            comp = Competition(**{
                'left': cands[0],
                'right': cand_cat2,
            })
            comp.clean()
Example #10
0
    def test_get_leaders(self):
        cat1 = mixer.blend('notorhot.CandidateCategory')
        cat2 = mixer.blend('notorhot.CandidateCategory')
        
        (cand1, cand2, cand3, cand4) = generate_leaderboard_data(cat1, cat2)

        view = self.make_view('get', view_kwargs={ 'leaderboard_length': 3 },
            request_kwargs={ 'category_slug': cat1.slug, })
        
        leaders = view.get_leaders()
        self.assertEqual(list(leaders), [cand4, cand3, cand2,])
Example #11
0
 def test_already_voted(self):
     cat = mixer.blend('notorhot.CandidateCategory', slug='cat-slug')
     cand1 = mixer.blend('notorhot.Candidate', category=cat, name='Alpha')
     cand2 = mixer.blend('notorhot.Candidate', category=cat, name='Beta')
     comp = mixer.blend('notorhot.Competition', left=cand1, right=cand2, 
         category=cat, id=1, date_voted=mixer.fake)
     
     response = self.client.post('/vote/1/', follow=False,
         data={ 'winner': Competition.SIDES.LEFT, })
         
     self.assertEqual(response.status_code, 404)
Example #12
0
 def test_manager_and_queryset_chaining(self):
     cat1 = mixer.blend('notorhot.CandidateCategory')
     cat2 = mixer.blend('notorhot.CandidateCategory')
     
     (cand1, cand2, cand3, cand4) = generate_leaderboard_data(cat1, cat2)
     
     in_order = Candidate.enabled.for_category(cat1).order_by_wins()
     # get win count for ordered candidates = should be 6, 7, 8, 9
     wins = [c.wins for c in in_order]
     self.assertEqual(wins, [6, 7, 8, 9])
     self.assertEqual(list(in_order), [cand4, cand3, cand2, cand1])
Example #13
0
 def test_get_category(self):
     cat1 = mixer.blend('notorhot.CandidateCategory')
     view = self.make_view('get', request_kwargs={ 'category_slug': cat1.slug, })
     
     view_cat = view.get_category()
     self.assertEqual(view_cat, cat1)
     
     cat2 = mixer.blend('notorhot.CandidateCategory', is_public=False)
     cand = mixer.blend('notorhot.Candidate', category=cat2)
     view = self.make_view('get', request_kwargs={ 'category_slug': cat2.slug, })
     
     with self.assertRaises(Http404):
         view.get_category()
Example #14
0
 def test_insufficient_data(self):
     cat = mixer.blend('notorhot.CandidateCategory', slug='cat-slug')
     mixer.blend('notorhot.CandidateCategory')
     cand1 = mixer.blend('notorhot.Candidate', category=cat, name='Alpha')
     
     response = self.client.get('/cat-slug/')
     
     self.assertEqual(response.status_code, 200)
     self.assertIsInstance(response.context['view'], CompetitionView)
     self.assertNotIn('competition', response.context)
     self.assertNotIn('Alpha', response)
     self.assertNotIn('Beta', response)
     self.assertTemplateUsed(response, 'notorhot/insufficient_data.html')
Example #15
0
    def test_success(self):
        cat = mixer.blend('notorhot.CandidateCategory', slug='cat-slug')
        cand = mixer.blend('notorhot.Candidate', category=cat, name='Alpha', slug='alpha')

        response = self.client.get('/candidate/cat-slug/alpha/')
        
        self.assertEqual(response.status_code, 200)
        self.assertIsInstance(response.context['view'], CandidateView)
        self.assertIsNotNone(response.context['candidate'])
        self.assertEqual(response.context['candidate'], cand)
        self.assertIsNotNone(response.context['category'])
        self.assertEqual(response.context['category'], cat)
        self.assertContains(response, 'Alpha')
        self.assertTemplateUsed(response, 'notorhot/candidate.html')
Example #16
0
    def test_clean(self):
        cands = mixer.cycle(3).blend('notorhot.Candidate', enabled=True)
        comp = mixer.blend('notorhot.Competition', left=cands[0], right=cands[1])
        
        # basic validation should work
        form = VoteForm({ 'winner': Competition.SIDES.LEFT, }, competition=comp)
        self.assertTrue(form.is_valid())
        
        # but if we have already voted, it shouldn't
        comp = mixer.blend('notorhot.Competition', left=cands[0], right=cands[1],
            date_voted=datetime.datetime.now())

        form = VoteForm({ 'winner': Competition.SIDES.LEFT, }, competition=comp)
        self.assertFalse(form.is_valid())
Example #17
0
 def test_get_category(self):
     cats = mixer.cycle(2).blend('notorhot.CandidateCategory')
     cand = mixer.blend('notorhot.Candidate', category=cats[0])
     view = self.make_view('get', view_kwargs={ 'object': cand, })
     
     cat = view.get_category()
     self.assertEqual(cat, cats[0])
     
     cat = mixer.blend('notorhot.CandidateCategory', is_public=False)
     cand = mixer.blend('notorhot.Candidate', category=cat)
     view = self.make_view('get', view_kwargs={ 'object': cand, })
     
     with self.assertRaises(Http404):
         view.get_category()
Example #18
0
 def test_get_form_kwargs(self):
     comp = mixer.blend('notorhot.Competition')
     view = self.make_view('post', view_kwargs={ 'object': comp, })
     
     kwargs = view.get_form_kwargs()
     self.assertTrue('competition' in kwargs)
     self.assertEqual(kwargs['competition'], comp)
     
     cat = mixer.blend('notorhot.CandidateCategory', is_public=False)
     comp2 = mixer.blend('notorhot.Competition', category=cat)
     view = self.make_view('post', view_kwargs={ 'object': comp2, })
     
     with self.assertRaises(Http404):
         view.get_form_kwargs()
Example #19
0
    def test__get_category(self):
        cat = mixer.blend('notorhot.CandidateCategory', slug='abcd')
        mixer.blend('notorhot.CandidateCategory')
        
        view = self.make_view('get', request_kwargs={ 'category_slug': 'abcd', })
        view_cat = view._get_category()
        self.assertEqual(view_cat, cat)
        
        view = self.make_view('get', request_kwargs={ 'slug': 'abcd', })
        view_cat = view._get_category(slug_name='slug')
        self.assertEqual(view_cat, cat)

        view = self.make_view('get', request_kwargs={ 'category_slug': 'def', })
        view_cat = view._get_category()
        self.assertIsNone(view_cat)
Example #20
0
 def test_get_category(self):
     cat = mixer.blend('notorhot.CandidateCategory')
     view = self.make_view('get', request_kwargs={ 'category_slug': cat.slug, })
     
     view_cat = view.get_category()
     self.assertEqual(view_cat, cat)
     
     cat = mixer.blend('notorhot.CandidateCategory', is_public=False)
     view = self.make_view('get', request_kwargs={ 'category_slug': cat.slug, })
     
     view_cat = view.get_category()
     self.assertIsNotNone(view_cat)
     
     # should be able to run with or without category
     view = self.make_view('get')
     view_cat = view.get_category()
Example #21
0
    def test_record_vote(self):
        cat = mixer.blend('notorhot.CandidateCategory')
        cands = mixer.cycle(2).blend('notorhot.Candidate', is_enabled=True, 
            challenges=0, votes=0, wins=0, category=cat)
            
        comp = Competition.objects.generate_from_candidates(cands[0], cands[1])
        
        self.assertEqual(cands[0].challenges, 1)
        self.assertEqual(cands[0].votes, 0)
        self.assertEqual(cands[0].wins, 0)
        self.assertIsNone(comp.winner)
        self.assertIsNone(comp.winning_side)
        self.assertIsNone(comp.date_voted)
        
        comp.record_vote(Competition.SIDES.RIGHT)

        self.assertEqual(cands[0].challenges, 1)
        self.assertEqual(cands[0].votes, 1)
        self.assertEqual(cands[0].wins, 0)
        self.assertEqual(cands[1].votes, 1)
        self.assertEqual(cands[1].wins, 1)
        self.assertEqual(comp.winner, cands[1])
        self.assertEqual(comp.winning_side, Competition.SIDES.RIGHT)
        self.assertIsNotNone(comp.date_voted)
        
        # shouldn't be able to record a vote on a competition that's already
        # been voted on
        with self.assertRaises(Competition.AlreadyVoted):
            comp.record_vote(Competition.SIDES.RIGHT)
Example #22
0
    def test_non_public(self):
        cat1 = mixer.blend('notorhot.CandidateCategory', slug='cat-slug', is_public=False)
        mixer.cycle(3).blend('notorhot.Candidate', category=cat1)

        response = self.client.get('/cat-slug/leaders/')
        
        self.assertEqual(response.status_code, 404)
Example #23
0
    def test_success(self):
        cat1 = mixer.blend('notorhot.CandidateCategory', name='Alpha')
        cat2 = mixer.blend('notorhot.CandidateCategory', name='Beta')    
        cat3 = mixer.blend('notorhot.CandidateCategory', name='Gamma', 
            is_public=False)        

        response = self.client.get('/')
        
        self.assertEqual(response.status_code, 200)
        self.assertIsInstance(response.context['view'], CategoryListView)
        self.assertIsNotNone(response.context['categories'])
        self.assertItemsEqual(response.context['categories'], [cat1, cat2])
        self.assertContains(response, 'Alpha')
        self.assertContains(response, 'Beta')
        self.assertNotContains(response, 'Gamma')
        self.assertTemplateUsed(response, 'notorhot/categories.html')
Example #24
0
    def test_get_form_class_without_category(self):
        cat = mixer.blend('notorhot.CandidateCategory')
        view = self.make_view('get')

        form_class = view.get_form_class()
        self.assertItemsEqual(form_class.base_fields.keys(), 
            ['candidate_name', 'submitter_name', 'submitter_email', 'category'])

        # make sure we add the category to fields / remove from exclude
        view = self.make_view('get')
        view.fields = ['candidate_name',]
        view.exclude_fields.append('category')
        self.assertNotIn('category', view.fields)   
        self.assertIn('category', view.exclude_fields)     
        
        form_class = view.get_form_class()
        self.assertIn('category', view.fields)
        self.assertNotIn('category', view.exclude_fields)
        
        # but don't add to fields if no fields set
        view = self.make_view('get')
        self.assertItemsEqual(view.fields, []) 
        
        form_class = view.get_form_class()
        self.assertItemsEqual(view.fields, [])
Example #25
0
    def test_with_object(self):
        instance = mixer.blend(self.Model)

        class View(RichFormFactoryCreateView):
            object = instance

        form_class = self.get_form_class(View)
        self.assertEqual(form_class._meta.model, self.Model)
Example #26
0
 def test_success(self):
     cat = mixer.blend('notorhot.CandidateCategory', slug='cat-slug')
     cand1 = mixer.blend('notorhot.Candidate', category=cat, name='Alpha')
     cand2 = mixer.blend('notorhot.Candidate', category=cat, name='Beta')
     comp = mixer.blend('notorhot.Competition', left=cand1, right=cand2, 
         category=cat, id=1)
     
     response = self.client.post('/vote/1/', follow=False,
         data={ 'winner': Competition.SIDES.LEFT, })
         
     the_comp = Competition.objects.get(id=1)
         
     self.assertEqual(response.status_code, 302)
     self.assertEqual(the_comp.winner, cand1)
     self.assertEqual(the_comp.winning_side, Competition.SIDES.LEFT)
     self.assertIsNotNone(the_comp.date_voted)
     self.assertRedirects(response, '/cat-slug/')
Example #27
0
    def test_success(self):
        cat1 = mixer.blend('notorhot.CandidateCategory', slug='cat-slug')
        cat2 = mixer.blend('notorhot.CandidateCategory')        
        (cand1, cand2, cand3, cand4) = generate_leaderboard_data(cat1, cat2)

        with patch.object(LeaderboardView, 'leaderboard_length', new=3):
            response = self.client.get('/cat-slug/leaders/')
        
        self.assertEqual(response.status_code, 200)
        self.assertIsInstance(response.context['view'], LeaderboardView)
        self.assertIsNotNone(response.context['leaders'])
        self.assertEqual(list(response.context['leaders']), [cand4, cand3, cand2])
        self.assertIsNotNone(response.context['category'])
        self.assertEqual(response.context['category'], cat1)
        self.assertContains(response, 'Beta')
        self.assertContains(response, 'Gamma')
        self.assertContains(response, 'Delta')
        self.assertTemplateUsed(response, 'notorhot/leaders.html')
Example #28
0
 def test_order_by_wins(self):
     # test ordered by % wins, not # wins
     mixer.blend('notorhot.Candidate', votes=18, wins=9) # .5
     mixer.blend('notorhot.Candidate', votes=15, wins=8) # .5333
     mixer.blend('notorhot.Candidate', votes=12, wins=7) # .58333
     mixer.blend('notorhot.Candidate', votes=9, wins=6) # .6667
         
     in_order = Candidate.objects.all().order_by_wins()
     # get win count for ordered candidates = should be 6, 7, 8, 9
     wins = [c.wins for c in in_order]
     self.assertEqual(wins, [6, 7, 8, 9])
Example #29
0
    def test_invalid_answer(self):
        cat = mixer.blend('notorhot.CandidateCategory', slug='cat-slug')
        cand1 = mixer.blend('notorhot.Candidate', category=cat, name='Alpha')
        cand2 = mixer.blend('notorhot.Candidate', category=cat, name='Beta')
        comp = mixer.blend('notorhot.Competition', left=cand1, right=cand2, 
            category=cat, id=1)
        
        response = self.client.post('/vote/1/', follow=False,
            data={ 'winner': 99, })

        the_comp = Competition.objects.get(id=1)
            
        self.assertEqual(response.status_code, 205)
        self.assertIsNone(the_comp.winner)
        self.assertIsNone(the_comp.winning_side)
        self.assertIsNone(the_comp.date_voted)
        self.assertIsInstance(response.context['form'], VoteForm)
        self.assertIsInstance(response.context['view'], VoteView)
        self.assertTemplateUsed(response, 'notorhot/invalid_vote.html')
Example #30
0
 def test_get_success_url(self):
     comp = mixer.blend('notorhot.Competition')
     view = self.make_view('post', view_kwargs={ 'object': comp, })
     
     with patch.object(comp.category, 'get_absolute_url') as mock_get_url:
         mock_get_url.return_value = '/a/url/'
         success_url = view.get_success_url()
         
         self.assertEqual(mock_get_url.call_count, 1)
         self.assertEqual(success_url, '/a/url/')