From 79ddc5af25a19187f1210f67df5489477d21d176 Mon Sep 17 00:00:00 2001 From: Adolfo Fitoria Date: Tue, 11 Sep 2012 15:23:32 -0600 Subject: added test cases for convert to comment/answer --- askbot/tests/misc_tests.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++ askbot/views/writers.py | 24 ++++++++++++--- 2 files changed, 96 insertions(+), 4 deletions(-) diff --git a/askbot/tests/misc_tests.py b/askbot/tests/misc_tests.py index 3150c377..328c213f 100644 --- a/askbot/tests/misc_tests.py +++ b/askbot/tests/misc_tests.py @@ -1,6 +1,9 @@ from askbot.tests.utils import AskbotTestCase from askbot.models.post import PostRevision +from django.test.client import Client +from django.core.urlresolvers import reverse + class MiscTests(AskbotTestCase): def setUp(self): @@ -13,3 +16,76 @@ class MiscTests(AskbotTestCase): question = self.post_question(user=self.u1) self.assertRaises(NotImplementedError, question.revisions.create) self.assertRaises(NotImplementedError, PostRevision.objects.create) + +class ContentConvertionTests(AskbotTestCase): + + def setUp(self): + self.u1 = self.create_user(username='user1') + self.u1.set_password('password') + self.u1.set_admin_status() + self.u1.save() + self.u2 = self.create_user(username='notadmin') + self.client = Client() + + #content + self.question = self.post_question(user=self.u1) + self.answer_to_convert = self.post_answer(user=self.u2, + question=self.question) + self.comment_on_answer = self.post_comment(user=self.u1, + parent_post=self.answer_to_convert) + self.another_answer = self.post_answer(user=self.u1, + question=self.question) + self.comment_to_convert = self.post_comment(user=self.u1, + parent_post=self.another_answer) + + def test_convert_comment_to_answer(self): + self.client.login(username='user1', password='password') + old_parent_comment_count = self.another_answer.comment_count + answer_count = self.question.thread.answer_count + self.client.post(reverse('comment_to_answer'), + {'comment_id': self.comment_to_convert.id}) + converted_answer = self.reload_object(self.comment_to_convert) + #old_parent = self.another_answer + old_parent = self.reload_object(self.another_answer) + + #test for convertion + self.assertEquals(converted_answer.post_type, 'answer') + #test for parent change + self.assertNotEquals(old_parent.id, converted_answer.parent.id) + #test for answer count update + self.assertEquals(converted_answer.thread.answer_count, answer_count + 1) + #test for comment count update + self.assertEquals(old_parent.comment_count, old_parent_comment_count - 1) + + #test the delete post view for errors + response = self.client.post(reverse('delete_post'), + {'post_id': converted_answer.id, + 'cancel_vote': 'false'}, + HTTP_X_REQUESTED_WITH='XMLHttpRequest') + self.assertEquals(response.status_code, 200) + self.assertTrue('is_deleted' in response.content) + + def test_convert_answer_to_comment(self): + comment_count = self.question.comment_count + #because the answer itself has a comment too! + comment_count += self.answer_to_convert.comment_count + + answer_count = self.question.thread.answer_count + self.client.login(username='user1', password='password') + self.client.post(reverse('answer_to_comment'), + {'answer_id': self.answer_to_convert.id}) + converted_comment = self.reload_object(self.answer_to_convert) + old_parent = self.reload_object(self.question) + + #test for convertion + self.assertEquals(converted_comment.post_type, 'comment') + #test for answer count update + self.assertEquals(converted_comment.thread.answer_count, answer_count - 1) + #test for comment count update + self.assertEquals(old_parent.comment_count, comment_count + 1) + + #test the delete comment view for errors + response = self.client.post(reverse('delete_comment'), + {'comment_id': converted_comment.id}, + HTTP_X_REQUESTED_WITH='XMLHttpRequest') + self.assertEquals(response.status_code, 200) diff --git a/askbot/views/writers.py b/askbot/views/writers.py index 23c8a2f6..4c7d3f4c 100644 --- a/askbot/views/writers.py +++ b/askbot/views/writers.py @@ -677,8 +677,19 @@ def comment_to_answer(request): comment = get_object_or_404(models.Post, post_type='comment', id=comment_id) comment.post_type = 'answer' + old_parent = comment.parent + + comment.parent = comment.thread._question_post() comment.save() + comment.thread.update_answer_count() + + comment.parent.comment_count += 1 + comment.parent.save() + + old_parent.comment_count -= 1 + old_parent.save() + comment.thread.invalidate_cached_data() return HttpResponseRedirect(comment.get_absolute_url()) @@ -696,14 +707,19 @@ def answer_to_comment(request): if len(answer.text) <= 300: answer.post_type = 'comment' answer.parent = answer.thread._question_post() - answer_comments = models.Post.objects.get_comments().filter(parent=answer) + #can we trust this? + old_comment_count = answer.comment_count + answer.comment_count = 0 - for comment in answer_comments: - comment.parent = answer.parent - comment.save() + answer_comments = models.Post.objects.get_comments().filter(parent=answer) + answer_comments.update(parent=answer.parent) answer.parse_and_save(author=answer.author) answer.thread.update_answer_count() + + answer.parent.comment_count = 1 + old_comment_count + answer.parent.save() + answer.thread.invalidate_cached_data() else: request.user.message_set.create(message = _("the selected answer cannot be a comment")) -- cgit v1.2.3-1-g7c22