Skip to content

Commit

Permalink
pre
Browse files Browse the repository at this point in the history
  • Loading branch information
krrish-sehgal committed Jan 8, 2025
1 parent a1b6c87 commit a23dea3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 60 deletions.
52 changes: 21 additions & 31 deletions website/test_blog.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,54 @@
from django.test import TestCase, override_settings
from django.urls import reverse
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from website.models import Post, UserProfile
from django.test import TestCase, override_settings
from django.urls import reverse

from comments.models import Comment
from website.models import Post, UserProfile


@override_settings(STATICFILES_STORAGE='django.contrib.staticfiles.storage.StaticFilesStorage')
@override_settings(STATICFILES_STORAGE="django.contrib.staticfiles.storage.StaticFilesStorage")
class BlogCommentTests(TestCase):
def setUp(self):
self.user = User.objects.create_user(username='testuser', password='12345')
self.user = User.objects.create_user(username="testuser", password="12345")
self.user_profile, created = UserProfile.objects.get_or_create(user=self.user)
self.post = Post.objects.create(
title='Test Post',
content='Test Content',
author=self.user
)
self.client.login(username='testuser', password='12345')
self.post = Post.objects.create(title="Test Post", content="Test Content", author=self.user)
self.client.login(username="testuser", password="12345")

def test_add_comment(self):
url = reverse('comment_on_content', args=[self.post.pk])
data = {
'content_type': 'post',
'comment': 'This is a test comment.'
}
url = reverse("comment_on_content", args=[self.post.pk])
data = {"content_type": "post", "comment": "This is a test comment."}
response = self.client.post(url, data)
self.assertEqual(response.status_code, 200)
self.assertTrue(Comment.objects.filter(content_type__model='post', object_id=self.post.pk).exists())
self.assertTrue(
Comment.objects.filter(content_type__model="post", object_id=self.post.pk).exists()
)

def test_update_comment(self):
comment = Comment.objects.create(
content_type=ContentType.objects.get_for_model(Post),
object_id=self.post.pk,
author=self.user.username,
author_fk=self.user_profile,
text='Original comment'
text="Original comment",
)
url = reverse('update_content_comment', args=[self.post.pk, comment.pk])
data = {
'content_type': 'post',
'comment': 'Updated comment'
}
url = reverse("update_content_comment", args=[self.post.pk, comment.pk])
data = {"content_type": "post", "comment": "Updated comment"}
response = self.client.post(url, data)
self.assertEqual(response.status_code, 200)
comment.refresh_from_db()
self.assertEqual(comment.text, 'Updated comment')
self.assertEqual(comment.text, "Updated comment")

def test_delete_comment(self):
comment = Comment.objects.create(
content_type=ContentType.objects.get_for_model(Post),
object_id=self.post.pk,
author=self.user.username,
author_fk=self.user_profile,
text='Comment to be deleted'
text="Comment to be deleted",
)
url = reverse('delete_content_comment')
data = {
'content_type': 'post',
'content_pk': self.post.pk,
'comment_pk': comment.pk
}
url = reverse("delete_content_comment")
data = {"content_type": "post", "content_pk": self.post.pk, "comment_pk": comment.pk}
response = self.client.post(url, data)
self.assertEqual(response.status_code, 200)
self.assertFalse(Comment.objects.filter(pk=comment.pk).exists())
50 changes: 21 additions & 29 deletions website/test_issues.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,56 @@
from django.test import TestCase,override_settings
from django.urls import reverse
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from website.models import Issue, UserProfile
from django.test import TestCase, override_settings
from django.urls import reverse

from comments.models import Comment
from website.models import Issue, UserProfile


@override_settings(STATICFILES_STORAGE='django.contrib.staticfiles.storage.StaticFilesStorage')
@override_settings(STATICFILES_STORAGE="django.contrib.staticfiles.storage.StaticFilesStorage")
class IssueCommentTests(TestCase):
def setUp(self):
self.user = User.objects.create_user(username='testuser', password='12345')
self.user = User.objects.create_user(username="testuser", password="12345")
self.user_profile, created = UserProfile.objects.get_or_create(user=self.user)
self.issue = Issue.objects.create(
url='http://example.com',
description='Test Issue',
user=self.user
url="http://example.com", description="Test Issue", user=self.user
)
self.client.login(username='testuser', password='12345')
self.client.login(username="testuser", password="12345")

def test_add_comment(self):
url = reverse('comment_on_content', args=[self.issue.pk])
data = {
'content_type': 'issue',
'comment': 'This is a test comment.'
}
url = reverse("comment_on_content", args=[self.issue.pk])
data = {"content_type": "issue", "comment": "This is a test comment."}
response = self.client.post(url, data)
self.assertEqual(response.status_code, 200)
self.assertTrue(Comment.objects.filter(content_type__model='issue', object_id=self.issue.pk).exists())
self.assertTrue(
Comment.objects.filter(content_type__model="issue", object_id=self.issue.pk).exists()
)

def test_update_comment(self):
comment = Comment.objects.create(
content_type=ContentType.objects.get_for_model(Issue),
object_id=self.issue.pk,
author=self.user.username,
author_fk=self.user_profile,
text='Original comment'
text="Original comment",
)
url = reverse('update_content_comment', args=[self.issue.pk, comment.pk])
data = {
'content_type': 'issue',
'comment': 'Updated comment'
}
url = reverse("update_content_comment", args=[self.issue.pk, comment.pk])
data = {"content_type": "issue", "comment": "Updated comment"}
response = self.client.post(url, data)
self.assertEqual(response.status_code, 200)
comment.refresh_from_db()
self.assertEqual(comment.text, 'Updated comment')
self.assertEqual(comment.text, "Updated comment")

def test_delete_comment(self):
comment = Comment.objects.create(
content_type=ContentType.objects.get_for_model(Issue),
object_id=self.issue.pk,
author=self.user.username,
author_fk=self.user_profile,
text='Comment to be deleted'
text="Comment to be deleted",
)
url = reverse('delete_content_comment')
data = {
'content_type': 'issue',
'content_pk': self.issue.pk,
'comment_pk': comment.pk
}
url = reverse("delete_content_comment")
data = {"content_type": "issue", "content_pk": self.issue.pk, "comment_pk": comment.pk}
response = self.client.post(url, data)
self.assertEqual(response.status_code, 200)
self.assertFalse(Comment.objects.filter(pk=comment.pk).exists())

0 comments on commit a23dea3

Please sign in to comment.