From 68487e144c56d4f40c1f54a695b5d30ec3d25786 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 20 May 2025 18:39:41 +0000 Subject: [PATCH] Add tests for edit_comment and delete_comment to improve test coverage Co-Authored-By: Shawn Azman --- realworld/comments/tests.py | 77 +++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/realworld/comments/tests.py b/realworld/comments/tests.py index e477576..1c0fd21 100644 --- a/realworld/comments/tests.py +++ b/realworld/comments/tests.py @@ -40,3 +40,80 @@ def test_add_comment(self): self.assertEqual(comment.article, self.article) self.assertEqual(comment.author, self.author) + + +class TestEditCommentView(TestCase): + + @classmethod + def setUpTestData(cls): + cls.author = User( + email="tester@gmail.com", + name="tester", + ) + cls.author.save() + + cls.article = Article.objects.create( + title="test", + summary="test", + content="test", + author=cls.author, + ) + + cls.comment = Comment.objects.create( + article=cls.article, + author=cls.author, + content="original comment" + ) + + cls.url = reverse("edit_comment", args=[cls.comment.id]) + + def test_edit_comment_get(self): + self.client.force_login(self.author) + + response = self.client.get(self.url) + self.assertEqual(response.status_code, http.HTTPStatus.OK) + self.assertIsNotNone(response.context["form"]) + self.assertEqual(response.context["comment"], self.comment) + + def test_edit_comment_post(self): + self.client.force_login(self.author) + + response = self.client.post(self.url, {"content": "updated comment"}) + self.assertEqual(response.status_code, http.HTTPStatus.OK) + + comment = Comment.objects.get(pk=self.comment.id) + self.assertEqual(comment.content, "updated comment") + + +class TestDeleteCommentView(TestCase): + + @classmethod + def setUpTestData(cls): + cls.author = User( + email="tester@gmail.com", + name="tester", + ) + cls.author.save() + + cls.article = Article.objects.create( + title="test", + summary="test", + content="test", + author=cls.author, + ) + + cls.comment = Comment.objects.create( + article=cls.article, + author=cls.author, + content="test comment" + ) + + cls.url = reverse("delete_comment", args=[cls.comment.id]) + + def test_delete_comment(self): + self.client.force_login(self.author) + + response = self.client.delete(self.url) + self.assertEqual(response.status_code, http.HTTPStatus.OK) + + self.assertFalse(Comment.objects.filter(pk=self.comment.id).exists())