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)
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)
def test_generate_competition(self): cat = mixer.blend('notorhot.CandidateCategory') mixer.cycle(4).blend('notorhot.Candidate', category=cat) view = self.make_view('get', view_kwargs={ 'object': cat, }) num_comps = Competition.objects.count() comp = view.generate_competition() self.assertEqual(comp.category, cat) self.assertEqual(Competition.objects.count(), num_comps + 1)
def test_category_competition_generator(self): cat = mixer.blend('notorhot.CandidateCategory') with self.assertRaises(Candidate.DoesNotExist): cat.generate_competition() enabled = mixer.cycle(3).blend('notorhot.Candidate', category=cat, is_enabled=True) disabled = mixer.cycle(2).blend('notorhot.Candidate', category=cat, is_enabled=False) comp = cat.generate_competition() self.assertTrue(comp.left in enabled) self.assertTrue(comp.right in enabled) self.assertEqual(comp.category, cat)
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)
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)
def test_get_context_data(self): cat = mixer.blend('notorhot.CandidateCategory') view = self.make_view('get', view_kwargs={ 'object': cat, }, session_data={}) context = view.get_context_data() self.assertEqual(view.template_name, 'notorhot/insufficient_data.html') self.assertFalse('competition' in context) self.assertFalse('previous_vote' in context) mixer.cycle(4).blend('notorhot.Candidate', category=cat) view = self.make_view('get', view_kwargs={ 'object': cat, }, session_data={}) context = view.get_context_data() self.assertEqual(view.template_name, 'notorhot/competition.html') self.assertTrue('competition' in context) self.assertTrue('previous_vote' in context)
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()
def test_save(self): cat = mixer.blend('notorhot.CandidateCategory') cands = mixer.cycle(2).blend('notorhot.Candidate', enabled=True, category=cat) comp = Competition.objects.generate_from_candidates(cands[0], cands[1]) form = VoteForm({ 'winner': Competition.SIDES.LEFT, }, competition=comp) self.assertTrue(form.is_valid()) with patch.object(comp, 'record_vote') as mock_record_vote: form.save() self.assertEqual(mock_record_vote.call_count, 1) self.assertEqual(mock_record_vote.call_args[0], (Competition.SIDES.LEFT,))
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())
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()
def test_generating_manager(self): with self.assertRaises(Candidate.DoesNotExist): comp = Competition.objects.generate_from_queryset( Candidate.objects.all()) self.assertEqual(Competition.objects.count(), 0) cats = mixer.cycle(2).blend('notorhot.CandidateCategory') cands = [] for i in range(10): cands.append(mixer.blend('notorhot.Candidate', name='Cand%d' % i, votes=i, enabled=True, category=cats[i % 2])) # candidates must have same category with self.assertRaises(Competition.objects.NonMatchingCategory): Competition.objects.generate_from_candidates(cands[0], cands[1]) with self.assertRaises(Competition.objects.NonMatchingCategory): Competition.objects.generate_from_queryset(Candidate.objects.all()) self.assertEqual(Competition.objects.count(), 0) comp = Competition.objects.generate_from_candidates(cands[0], cands[2]) self.assertEqual(Competition.objects.count(), 1) self.assertIsNone(comp.date_voted) self.assertEqual(comp.left, cands[0]) self.assertEqual(comp.right, cands[2]) popular = Candidate.objects.filter(category=cats[0], votes__gt=3) for i in range(100): comp = Competition.objects.generate_from_queryset(popular) self.assertEqual(Competition.objects.count(), i + 2) self.assertTrue(comp.left in popular) self.assertTrue(comp.right in popular)
def test_enabled(self): mixer.cycle(3).blend('notorhot.Candidate', is_enabled=True) mixer.cycle(2).blend('notorhot.Candidate', is_enabled=False) self.assertEqual(Candidate.enabled.count(), 3)
def test_category_num_voted_comps(self): cat = mixer.blend('notorhot.CandidateCategory') mixer.cycle(3).blend('notorhot.Competition', category=cat, date_voted=None) mixer.cycle(2).blend('notorhot.Competition', category=cat, date_voted=datetime.datetime.now()) self.assertEqual(cat.num_voted_competitions, 2)
def test_category_num_candidates(self): cat = mixer.blend('notorhot.CandidateCategory') mixer.cycle(3).blend('notorhot.Candidate', category=cat, is_enabled=True) mixer.cycle(2).blend('notorhot.Candidate', category=cat, is_enabled=False) self.assertEqual(cat.num_candidates, 3)
def test_votable_manager(self): mixer.cycle(3).blend('notorhot.Competition', date_voted=None) mixer.cycle(2).blend('notorhot.Competition', date_voted=datetime.datetime.now()) self.assertEqual(Competition.votable.count(), 3)