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