comparison contests/tests/model_tests.py @ 581:ee87ea74d46b

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