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: winner=user, bgneal@540: win_date=now) 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@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: winner=user, bgneal@540: win_date=end + datetime.timedelta(days=1)) bgneal@540: bgneal@744: self.assertFalse(c.can_enter()) bgneal@540: bgneal@540: def test_pick_winner(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@540: end_date=end) 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@540: c.pick_winner() bgneal@540: bgneal@540: self.assertTrue(datetime.datetime.now() - c.win_date < bgneal@540: datetime.timedelta(seconds=1)) bgneal@540: self.assertTrue(c.winner.id in [user1.id, user2.id, user3.id]) bgneal@540: