|
|
|
|
@ -1,68 +1,92 @@
|
|
|
|
|
# 模块级注释:Django测试模块 - 评论系统功能测试
|
|
|
|
|
# 本模块包含评论系统的完整测试用例,验证评论发布、回复、显示等核心功能
|
|
|
|
|
from django.test import Client, RequestFactory, TransactionTestCase
|
|
|
|
|
from django.urls import reverse
|
|
|
|
|
|
|
|
|
|
# 导入相关模型,用于测试数据准备
|
|
|
|
|
from accounts.models import BlogUser
|
|
|
|
|
from blog.models import Category, Article
|
|
|
|
|
from comments.models import Comment
|
|
|
|
|
# 导入评论标签模块,测试模板标签功能
|
|
|
|
|
from comments.templatetags.comments_tags import *
|
|
|
|
|
from djangoblog.utils import get_max_articleid_commentid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Create your tests here.
|
|
|
|
|
|
|
|
|
|
# 类级注释:评论系统测试类
|
|
|
|
|
# 继承自TransactionTestCase,支持数据库事务的测试用例
|
|
|
|
|
class CommentsTest(TransactionTestCase):
|
|
|
|
|
|
|
|
|
|
# 测试初始化方法:在每个测试方法执行前运行
|
|
|
|
|
def setUp(self):
|
|
|
|
|
# 创建测试客户端,用于模拟HTTP请求
|
|
|
|
|
self.client = Client()
|
|
|
|
|
# 创建请求工厂,用于构建请求对象
|
|
|
|
|
self.factory = RequestFactory()
|
|
|
|
|
|
|
|
|
|
# 配置博客设置:启用评论审核功能
|
|
|
|
|
from blog.models import BlogSettings
|
|
|
|
|
value = BlogSettings()
|
|
|
|
|
value.comment_need_review = True
|
|
|
|
|
value.comment_need_review = True # 设置评论需要审核
|
|
|
|
|
value.save()
|
|
|
|
|
|
|
|
|
|
# 创建超级用户,用于测试认证相关的评论功能
|
|
|
|
|
self.user = BlogUser.objects.create_superuser(
|
|
|
|
|
email="liangliangyy1@gmail.com",
|
|
|
|
|
username="liangliangyy1",
|
|
|
|
|
password="liangliangyy1")
|
|
|
|
|
|
|
|
|
|
# 辅助方法:更新文章评论状态为启用
|
|
|
|
|
def update_article_comment_status(self, article):
|
|
|
|
|
# 获取文章的所有评论
|
|
|
|
|
comments = article.comment_set.all()
|
|
|
|
|
# 遍历所有评论,将其状态设置为启用
|
|
|
|
|
for comment in comments:
|
|
|
|
|
comment.is_enable = True
|
|
|
|
|
comment.save()
|
|
|
|
|
|
|
|
|
|
# 测试方法:验证评论功能
|
|
|
|
|
def test_validate_comment(self):
|
|
|
|
|
# 使用测试用户登录系统
|
|
|
|
|
self.client.login(username='liangliangyy1', password='liangliangyy1')
|
|
|
|
|
|
|
|
|
|
# 创建测试分类
|
|
|
|
|
category = Category()
|
|
|
|
|
category.name = "categoryccc"
|
|
|
|
|
category.save()
|
|
|
|
|
|
|
|
|
|
# 创建测试文章
|
|
|
|
|
article = Article()
|
|
|
|
|
article.title = "nicetitleccc"
|
|
|
|
|
article.body = "nicecontentccc"
|
|
|
|
|
article.author = self.user
|
|
|
|
|
article.category = category
|
|
|
|
|
article.type = 'a'
|
|
|
|
|
article.status = 'p'
|
|
|
|
|
article.type = 'a' # 文章类型
|
|
|
|
|
article.status = 'p' # 发布状态
|
|
|
|
|
article.save()
|
|
|
|
|
|
|
|
|
|
# 生成评论提交URL
|
|
|
|
|
comment_url = reverse(
|
|
|
|
|
'comments:postcomment', kwargs={
|
|
|
|
|
'article_id': article.id})
|
|
|
|
|
|
|
|
|
|
# 测试提交第一条评论
|
|
|
|
|
response = self.client.post(comment_url,
|
|
|
|
|
{
|
|
|
|
|
'body': '123ffffffffff'
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
# 验证响应状态码为302重定向
|
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
|
|
|
|
|
|
# 重新获取文章对象,验证评论数量(由于审核机制,初始应为0)
|
|
|
|
|
article = Article.objects.get(pk=article.pk)
|
|
|
|
|
self.assertEqual(len(article.comment_list()), 0)
|
|
|
|
|
self.update_article_comment_status(article)
|
|
|
|
|
|
|
|
|
|
# 更新评论状态为启用后,验证评论数量变为1
|
|
|
|
|
self.update_article_comment_status(article)
|
|
|
|
|
self.assertEqual(len(article.comment_list()), 1)
|
|
|
|
|
|
|
|
|
|
# 测试提交第二条评论
|
|
|
|
|
response = self.client.post(comment_url,
|
|
|
|
|
{
|
|
|
|
|
'body': '123ffffffffff',
|
|
|
|
|
@ -70,11 +94,15 @@ class CommentsTest(TransactionTestCase):
|
|
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
|
|
|
|
|
|
# 验证第二条评论提交成功
|
|
|
|
|
article = Article.objects.get(pk=article.pk)
|
|
|
|
|
self.update_article_comment_status(article)
|
|
|
|
|
self.assertEqual(len(article.comment_list()), 2)
|
|
|
|
|
|
|
|
|
|
# 获取第一条评论的ID,用于测试回复功能
|
|
|
|
|
parent_comment_id = article.comment_list()[0].id
|
|
|
|
|
|
|
|
|
|
# 测试提交带Markdown格式的回复评论
|
|
|
|
|
response = self.client.post(comment_url,
|
|
|
|
|
{
|
|
|
|
|
'body': '''
|
|
|
|
|
@ -93,17 +121,25 @@ class CommentsTest(TransactionTestCase):
|
|
|
|
|
'parent_comment_id': parent_comment_id
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
# 验证回复评论提交成功
|
|
|
|
|
self.assertEqual(response.status_code, 302)
|
|
|
|
|
self.update_article_comment_status(article)
|
|
|
|
|
article = Article.objects.get(pk=article.pk)
|
|
|
|
|
self.assertEqual(len(article.comment_list()), 3)
|
|
|
|
|
|
|
|
|
|
# 测试评论树解析功能
|
|
|
|
|
comment = Comment.objects.get(id=parent_comment_id)
|
|
|
|
|
tree = parse_commenttree(article.comment_list(), comment)
|
|
|
|
|
self.assertEqual(len(tree), 1)
|
|
|
|
|
|
|
|
|
|
# 测试评论项显示功能
|
|
|
|
|
data = show_comment_item(comment, True)
|
|
|
|
|
self.assertIsNotNone(data)
|
|
|
|
|
|
|
|
|
|
# 测试获取最大文章ID和评论ID功能
|
|
|
|
|
s = get_max_articleid_commentid()
|
|
|
|
|
self.assertIsNotNone(s)
|
|
|
|
|
|
|
|
|
|
# 测试评论邮件发送功能
|
|
|
|
|
from comments.utils import send_comment_email
|
|
|
|
|
send_comment_email(comment)
|
|
|
|
|
send_comment_email(comment)
|