# HG changeset patch # User neko259 # Date 2013-11-17 15:23:16 # Node ID f21d714a312c429fc9efd6d2ffb5d2de1c3bf8a1 # Parent 631bd6582d28062d86e151398fbac2cfd4bcd9de Moved adding linked posts from view to post manager. Cleaned up tests, added some more tests diff --git a/boards/models.py b/boards/models.py --- a/boards/models.py +++ b/boards/models.py @@ -61,6 +61,13 @@ class PostManager(models.Manager): user=user) if tags: + linked_tags = [] + for tag in tags: + tag_linked_tags = tag.get_linked_tags() + if len(tag_linked_tags) > 0: + linked_tags.extend(tag_linked_tags) + + tags.extend(linked_tags) map(post.tags.add, tags) for tag in tags: tag.threads.add(post) diff --git a/boards/tests.py b/boards/tests.py --- a/boards/tests.py +++ b/boards/tests.py @@ -19,19 +19,23 @@ HTTP_CODE_OK = 200 HTTP_CODE_NOT_FOUND = 404 -class BoardTests(TestCase): +class PostTests(TestCase): def _create_post(self): return Post.objects.create_post(title='title', text='text') def test_post_add(self): + """Test adding post""" + post = self._create_post() self.assertIsNotNone(post) self.assertIsNone(post.thread, 'Opening post has a thread') def test_delete_post(self): + """Test post deletion""" + post = self._create_post() post_id = post.id @@ -39,7 +43,20 @@ class BoardTests(TestCase): self.assertFalse(Post.objects.exists(post_id)) + def test_delete_thread(self): + """Test thread deletion""" + + thread = self._create_post() + reply = Post.objects.create_post("", "", thread=thread) + + Post.objects.delete_post(thread) + + self.assertFalse(Post.objects.exists(reply.id)) + + def test_post_to_thread(self): + """Test adding post to a thread""" + op = self._create_post() post = Post.objects.create_post("", "", thread=op) @@ -49,6 +66,8 @@ class BoardTests(TestCase): ' time') def test_delete_posts_by_ip(self): + """Test deleting posts with the given ip""" + post = self._create_post() post_id = post.id @@ -57,6 +76,8 @@ class BoardTests(TestCase): self.assertFalse(Post.objects.exists(post_id)) def test_get_thread(self): + """Test getting all posts of a thread""" + opening_post = self._create_post() for i in range(0, 2): @@ -67,11 +88,15 @@ class BoardTests(TestCase): self.assertEqual(3, len(thread)) def test_create_post_with_tag(self): + """Test adding tag to post""" + tag = Tag.objects.create(name='test_tag') post = Post.objects.create_post(title='title', text='text', tags=[tag]) self.assertIsNotNone(post) def test_thread_max_count(self): + """Test deletion of old posts when the max thread count is reached""" + for i in range(settings.MAX_THREAD_COUNT + 1): self._create_post() @@ -92,6 +117,61 @@ class BoardTests(TestCase): self.assertEqual(all_threads[settings.THREADS_PER_PAGE].id, first_post.id) + def test_linked_tag(self): + """Test adding a linked tag""" + + linked_tag = Tag.objects.create(name=u'tag1') + tag = Tag.objects.create(name=u'tag2', linked=linked_tag) + + post = Post.objects.create_post("", "", tags=[tag]) + + self.assertTrue(linked_tag in post.tags.all(), + 'Linked tag was not added') + + +class PagesTest(TestCase): + + def test_404(self): + """Test receiving error 404 when opening a non-existent page""" + + tag_name = u'test_tag' + tag = Tag.objects.create(name=tag_name) + client = Client() + + Post.objects.create_post('title', TEST_TEXT, tags=[tag]) + + existing_post_id = Post.objects.all()[0].id + response_existing = client.get(THREAD_PAGE + str(existing_post_id) + + '/') + self.assertEqual(HTTP_CODE_OK, response_existing.status_code, + u'Cannot open existing thread') + + response_not_existing = client.get(THREAD_PAGE + str( + existing_post_id + 1) + '/') + self.assertEqual(PAGE_404, + response_not_existing.templates[0].name, + u'Not existing thread is opened') + + response_existing = client.get(TAG_PAGE + tag_name + '/') + self.assertEqual(HTTP_CODE_OK, + response_existing.status_code, + u'Cannot open existing tag') + + response_not_existing = client.get(TAG_PAGE + u'not_tag' + '/') + self.assertEqual(PAGE_404, + response_not_existing.templates[0].name, + u'Not existing tag is opened') + + reply_id = Post.objects.create_post('', TEST_TEXT, + thread=Post.objects.all()[0]) + response_not_existing = client.get(THREAD_PAGE + str( + reply_id) + '/') + self.assertEqual(PAGE_404, + response_not_existing.templates[0].name, + u'Reply is opened as a thread') + + +class FormTest(TestCase): def test_post_validation(self): """Test the validation of the post form""" @@ -137,48 +217,3 @@ class BoardTests(TestCase): # Restore captcha setting settings.ENABLE_CAPTCHA = captcha_enabled - - def test_404(self): - """Test receiving error 404 when opening a non-existent page""" - - tag_name = u'test_tag' - tag = Tag.objects.create(name=tag_name) - client = Client() - - Post.objects.create_post('title', TEST_TEXT, tags=[tag]) - - existing_post_id = Post.objects.all()[0].id - response_existing = client.get(THREAD_PAGE + str(existing_post_id) + - '/') - self.assertEqual(HTTP_CODE_OK, response_existing.status_code, - u'Cannot open existing thread') - - response_not_existing = client.get(THREAD_PAGE + str( - existing_post_id + 1) + '/') - self.assertEqual(PAGE_404, - response_not_existing.templates[0].name, - u'Not existing thread is opened') - - response_existing = client.get(TAG_PAGE + tag_name + '/') - self.assertEqual(HTTP_CODE_OK, - response_existing.status_code, - u'Cannot open existing tag') - - response_not_existing = client.get(TAG_PAGE + u'not_tag' + '/') - self.assertEqual(PAGE_404, - response_not_existing.templates[0].name, - u'Not existing tag is opened') - - reply_id = Post.objects.create_post('', TEST_TEXT, - thread=Post.objects.all()[0]) - response_not_existing = client.get(THREAD_PAGE + str( - reply_id) + '/') - self.assertEqual(PAGE_404, - response_not_existing.templates[0].name, - u'Reply is opened as a thread') - - def test_linked_tag(self): - tag = Tag.objects.create(name=u'tag1') - linked_tag = Tag.objects.create(name=u'tag2', linked=tag) - - # TODO run add post view and check the tag is added \ No newline at end of file diff --git a/boards/views.py b/boards/views.py --- a/boards/views.py +++ b/boards/views.py @@ -115,10 +115,6 @@ def _new_post(request, form, thread_id=b tag, created = Tag.objects.get_or_create(name=tag_name) tags.append(tag) - linked_tags = tag.get_linked_tags() - if len(linked_tags) > 0: - tags.extend(linked_tags) - op = None if thread_id == boards.models.NO_PARENT else \ get_object_or_404(Post, id=thread_id) post = Post.objects.create_post(title=title, text=text, ip=ip,