Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions realworld/comments/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())