annotate contests/tests/model_tests.py @ 694:d84aaf239182

For #53, use HTML5 DOCTYPE. Some markup cleanup also.
author Brian Neal <bgneal@gmail.com>
date Fri, 06 Sep 2013 21:50:53 -0500
parents ee87ea74d46b
children
rev   line source
bgneal@540 1 """
bgneal@540 2 Model tests for the contests application.
bgneal@540 3
bgneal@540 4 """
bgneal@540 5 import datetime
bgneal@540 6
bgneal@540 7 from django.test import TestCase
bgneal@540 8 from django.contrib.auth.models import User
bgneal@540 9
bgneal@540 10 from contests.models import Contest
bgneal@540 11
bgneal@540 12
bgneal@540 13 class ContestTestCase(TestCase):
bgneal@540 14
bgneal@540 15 def test_creation_date(self):
bgneal@540 16
bgneal@540 17 c = Contest(title='test',
bgneal@540 18 slug='test',
bgneal@540 19 description='test',
bgneal@540 20 is_public=False,
bgneal@540 21 end_date=datetime.datetime.now() + datetime.timedelta(days=30))
bgneal@540 22
bgneal@540 23 c.save()
bgneal@540 24
bgneal@540 25 self.assertTrue(c.creation_date)
bgneal@540 26 self.assertTrue(datetime.datetime.now() - c.creation_date <
bgneal@540 27 datetime.timedelta(seconds=1))
bgneal@540 28
bgneal@540 29 def test_is_active(self):
bgneal@540 30
bgneal@540 31 now = datetime.datetime.now()
bgneal@540 32 start = now + datetime.timedelta(days=7)
bgneal@540 33 end = start + datetime.timedelta(days=30)
bgneal@540 34
bgneal@540 35 c = Contest(title='test',
bgneal@540 36 slug='test',
bgneal@540 37 description='test',
bgneal@540 38 is_public=False,
bgneal@540 39 creation_date=start,
bgneal@540 40 end_date=end)
bgneal@540 41
bgneal@540 42 self.failIf(c.is_active())
bgneal@540 43
bgneal@540 44 start = now - datetime.timedelta(days=7)
bgneal@540 45 end = start + datetime.timedelta(days=30)
bgneal@540 46
bgneal@540 47 c = Contest(title='test',
bgneal@540 48 slug='test',
bgneal@540 49 description='test',
bgneal@540 50 is_public=True,
bgneal@540 51 creation_date=start,
bgneal@540 52 end_date=end)
bgneal@540 53
bgneal@540 54 self.assertTrue(c.is_active())
bgneal@540 55
bgneal@540 56 start = now - datetime.timedelta(days=7)
bgneal@540 57 end = start - datetime.timedelta(days=3)
bgneal@540 58
bgneal@540 59 c = Contest(title='test',
bgneal@540 60 slug='test',
bgneal@540 61 description='test',
bgneal@540 62 is_public=True,
bgneal@540 63 creation_date=start,
bgneal@540 64 end_date=end)
bgneal@540 65
bgneal@540 66 self.failIf(c.is_active())
bgneal@540 67
bgneal@540 68 def test_can_enter(self):
bgneal@540 69
bgneal@540 70 now = datetime.datetime.now()
bgneal@540 71 start = now + datetime.timedelta(days=7)
bgneal@540 72 end = start + datetime.timedelta(days=30)
bgneal@540 73
bgneal@540 74 c = Contest(title='test',
bgneal@540 75 slug='test',
bgneal@540 76 description='test',
bgneal@540 77 is_public=False,
bgneal@540 78 creation_date=start,
bgneal@540 79 end_date=end)
bgneal@540 80
bgneal@540 81 self.failIf(c.can_enter())
bgneal@540 82
bgneal@540 83 start = now - datetime.timedelta(days=7)
bgneal@540 84 end = start + datetime.timedelta(days=30)
bgneal@540 85
bgneal@540 86 c = Contest(title='test',
bgneal@540 87 slug='test',
bgneal@540 88 description='test',
bgneal@540 89 is_public=True,
bgneal@540 90 creation_date=start,
bgneal@540 91 end_date=end)
bgneal@540 92
bgneal@540 93 self.assertTrue(c.can_enter())
bgneal@540 94
bgneal@540 95 start = now - datetime.timedelta(days=7)
bgneal@540 96 end = start - datetime.timedelta(days=3)
bgneal@540 97
bgneal@540 98 c = Contest(title='test',
bgneal@540 99 slug='test',
bgneal@540 100 description='test',
bgneal@540 101 is_public=True,
bgneal@540 102 creation_date=start,
bgneal@540 103 end_date=end)
bgneal@540 104
bgneal@540 105 self.failIf(c.can_enter())
bgneal@540 106
bgneal@540 107 start = now - datetime.timedelta(days=7)
bgneal@540 108 end = start + datetime.timedelta(days=30)
bgneal@540 109
bgneal@540 110 user = User.objects.create_user('test_user', '', 'password')
bgneal@540 111 user.save()
bgneal@540 112
bgneal@540 113 c = Contest(title='test',
bgneal@540 114 slug='test',
bgneal@540 115 description='test',
bgneal@540 116 is_public=True,
bgneal@540 117 creation_date=start,
bgneal@540 118 end_date=end,
bgneal@540 119 winner=user,
bgneal@540 120 win_date=now)
bgneal@540 121
bgneal@540 122 self.failIf(c.can_enter())
bgneal@540 123
bgneal@540 124 start = now - datetime.timedelta(days=7)
bgneal@540 125 end = start - datetime.timedelta(days=3)
bgneal@540 126
bgneal@540 127 c = Contest(title='test',
bgneal@540 128 slug='test',
bgneal@540 129 description='test',
bgneal@540 130 is_public=True,
bgneal@540 131 creation_date=start,
bgneal@540 132 end_date=end,
bgneal@540 133 winner=user,
bgneal@540 134 win_date=end + datetime.timedelta(days=1))
bgneal@540 135
bgneal@540 136 self.failIf(c.can_enter())
bgneal@540 137
bgneal@540 138 def test_pick_winner(self):
bgneal@540 139
bgneal@540 140 now = datetime.datetime.now()
bgneal@540 141 start = now - datetime.timedelta(days=7)
bgneal@540 142 end = start - datetime.timedelta(days=3)
bgneal@540 143
bgneal@540 144 c = Contest(title='test',
bgneal@540 145 slug='test',
bgneal@540 146 description='test',
bgneal@540 147 is_public=False,
bgneal@540 148 creation_date=start,
bgneal@540 149 end_date=end)
bgneal@540 150 c.save()
bgneal@540 151
bgneal@540 152 user1 = User.objects.create_user('test_user1', '', 'password')
bgneal@540 153 user1.save()
bgneal@540 154 user2 = User.objects.create_user('test_user2', '', 'password')
bgneal@540 155 user2.save()
bgneal@540 156 user3 = User.objects.create_user('test_user3', '', 'password')
bgneal@540 157 user3.save()
bgneal@540 158
bgneal@540 159 c.contestants.add(user1, user2, user3)
bgneal@540 160
bgneal@540 161 c.pick_winner()
bgneal@540 162
bgneal@540 163 self.assertTrue(datetime.datetime.now() - c.win_date <
bgneal@540 164 datetime.timedelta(seconds=1))
bgneal@540 165 self.assertTrue(c.winner.id in [user1.id, user2.id, user3.id])
bgneal@540 166