comparison contests/tests/test_models.py @ 796:5977b43499f7

Modified contests to have multiple winners.
author Brian Neal <bgneal@gmail.com>
date Fri, 04 Jul 2014 20:30:14 -0500
parents 8789299c75b1
children
comparison
equal deleted inserted replaced
795:5d48fd80f27d 796:5977b43499f7
114 slug='test', 114 slug='test',
115 description='test', 115 description='test',
116 is_public=True, 116 is_public=True,
117 creation_date=start, 117 creation_date=start,
118 end_date=end, 118 end_date=end,
119 winner=user,
120 win_date=now) 119 win_date=now)
121 120 c.save()
122 self.assertFalse(c.can_enter()) 121 c.contestants.add(user)
123 122 c.winners.add(user)
124 start = now - datetime.timedelta(days=7) 123
125 end = start - datetime.timedelta(days=3) 124 self.assertFalse(c.can_enter())
126 125
127 c = Contest(title='test', 126 start = now - datetime.timedelta(days=7)
128 slug='test', 127 end = start - datetime.timedelta(days=3)
129 description='test', 128
130 is_public=True, 129 c = Contest(title='test 2',
131 creation_date=start, 130 slug='test-2',
132 end_date=end, 131 description='test 2',
133 winner=user, 132 is_public=True,
133 creation_date=start,
134 end_date=end,
134 win_date=end + datetime.timedelta(days=1)) 135 win_date=end + datetime.timedelta(days=1))
135 136 c.save()
136 self.assertFalse(c.can_enter()) 137 c.contestants.add(user)
137 138 c.winners.add(user)
138 def test_pick_winner(self): 139
139 140 self.assertFalse(c.can_enter())
140 now = datetime.datetime.now() 141
141 start = now - datetime.timedelta(days=7) 142 def test_pick_winners(self):
142 end = start - datetime.timedelta(days=3) 143
143 144 now = datetime.datetime.now()
144 c = Contest(title='test', 145 start = now - datetime.timedelta(days=7)
145 slug='test', 146 end = start - datetime.timedelta(days=3)
146 description='test', 147
147 is_public=False, 148 c = Contest(title='test',
148 creation_date=start, 149 slug='test',
149 end_date=end) 150 description='test',
151 is_public=False,
152 creation_date=start,
153 end_date=end,
154 num_winners=1)
150 c.save() 155 c.save()
151 156
152 user1 = User.objects.create_user('test_user1', '', 'password') 157 user1 = User.objects.create_user('test_user1', '', 'password')
153 user1.save() 158 user1.save()
154 user2 = User.objects.create_user('test_user2', '', 'password') 159 user2 = User.objects.create_user('test_user2', '', 'password')
156 user3 = User.objects.create_user('test_user3', '', 'password') 161 user3 = User.objects.create_user('test_user3', '', 'password')
157 user3.save() 162 user3.save()
158 163
159 c.contestants.add(user1, user2, user3) 164 c.contestants.add(user1, user2, user3)
160 165
161 c.pick_winner() 166 c.pick_winners()
162 167
163 self.assertTrue(datetime.datetime.now() - c.win_date < 168 self.assertTrue(datetime.datetime.now() - c.win_date <
164 datetime.timedelta(seconds=1)) 169 datetime.timedelta(seconds=1))
165 self.assertTrue(c.winner.id in [user1.id, user2.id, user3.id]) 170
166 171 winners = list(c.winners.all())
172 self.assertEqual(len(winners), 1)
173 self.assertTrue(winners[0].id in [user1.id, user2.id, user3.id])
174
175 def test_pick_winners2(self):
176
177 now = datetime.datetime.now()
178 start = now - datetime.timedelta(days=7)
179 end = start - datetime.timedelta(days=3)
180
181 c = Contest(title='test',
182 slug='test',
183 description='test',
184 is_public=False,
185 creation_date=start,
186 end_date=end,
187 num_winners=2)
188 c.save()
189
190 user1 = User.objects.create_user('test_user1', '', 'password')
191 user1.save()
192 user2 = User.objects.create_user('test_user2', '', 'password')
193 user2.save()
194 user3 = User.objects.create_user('test_user3', '', 'password')
195 user3.save()
196
197 c.contestants.add(user1, user2, user3)
198
199 c.pick_winners()
200
201 self.assertTrue(datetime.datetime.now() - c.win_date <
202 datetime.timedelta(seconds=1))
203
204 winners = list(c.winners.all())
205 self.assertEqual(len(winners), 2)
206
207 user_ids = set([user1.id, user2.id, user3.id])
208 self.assertTrue(winners[0].id in user_ids)
209 self.assertTrue(winners[1].id in user_ids)
210 self.assertTrue(winners[0].id != winners[1].id)
211