bgneal@540: """ bgneal@540: Model tests for the contests application. bgneal@540: bgneal@540: """ bgneal@540: import datetime bgneal@540: bgneal@540: from django.test import TestCase bgneal@540: from django.contrib.auth.models import User bgneal@540: bgneal@540: from contests.models import Contest bgneal@540: bgneal@540: bgneal@540: class ContestTestCase(TestCase): bgneal@540: bgneal@540: def test_creation_date(self): bgneal@540: bgneal@540: c = Contest(title='test', bgneal@540: slug='test', bgneal@540: description='test', bgneal@540: is_public=False, bgneal@540: end_date=datetime.datetime.now() + datetime.timedelta(days=30)) bgneal@540: bgneal@540: c.save() bgneal@540: bgneal@540: self.assertTrue(c.creation_date) bgneal@540: self.assertTrue(datetime.datetime.now() - c.creation_date < bgneal@540: datetime.timedelta(seconds=1)) bgneal@540: bgneal@540: def test_is_active(self): bgneal@540: bgneal@540: now = datetime.datetime.now() bgneal@540: start = now + datetime.timedelta(days=7) bgneal@540: end = start + datetime.timedelta(days=30) bgneal@540: bgneal@540: c = Contest(title='test', bgneal@540: slug='test', bgneal@540: description='test', bgneal@540: is_public=False, bgneal@540: creation_date=start, bgneal@540: end_date=end) bgneal@540: bgneal@744: self.assertFalse(c.is_active()) bgneal@540: bgneal@540: start = now - datetime.timedelta(days=7) bgneal@540: end = start + datetime.timedelta(days=30) bgneal@540: bgneal@540: c = Contest(title='test', bgneal@540: slug='test', bgneal@540: description='test', bgneal@540: is_public=True, bgneal@540: creation_date=start, bgneal@540: end_date=end) bgneal@540: bgneal@540: self.assertTrue(c.is_active()) bgneal@540: bgneal@540: start = now - datetime.timedelta(days=7) bgneal@540: end = start - datetime.timedelta(days=3) bgneal@540: bgneal@540: c = Contest(title='test', bgneal@540: slug='test', bgneal@540: description='test', bgneal@540: is_public=True, bgneal@540: creation_date=start, bgneal@540: end_date=end) bgneal@540: bgneal@744: self.assertFalse(c.is_active()) bgneal@540: bgneal@540: def test_can_enter(self): bgneal@540: bgneal@540: now = datetime.datetime.now() bgneal@540: start = now + datetime.timedelta(days=7) bgneal@540: end = start + datetime.timedelta(days=30) bgneal@540: bgneal@540: c = Contest(title='test', bgneal@540: slug='test', bgneal@540: description='test', bgneal@540: is_public=False, bgneal@540: creation_date=start, bgneal@540: end_date=end) bgneal@540: bgneal@744: self.assertFalse(c.can_enter()) bgneal@540: bgneal@540: start = now - datetime.timedelta(days=7) bgneal@540: end = start + datetime.timedelta(days=30) bgneal@540: bgneal@540: c = Contest(title='test', bgneal@540: slug='test', bgneal@540: description='test', bgneal@540: is_public=True, bgneal@540: creation_date=start, bgneal@540: end_date=end) bgneal@540: bgneal@540: self.assertTrue(c.can_enter()) bgneal@540: bgneal@540: start = now - datetime.timedelta(days=7) bgneal@540: end = start - datetime.timedelta(days=3) bgneal@540: bgneal@540: c = Contest(title='test', bgneal@540: slug='test', bgneal@540: description='test', bgneal@540: is_public=True, bgneal@540: creation_date=start, bgneal@540: end_date=end) bgneal@540: bgneal@744: self.assertFalse(c.can_enter()) bgneal@540: bgneal@540: start = now - datetime.timedelta(days=7) bgneal@540: end = start + datetime.timedelta(days=30) bgneal@540: bgneal@540: user = User.objects.create_user('test_user', '', 'password') bgneal@540: user.save() bgneal@540: bgneal@540: c = Contest(title='test', bgneal@540: slug='test', bgneal@540: description='test', bgneal@540: is_public=True, bgneal@540: creation_date=start, bgneal@540: end_date=end, bgneal@540: win_date=now) bgneal@796: c.save() bgneal@796: c.contestants.add(user) bgneal@796: c.winners.add(user) bgneal@540: bgneal@744: self.assertFalse(c.can_enter()) bgneal@540: bgneal@540: start = now - datetime.timedelta(days=7) bgneal@540: end = start - datetime.timedelta(days=3) bgneal@540: bgneal@796: c = Contest(title='test 2', bgneal@796: slug='test-2', bgneal@796: description='test 2', bgneal@540: is_public=True, bgneal@540: creation_date=start, bgneal@540: end_date=end, bgneal@540: win_date=end + datetime.timedelta(days=1)) bgneal@796: c.save() bgneal@796: c.contestants.add(user) bgneal@796: c.winners.add(user) bgneal@540: bgneal@744: self.assertFalse(c.can_enter()) bgneal@540: bgneal@796: def test_pick_winners(self): bgneal@540: bgneal@540: now = datetime.datetime.now() bgneal@540: start = now - datetime.timedelta(days=7) bgneal@540: end = start - datetime.timedelta(days=3) bgneal@540: bgneal@540: c = Contest(title='test', bgneal@540: slug='test', bgneal@540: description='test', bgneal@540: is_public=False, bgneal@540: creation_date=start, bgneal@796: end_date=end, bgneal@796: num_winners=1) bgneal@540: c.save() bgneal@540: bgneal@540: user1 = User.objects.create_user('test_user1', '', 'password') bgneal@540: user1.save() bgneal@540: user2 = User.objects.create_user('test_user2', '', 'password') bgneal@540: user2.save() bgneal@540: user3 = User.objects.create_user('test_user3', '', 'password') bgneal@540: user3.save() bgneal@540: bgneal@540: c.contestants.add(user1, user2, user3) bgneal@540: bgneal@796: c.pick_winners() bgneal@540: bgneal@540: self.assertTrue(datetime.datetime.now() - c.win_date < bgneal@540: datetime.timedelta(seconds=1)) bgneal@540: bgneal@796: winners = list(c.winners.all()) bgneal@796: self.assertEqual(len(winners), 1) bgneal@796: self.assertTrue(winners[0].id in [user1.id, user2.id, user3.id]) bgneal@796: bgneal@796: def test_pick_winners2(self): bgneal@796: bgneal@796: now = datetime.datetime.now() bgneal@796: start = now - datetime.timedelta(days=7) bgneal@796: end = start - datetime.timedelta(days=3) bgneal@796: bgneal@796: c = Contest(title='test', bgneal@796: slug='test', bgneal@796: description='test', bgneal@796: is_public=False, bgneal@796: creation_date=start, bgneal@796: end_date=end, bgneal@796: num_winners=2) bgneal@796: c.save() bgneal@796: bgneal@796: user1 = User.objects.create_user('test_user1', '', 'password') bgneal@796: user1.save() bgneal@796: user2 = User.objects.create_user('test_user2', '', 'password') bgneal@796: user2.save() bgneal@796: user3 = User.objects.create_user('test_user3', '', 'password') bgneal@796: user3.save() bgneal@796: bgneal@796: c.contestants.add(user1, user2, user3) bgneal@796: bgneal@796: c.pick_winners() bgneal@796: bgneal@796: self.assertTrue(datetime.datetime.now() - c.win_date < bgneal@796: datetime.timedelta(seconds=1)) bgneal@796: bgneal@796: winners = list(c.winners.all()) bgneal@796: self.assertEqual(len(winners), 2) bgneal@796: bgneal@796: user_ids = set([user1.id, user2.id, user3.id]) bgneal@796: self.assertTrue(winners[0].id in user_ids) bgneal@796: self.assertTrue(winners[1].id in user_ids) bgneal@796: self.assertTrue(winners[0].id != winners[1].id) bgneal@796: