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@744
|
42 self.assertFalse(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@744
|
66 self.assertFalse(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@744
|
81 self.assertFalse(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@744
|
105 self.assertFalse(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 win_date=now)
|
bgneal@796
|
120 c.save()
|
bgneal@796
|
121 c.contestants.add(user)
|
bgneal@796
|
122 c.winners.add(user)
|
bgneal@540
|
123
|
bgneal@744
|
124 self.assertFalse(c.can_enter())
|
bgneal@540
|
125
|
bgneal@540
|
126 start = now - datetime.timedelta(days=7)
|
bgneal@540
|
127 end = start - datetime.timedelta(days=3)
|
bgneal@540
|
128
|
bgneal@796
|
129 c = Contest(title='test 2',
|
bgneal@796
|
130 slug='test-2',
|
bgneal@796
|
131 description='test 2',
|
bgneal@540
|
132 is_public=True,
|
bgneal@540
|
133 creation_date=start,
|
bgneal@540
|
134 end_date=end,
|
bgneal@540
|
135 win_date=end + datetime.timedelta(days=1))
|
bgneal@796
|
136 c.save()
|
bgneal@796
|
137 c.contestants.add(user)
|
bgneal@796
|
138 c.winners.add(user)
|
bgneal@540
|
139
|
bgneal@744
|
140 self.assertFalse(c.can_enter())
|
bgneal@540
|
141
|
bgneal@796
|
142 def test_pick_winners(self):
|
bgneal@540
|
143
|
bgneal@540
|
144 now = datetime.datetime.now()
|
bgneal@540
|
145 start = now - datetime.timedelta(days=7)
|
bgneal@540
|
146 end = start - datetime.timedelta(days=3)
|
bgneal@540
|
147
|
bgneal@540
|
148 c = Contest(title='test',
|
bgneal@540
|
149 slug='test',
|
bgneal@540
|
150 description='test',
|
bgneal@540
|
151 is_public=False,
|
bgneal@540
|
152 creation_date=start,
|
bgneal@796
|
153 end_date=end,
|
bgneal@796
|
154 num_winners=1)
|
bgneal@540
|
155 c.save()
|
bgneal@540
|
156
|
bgneal@540
|
157 user1 = User.objects.create_user('test_user1', '', 'password')
|
bgneal@540
|
158 user1.save()
|
bgneal@540
|
159 user2 = User.objects.create_user('test_user2', '', 'password')
|
bgneal@540
|
160 user2.save()
|
bgneal@540
|
161 user3 = User.objects.create_user('test_user3', '', 'password')
|
bgneal@540
|
162 user3.save()
|
bgneal@540
|
163
|
bgneal@540
|
164 c.contestants.add(user1, user2, user3)
|
bgneal@540
|
165
|
bgneal@796
|
166 c.pick_winners()
|
bgneal@540
|
167
|
bgneal@540
|
168 self.assertTrue(datetime.datetime.now() - c.win_date <
|
bgneal@540
|
169 datetime.timedelta(seconds=1))
|
bgneal@540
|
170
|
bgneal@796
|
171 winners = list(c.winners.all())
|
bgneal@796
|
172 self.assertEqual(len(winners), 1)
|
bgneal@796
|
173 self.assertTrue(winners[0].id in [user1.id, user2.id, user3.id])
|
bgneal@796
|
174
|
bgneal@796
|
175 def test_pick_winners2(self):
|
bgneal@796
|
176
|
bgneal@796
|
177 now = datetime.datetime.now()
|
bgneal@796
|
178 start = now - datetime.timedelta(days=7)
|
bgneal@796
|
179 end = start - datetime.timedelta(days=3)
|
bgneal@796
|
180
|
bgneal@796
|
181 c = Contest(title='test',
|
bgneal@796
|
182 slug='test',
|
bgneal@796
|
183 description='test',
|
bgneal@796
|
184 is_public=False,
|
bgneal@796
|
185 creation_date=start,
|
bgneal@796
|
186 end_date=end,
|
bgneal@796
|
187 num_winners=2)
|
bgneal@796
|
188 c.save()
|
bgneal@796
|
189
|
bgneal@796
|
190 user1 = User.objects.create_user('test_user1', '', 'password')
|
bgneal@796
|
191 user1.save()
|
bgneal@796
|
192 user2 = User.objects.create_user('test_user2', '', 'password')
|
bgneal@796
|
193 user2.save()
|
bgneal@796
|
194 user3 = User.objects.create_user('test_user3', '', 'password')
|
bgneal@796
|
195 user3.save()
|
bgneal@796
|
196
|
bgneal@796
|
197 c.contestants.add(user1, user2, user3)
|
bgneal@796
|
198
|
bgneal@796
|
199 c.pick_winners()
|
bgneal@796
|
200
|
bgneal@796
|
201 self.assertTrue(datetime.datetime.now() - c.win_date <
|
bgneal@796
|
202 datetime.timedelta(seconds=1))
|
bgneal@796
|
203
|
bgneal@796
|
204 winners = list(c.winners.all())
|
bgneal@796
|
205 self.assertEqual(len(winners), 2)
|
bgneal@796
|
206
|
bgneal@796
|
207 user_ids = set([user1.id, user2.id, user3.id])
|
bgneal@796
|
208 self.assertTrue(winners[0].id in user_ids)
|
bgneal@796
|
209 self.assertTrue(winners[1].id in user_ids)
|
bgneal@796
|
210 self.assertTrue(winners[0].id != winners[1].id)
|
bgneal@796
|
211
|