comparison contests/tests/view_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/view_tests.py@51fa1e0ca218
children 89b240fe9297
comparison
equal deleted inserted replaced
580:c525f3e0b5d0 581:ee87ea74d46b
1 """
2 View tests for the contests application.
3
4 """
5 import datetime
6 from django.test import TestCase
7 from django.contrib.auth.models import User
8 from django.core.urlresolvers import reverse
9 from django.utils import simplejson
10
11 from contests.models import Contest
12
13
14 class NoConstestsTestCase(TestCase):
15
16 def test_no_contests(self):
17 response = self.client.get(reverse('contests-index'))
18 self.assertEqual(response.status_code, 200)
19
20 url = reverse('contests-contest', kwargs={'slug': 'test'})
21 response = self.client.get(url)
22 self.assertEqual(response.status_code, 404)
23
24
25 class ConstestsTestCase(TestCase):
26
27 def setUp(self):
28 now = datetime.datetime.now()
29 start = now - datetime.timedelta(days=7)
30 end = start - datetime.timedelta(days=3)
31
32 user = User.objects.create_user('test_user', '', 'password')
33 user.save()
34
35 c = Contest(title='test',
36 slug='test',
37 description='test',
38 is_public=True,
39 creation_date=start,
40 end_date=end,
41 winner=user,
42 win_date=end + datetime.timedelta(days=1))
43 c.save()
44 self.contest_id = c.id
45
46 def test_contests(self):
47 response = self.client.get(reverse('contests-index'))
48 self.assertEqual(response.status_code, 200)
49
50 url = reverse('contests-contest', kwargs={'slug': 'test'})
51 response = self.client.get(url)
52 self.assertEqual(response.status_code, 200)
53
54
55 class ContestEntryTestCase(TestCase):
56
57 def setUp(self):
58 self.username = 'test_user'
59 self.pw = 'password'
60 self.user = User.objects.create_user(self.username, '', self.pw)
61 self.user.save()
62 self.assertTrue(self.client.login(username=self.username,
63 password=self.pw))
64
65 now = datetime.datetime.now()
66 start = now - datetime.timedelta(days=7)
67 end = now + datetime.timedelta(days=3)
68
69 c = Contest(title='test',
70 slug='test',
71 description='test',
72 is_public=True,
73 creation_date=start,
74 end_date=end)
75 c.save()
76 self.contest_id = c.id
77
78 def test_entry_toggle(self):
79 response = self.client.post(reverse('contests-enter'),
80 {'contest_id': self.contest_id},
81 HTTP_X_REQUESTED_WITH='XMLHttpRequest')
82 self.assertEqual(response.status_code, 200)
83
84 json = simplejson.loads(response.content)
85 self.assertTrue(json['entered'])
86
87 contest = Contest.objects.get(pk=self.contest_id)
88 self.assertTrue(self.user in contest.contestants.all())
89
90 response = self.client.post(reverse('contests-enter'),
91 {'contest_id': self.contest_id},
92 HTTP_X_REQUESTED_WITH='XMLHttpRequest')
93 self.assertEqual(response.status_code, 200)
94
95 json = simplejson.loads(response.content)
96 self.failIf(json['entered'])
97
98 contest = Contest.objects.get(pk=self.contest_id)
99 self.failIf(self.user in contest.contestants.all())
100
101
102 class NoPublicConstestsTestCase(TestCase):
103
104 def setUp(self):
105 now = datetime.datetime.now()
106 start = now - datetime.timedelta(days=7)
107 end = start - datetime.timedelta(days=3)
108
109 c = Contest(title='test',
110 slug='test',
111 description='test',
112 is_public=False,
113 creation_date=start,
114 end_date=end)
115 c.save()
116
117 def test_contests(self):
118 response = self.client.get(reverse('contests-index'))
119 self.assertEqual(response.status_code, 200)
120
121 url = reverse('contests-contest', kwargs={'slug': 'test'})
122 response = self.client.get(url)
123 self.assertEqual(response.status_code, 404)