##// END OF EJS Templates
Moved adding linked posts from view to post manager. Cleaned up tests, added some more tests
neko259 -
r381:f21d714a default
parent child Browse files
Show More
@@ -61,6 +61,13 b' class PostManager(models.Manager):'
61 user=user)
61 user=user)
62
62
63 if tags:
63 if tags:
64 linked_tags = []
65 for tag in tags:
66 tag_linked_tags = tag.get_linked_tags()
67 if len(tag_linked_tags) > 0:
68 linked_tags.extend(tag_linked_tags)
69
70 tags.extend(linked_tags)
64 map(post.tags.add, tags)
71 map(post.tags.add, tags)
65 for tag in tags:
72 for tag in tags:
66 tag.threads.add(post)
73 tag.threads.add(post)
@@ -19,19 +19,23 b' 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 PostTests(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 """Test adding post"""
30
29 post = self._create_post()
31 post = self._create_post()
30
32
31 self.assertIsNotNone(post)
33 self.assertIsNotNone(post)
32 self.assertIsNone(post.thread, 'Opening post has a thread')
34 self.assertIsNone(post.thread, 'Opening post has a thread')
33
35
34 def test_delete_post(self):
36 def test_delete_post(self):
37 """Test post deletion"""
38
35 post = self._create_post()
39 post = self._create_post()
36 post_id = post.id
40 post_id = post.id
37
41
@@ -39,7 +43,20 b' class BoardTests(TestCase):'
39
43
40 self.assertFalse(Post.objects.exists(post_id))
44 self.assertFalse(Post.objects.exists(post_id))
41
45
46 def test_delete_thread(self):
47 """Test thread deletion"""
48
49 thread = self._create_post()
50 reply = Post.objects.create_post("", "", thread=thread)
51
52 Post.objects.delete_post(thread)
53
54 self.assertFalse(Post.objects.exists(reply.id))
55
56
42 def test_post_to_thread(self):
57 def test_post_to_thread(self):
58 """Test adding post to a thread"""
59
43 op = self._create_post()
60 op = self._create_post()
44 post = Post.objects.create_post("", "", thread=op)
61 post = Post.objects.create_post("", "", thread=op)
45
62
@@ -49,6 +66,8 b' class BoardTests(TestCase):'
49 ' time')
66 ' time')
50
67
51 def test_delete_posts_by_ip(self):
68 def test_delete_posts_by_ip(self):
69 """Test deleting posts with the given ip"""
70
52 post = self._create_post()
71 post = self._create_post()
53 post_id = post.id
72 post_id = post.id
54
73
@@ -57,6 +76,8 b' class BoardTests(TestCase):'
57 self.assertFalse(Post.objects.exists(post_id))
76 self.assertFalse(Post.objects.exists(post_id))
58
77
59 def test_get_thread(self):
78 def test_get_thread(self):
79 """Test getting all posts of a thread"""
80
60 opening_post = self._create_post()
81 opening_post = self._create_post()
61
82
62 for i in range(0, 2):
83 for i in range(0, 2):
@@ -67,11 +88,15 b' class BoardTests(TestCase):'
67 self.assertEqual(3, len(thread))
88 self.assertEqual(3, len(thread))
68
89
69 def test_create_post_with_tag(self):
90 def test_create_post_with_tag(self):
91 """Test adding tag to post"""
92
70 tag = Tag.objects.create(name='test_tag')
93 tag = Tag.objects.create(name='test_tag')
71 post = Post.objects.create_post(title='title', text='text', tags=[tag])
94 post = Post.objects.create_post(title='title', text='text', tags=[tag])
72 self.assertIsNotNone(post)
95 self.assertIsNotNone(post)
73
96
74 def test_thread_max_count(self):
97 def test_thread_max_count(self):
98 """Test deletion of old posts when the max thread count is reached"""
99
75 for i in range(settings.MAX_THREAD_COUNT + 1):
100 for i in range(settings.MAX_THREAD_COUNT + 1):
76 self._create_post()
101 self._create_post()
77
102
@@ -92,6 +117,61 b' class BoardTests(TestCase):'
92 self.assertEqual(all_threads[settings.THREADS_PER_PAGE].id,
117 self.assertEqual(all_threads[settings.THREADS_PER_PAGE].id,
93 first_post.id)
118 first_post.id)
94
119
120 def test_linked_tag(self):
121 """Test adding a linked tag"""
122
123 linked_tag = Tag.objects.create(name=u'tag1')
124 tag = Tag.objects.create(name=u'tag2', linked=linked_tag)
125
126 post = Post.objects.create_post("", "", tags=[tag])
127
128 self.assertTrue(linked_tag in post.tags.all(),
129 'Linked tag was not added')
130
131
132 class PagesTest(TestCase):
133
134 def test_404(self):
135 """Test receiving error 404 when opening a non-existent page"""
136
137 tag_name = u'test_tag'
138 tag = Tag.objects.create(name=tag_name)
139 client = Client()
140
141 Post.objects.create_post('title', TEST_TEXT, tags=[tag])
142
143 existing_post_id = Post.objects.all()[0].id
144 response_existing = client.get(THREAD_PAGE + str(existing_post_id) +
145 '/')
146 self.assertEqual(HTTP_CODE_OK, response_existing.status_code,
147 u'Cannot open existing thread')
148
149 response_not_existing = client.get(THREAD_PAGE + str(
150 existing_post_id + 1) + '/')
151 self.assertEqual(PAGE_404,
152 response_not_existing.templates[0].name,
153 u'Not existing thread is opened')
154
155 response_existing = client.get(TAG_PAGE + tag_name + '/')
156 self.assertEqual(HTTP_CODE_OK,
157 response_existing.status_code,
158 u'Cannot open existing tag')
159
160 response_not_existing = client.get(TAG_PAGE + u'not_tag' + '/')
161 self.assertEqual(PAGE_404,
162 response_not_existing.templates[0].name,
163 u'Not existing tag is opened')
164
165 reply_id = Post.objects.create_post('', TEST_TEXT,
166 thread=Post.objects.all()[0])
167 response_not_existing = client.get(THREAD_PAGE + str(
168 reply_id) + '/')
169 self.assertEqual(PAGE_404,
170 response_not_existing.templates[0].name,
171 u'Reply is opened as a thread')
172
173
174 class FormTest(TestCase):
95 def test_post_validation(self):
175 def test_post_validation(self):
96 """Test the validation of the post form"""
176 """Test the validation of the post form"""
97
177
@@ -137,48 +217,3 b' class BoardTests(TestCase):'
137
217
138 # Restore captcha setting
218 # Restore captcha setting
139 settings.ENABLE_CAPTCHA = captcha_enabled
219 settings.ENABLE_CAPTCHA = captcha_enabled
140
141 def test_404(self):
142 """Test receiving error 404 when opening a non-existent page"""
143
144 tag_name = u'test_tag'
145 tag = Tag.objects.create(name=tag_name)
146 client = Client()
147
148 Post.objects.create_post('title', TEST_TEXT, tags=[tag])
149
150 existing_post_id = Post.objects.all()[0].id
151 response_existing = client.get(THREAD_PAGE + str(existing_post_id) +
152 '/')
153 self.assertEqual(HTTP_CODE_OK, response_existing.status_code,
154 u'Cannot open existing thread')
155
156 response_not_existing = client.get(THREAD_PAGE + str(
157 existing_post_id + 1) + '/')
158 self.assertEqual(PAGE_404,
159 response_not_existing.templates[0].name,
160 u'Not existing thread is opened')
161
162 response_existing = client.get(TAG_PAGE + tag_name + '/')
163 self.assertEqual(HTTP_CODE_OK,
164 response_existing.status_code,
165 u'Cannot open existing tag')
166
167 response_not_existing = client.get(TAG_PAGE + u'not_tag' + '/')
168 self.assertEqual(PAGE_404,
169 response_not_existing.templates[0].name,
170 u'Not existing tag is opened')
171
172 reply_id = Post.objects.create_post('', TEST_TEXT,
173 thread=Post.objects.all()[0])
174 response_not_existing = client.get(THREAD_PAGE + str(
175 reply_id) + '/')
176 self.assertEqual(PAGE_404,
177 response_not_existing.templates[0].name,
178 u'Reply is opened as a thread')
179
180 def test_linked_tag(self):
181 tag = Tag.objects.create(name=u'tag1')
182 linked_tag = Tag.objects.create(name=u'tag2', linked=tag)
183
184 # TODO run add post view and check the tag is added No newline at end of file
@@ -115,10 +115,6 b' def _new_post(request, form, thread_id=b'
115 tag, created = Tag.objects.get_or_create(name=tag_name)
115 tag, created = Tag.objects.get_or_create(name=tag_name)
116 tags.append(tag)
116 tags.append(tag)
117
117
118 linked_tags = tag.get_linked_tags()
119 if len(linked_tags) > 0:
120 tags.extend(linked_tags)
121
122 op = None if thread_id == boards.models.NO_PARENT else \
118 op = None if thread_id == boards.models.NO_PARENT else \
123 get_object_or_404(Post, id=thread_id)
119 get_object_or_404(Post, id=thread_id)
124 post = Post.objects.create_post(title=title, text=text, ip=ip,
120 post = Post.objects.create_post(title=title, text=text, ip=ip,
General Comments 0
You need to be logged in to leave comments. Login now