You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tentest/doc/DjangoBlog/comments/tests.py

106 lines
5.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# FRR该模块为评论功能(comments)的单元测试类,
# 主要测试评论提交、评论列表展示、评论嵌套结构解析及邮件通知等核心功能,
# 确保评论功能在各种场景下正常工作。
from django.test import Client, RequestFactory, TransactionTestCase
from django.urls import reverse # 用于生成URL反向解析
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 # 导入工具函数测试ID获取功能
# FRR评论功能测试类继承TransactionTestCase以支持事务性测试避免测试数据污染
class CommentsTest(TransactionTestCase):
# LGM测试前的初始化方法会在每个测试方法执行前运行
def setUp(self):
self.client = Client() # 创建测试客户端,模拟用户请求
self.factory = RequestFactory() # 创建请求工厂,用于构造复杂请求
# FRR配置博客评论设置需要审核才能显示
from blog.models import BlogSettings
value = BlogSettings()
value.comment_need_review = True # 评论需要审核
value.save() # 保存设置到数据库
# FRR创建超级用户用于测试登录状态下的评论功能
self.user = BlogUser.objects.create_superuser(
email="liangliangyy1@gmail.com",
username="liangliangyy1",
password="liangliangyy1") # 用户名和密码用于测试登录
# FRR辅助方法批量更新文章下所有评论为"启用"状态(模拟管理员审核通过)
def update_article_comment_status(self, article):
comments = article.comment_set.all() # 获取文章下所有评论
for comment in comments:
comment.is_enable = True # 设为启用
comment.save() # 保存修改
# FRR核心测试方法验证评论提交、嵌套回复、模板渲染等功能
def test_validate_comment(self):
# 1. 登录测试用户
self.client.login(username='liangliangyy1', password='liangliangyy1')
# 2. 创建测试分类和文章(评论必须关联到具体文章)
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' # 文章类型(假设'a'表示普通文章)
article.status = 'p' # 发布状态(假设'p'表示已发布)
article.save() # 保存文章
# 3. 测试提交一级评论(无父评论)
# 生成评论提交URL通过文章ID反向解析
comment_url = reverse(
'comments:postcomment', kwargs={
'article_id': article.id})
# 模拟POST请求提交评论
response = self.client.post(comment_url,
{
'body': '123ffffffffff' # 评论内容
})
# 验证提交成功302表示重定向通常是提交后跳回文章页
self.assertEqual(response.status_code, 302)
# 由于评论需要审核(默认未启用),此时评论列表应为空
article = Article.objects.get(pk=article.pk) # 重新获取文章(刷新数据)
self.assertEqual(len(article.comment_list()), 0) # 评论列表长度为0
# 手动审核评论(设为启用)
self.update_article_comment_status(article)
# 审核后评论列表应包含1条评论
self.assertEqual(len(article.comment_list()), 1)
# 4. 再次提交一条评论,测试多条评论场景
response = self.client.post(comment_url,
{
'body': '123ffffffffff',
})
self.assertEqual(response.status_code, 302) # 验证提交成功
# 审核后评论列表应包含2条评论
article = Article.objects.get(pk=article.pk)
self.update_article_comment_status(article)
self.assertEqual(len(article.comment_list()), 2)
# 5. 测试提交嵌套回复(针对第一条评论的回复)
parent_comment_id = article.comment_list()[0].id # 获取第一条评论ID作为父评论
# 提交带Markdown格式的回复内容测试富文本支持
response = self.client.post(comment_url,
{
'body': '''
# Title1
```python
import os