##// END OF EJS Templates
Use django's test case that works with database correctry without the need to flush transactions manually. Removed some workarounds that were used before
neko259 -
r345:ccd7caf6 default
parent child Browse files
Show More
@@ -1,182 +1,175 b''
1 # coding=utf-8
1 # coding=utf-8
2 from django.utils.unittest import TestCase
2 from django.test import TestCase
3 from django.test.client import Client
3 from django.test.client import Client
4 import time
4 import time
5
5
6 from boards.models import Post, Tag
6 from boards.models import Post, Tag
7 from neboard import settings
7 from neboard import settings
8
8
9 PAGE_404 = 'boards/404.html'
9 PAGE_404 = 'boards/404.html'
10
10
11 TEST_TEXT = 'test text'
11 TEST_TEXT = 'test text'
12
12
13 NEW_THREAD_PAGE = '/'
13 NEW_THREAD_PAGE = '/'
14 THREAD_PAGE_ONE = '/thread/1/'
14 THREAD_PAGE_ONE = '/thread/1/'
15 THREAD_PAGE = '/thread/'
15 THREAD_PAGE = '/thread/'
16 TAG_PAGE = '/tag/'
16 TAG_PAGE = '/tag/'
17 HTTP_CODE_REDIRECT = 302
17 HTTP_CODE_REDIRECT = 302
18 HTTP_CODE_OK = 200
18 HTTP_CODE_OK = 200
19 HTTP_CODE_NOT_FOUND = 404
19 HTTP_CODE_NOT_FOUND = 404
20
20
21
21
22 class BoardTests(TestCase):
22 class BoardTests(TestCase):
23
23
24 def _create_post(self):
24 def _create_post(self):
25 return Post.objects.create_post(title='title',
25 return Post.objects.create_post(title='title',
26 text='text')
26 text='text')
27
27
28 def test_post_add(self):
28 def test_post_add(self):
29 post = self._create_post()
29 post = self._create_post()
30
30
31 self.assertIsNotNone(post)
31 self.assertIsNotNone(post)
32 self.assertIsNone(post.thread, 'Opening post has a thread')
32 self.assertIsNone(post.thread, 'Opening post has a thread')
33
33
34 def test_delete_post(self):
34 def test_delete_post(self):
35 post = self._create_post()
35 post = self._create_post()
36 post_id = post.id
36 post_id = post.id
37
37
38 Post.objects.delete_post(post)
38 Post.objects.delete_post(post)
39
39
40 self.assertFalse(Post.objects.exists(post_id))
40 self.assertFalse(Post.objects.exists(post_id))
41
41
42 def test_delete_posts_by_ip(self):
42 def test_delete_posts_by_ip(self):
43 post = self._create_post()
43 post = self._create_post()
44 post_id = post.id
44 post_id = post.id
45
45
46 Post.objects.delete_posts_by_ip('0.0.0.0')
46 Post.objects.delete_posts_by_ip('0.0.0.0')
47
47
48 self.assertFalse(Post.objects.exists(post_id))
48 self.assertFalse(Post.objects.exists(post_id))
49
49
50 # Authentication tests
51
52 def test_get_thread(self):
50 def test_get_thread(self):
53 opening_post = self._create_post()
51 opening_post = self._create_post()
54
52
55 for i in range(0, 2):
53 for i in range(0, 2):
56 Post.objects.create_post('title', 'text', thread=opening_post)
54 Post.objects.create_post('title', 'text', thread=opening_post)
57
55
58 thread = Post.objects.get_thread(opening_post.id)
56 thread = Post.objects.get_thread(opening_post.id)
59
57
60 self.assertEqual(3, len(thread))
58 self.assertEqual(3, len(thread))
61
59
62 def test_create_post_with_tag(self):
60 def test_create_post_with_tag(self):
63 tag = Tag.objects.create(name='test_tag')
61 tag = Tag.objects.create(name='test_tag')
64 post = Post.objects.create_post(title='title', text='text', tags=[tag])
62 post = Post.objects.create_post(title='title', text='text', tags=[tag])
65 self.assertIsNotNone(post)
63 self.assertIsNotNone(post)
66
64
67 def test_thread_max_count(self):
65 def test_thread_max_count(self):
68 for i in range(settings.MAX_THREAD_COUNT + 1):
66 for i in range(settings.MAX_THREAD_COUNT + 1):
69 self._create_post()
67 self._create_post()
70
68
71 self.assertEqual(settings.MAX_THREAD_COUNT,
69 self.assertEqual(settings.MAX_THREAD_COUNT,
72 len(Post.objects.get_threads()))
70 len(Post.objects.get_threads()))
73
71
74 def test_pages(self):
72 def test_pages(self):
75 """Test that the thread list is properly split into pages"""
73 """Test that the thread list is properly split into pages"""
76
74
77 for i in range(settings.MAX_THREAD_COUNT):
75 for i in range(settings.MAX_THREAD_COUNT):
78 self._create_post()
76 self._create_post()
79
77
80 all_threads = Post.objects.get_threads()
78 all_threads = Post.objects.get_threads()
81
79
82 posts_in_second_page = Post.objects.get_threads(page=1)
80 posts_in_second_page = Post.objects.get_threads(page=1)
83 first_post = posts_in_second_page[0]
81 first_post = posts_in_second_page[0]
84
82
85 self.assertEqual(all_threads[settings.THREADS_PER_PAGE].id,
83 self.assertEqual(all_threads[settings.THREADS_PER_PAGE].id,
86 first_post.id)
84 first_post.id)
87
85
88 def test_post_validation(self):
86 def test_post_validation(self):
89 """Test the validation of the post form"""
87 """Test the validation of the post form"""
90
88
91 # Disable captcha for the test
89 # Disable captcha for the test
92 captcha_enabled = settings.ENABLE_CAPTCHA
90 captcha_enabled = settings.ENABLE_CAPTCHA
93 settings.ENABLE_CAPTCHA = False
91 settings.ENABLE_CAPTCHA = False
94
92
95 Post.objects.all().delete()
96
97 client = Client()
93 client = Client()
98
94
99 valid_tags = u'tag1 tag_2 Ρ‚Π΅Π³_3'
95 valid_tags = u'tag1 tag_2 Ρ‚Π΅Π³_3'
100 invalid_tags = u'$%_356 ---'
96 invalid_tags = u'$%_356 ---'
101
97
102 response = client.post(NEW_THREAD_PAGE, {'title': 'test title',
98 response = client.post(NEW_THREAD_PAGE, {'title': 'test title',
103 'text': TEST_TEXT,
99 'text': TEST_TEXT,
104 'tags': valid_tags})
100 'tags': valid_tags})
105 self.assertEqual(response.status_code, HTTP_CODE_REDIRECT,
101 self.assertEqual(response.status_code, HTTP_CODE_REDIRECT,
106 msg='Posting new message failed: got code ' +
102 msg='Posting new message failed: got code ' +
107 str(response.status_code))
103 str(response.status_code))
108
104
109 self.assertEqual(1, Post.objects.count(),
105 self.assertEqual(1, Post.objects.count(),
110 msg='No posts were created')
106 msg='No posts were created')
111
107
112 client.post(NEW_THREAD_PAGE, {'text': TEST_TEXT,
108 client.post(NEW_THREAD_PAGE, {'text': TEST_TEXT,
113 'tags': invalid_tags})
109 'tags': invalid_tags})
114 self.assertEqual(1, Post.objects.count(), msg='The validation passed '
110 self.assertEqual(1, Post.objects.count(), msg='The validation passed '
115 'where it should fail')
111 'where it should fail')
116
112
117 # Change posting delay so we don't have to wait for 30 seconds or more
113 # Change posting delay so we don't have to wait for 30 seconds or more
118 old_posting_delay = settings.POSTING_DELAY
114 old_posting_delay = settings.POSTING_DELAY
119 # Wait fot the posting delay or we won't be able to post
115 # Wait fot the posting delay or we won't be able to post
120 settings.POSTING_DELAY = 1
116 settings.POSTING_DELAY = 1
121 time.sleep(settings.POSTING_DELAY + 1)
117 time.sleep(settings.POSTING_DELAY + 1)
122 response = client.post(THREAD_PAGE_ONE, {'text': TEST_TEXT,
118 response = client.post(THREAD_PAGE_ONE, {'text': TEST_TEXT,
123 'tags': valid_tags})
119 'tags': valid_tags})
124 self.assertEqual(HTTP_CODE_REDIRECT, response.status_code,
120 self.assertEqual(HTTP_CODE_REDIRECT, response.status_code,
125 msg=u'Posting new message failed: got code ' +
121 msg=u'Posting new message failed: got code ' +
126 str(response.status_code))
122 str(response.status_code))
127 # Restore posting delay
123 # Restore posting delay
128 settings.POSTING_DELAY = old_posting_delay
124 settings.POSTING_DELAY = old_posting_delay
129
125
130 self.assertEqual(2, Post.objects.count(),
126 self.assertEqual(2, Post.objects.count(),
131 msg=u'No posts were created')
127 msg=u'No posts were created')
132
128
133 # Restore captcha setting
129 # Restore captcha setting
134 settings.ENABLE_CAPTCHA = captcha_enabled
130 settings.ENABLE_CAPTCHA = captcha_enabled
135
131
136 def test_404(self):
132 def test_404(self):
137 """Test receiving error 404 when opening a non-existent page"""
133 """Test receiving error 404 when opening a non-existent page"""
138
134
139 Post.objects.all().delete()
140 Tag.objects.all().delete()
141
142 tag_name = u'test_tag'
135 tag_name = u'test_tag'
143 tag = Tag.objects.create(name=tag_name)
136 tag = Tag.objects.create(name=tag_name)
144 client = Client()
137 client = Client()
145
138
146 Post.objects.create_post('title', TEST_TEXT, tags=[tag])
139 Post.objects.create_post('title', TEST_TEXT, tags=[tag])
147
140
148 existing_post_id = Post.objects.all()[0].id
141 existing_post_id = Post.objects.all()[0].id
149 response_existing = client.get(THREAD_PAGE + str(existing_post_id) +
142 response_existing = client.get(THREAD_PAGE + str(existing_post_id) +
150 '/')
143 '/')
151 self.assertEqual(HTTP_CODE_OK, response_existing.status_code,
144 self.assertEqual(HTTP_CODE_OK, response_existing.status_code,
152 u'Cannot open existing thread')
145 u'Cannot open existing thread')
153
146
154 response_not_existing = client.get(THREAD_PAGE + str(
147 response_not_existing = client.get(THREAD_PAGE + str(
155 existing_post_id + 1) + '/')
148 existing_post_id + 1) + '/')
156 self.assertEqual(PAGE_404,
149 self.assertEqual(PAGE_404,
157 response_not_existing.templates[0].name,
150 response_not_existing.templates[0].name,
158 u'Not existing thread is opened')
151 u'Not existing thread is opened')
159
152
160 response_existing = client.get(TAG_PAGE + tag_name + '/')
153 response_existing = client.get(TAG_PAGE + tag_name + '/')
161 self.assertEqual(HTTP_CODE_OK,
154 self.assertEqual(HTTP_CODE_OK,
162 response_existing.status_code,
155 response_existing.status_code,
163 u'Cannot open existing tag')
156 u'Cannot open existing tag')
164
157
165 response_not_existing = client.get(TAG_PAGE + u'not_tag' + '/')
158 response_not_existing = client.get(TAG_PAGE + u'not_tag' + '/')
166 self.assertEqual(PAGE_404,
159 self.assertEqual(PAGE_404,
167 response_not_existing.templates[0].name,
160 response_not_existing.templates[0].name,
168 u'Not existing tag is opened')
161 u'Not existing tag is opened')
169
162
170 reply_id = Post.objects.create_post('', TEST_TEXT,
163 reply_id = Post.objects.create_post('', TEST_TEXT,
171 thread=Post.objects.all()[0])
164 thread=Post.objects.all()[0])
172 response_not_existing = client.get(THREAD_PAGE + str(
165 response_not_existing = client.get(THREAD_PAGE + str(
173 reply_id) + '/')
166 reply_id) + '/')
174 self.assertEqual(PAGE_404,
167 self.assertEqual(PAGE_404,
175 response_not_existing.templates[0].name,
168 response_not_existing.templates[0].name,
176 u'Reply is opened as a thread')
169 u'Reply is opened as a thread')
177
170
178 def test_linked_tag(self):
171 def test_linked_tag(self):
179 tag = Tag.objects.create(name=u'tag1')
172 tag = Tag.objects.create(name=u'tag1')
180 linked_tag = Tag.objects.create(name=u'tag2', linked=tag)
173 linked_tag = Tag.objects.create(name=u'tag2', linked=tag)
181
174
182 # TODO run add post view and check the tag is added No newline at end of file
175 # TODO run add post view and check the tag is added
General Comments 0
You need to be logged in to leave comments. Login now