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.
Django/comments-jrx/tests.py

120 lines
4.4 KiB

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.
class CommentsTest(TransactionTestCase):
def setUp(self):
#jrx: 初始化测试客户端和工厂
self.client = Client()
self.factory = RequestFactory()
#jrx: 设置博客评论需要审核
from blog.models import BlogSettings
value = BlogSettings()
value.comment_need_review = True
value.save()
#jrx: 创建超级用户用于测试
self.user = BlogUser.objects.create_superuser(
email="liangliangyy1@gmail.com",
username="liangliangyy1",
password="liangliangyy1")
#jrx: 辅助方法:更新文章下所有评论为启用状态
def update_article_comment_status(self, article):
comments = article.comment_set.all()
for comment in comments:
comment.is_enable = True
comment.save()
#jrx: 测试评论功能(提交、回复、显示等)
def test_validate_comment(self):
#jrx: 登录测试用户
self.client.login(username='liangliangyy1', password='liangliangyy1')
#jrx: 创建测试分类和文章
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.save()
#jrx: 获取评论提交URL
comment_url = reverse(
'comments:postcomment', kwargs={
'article_id': article.id})
#jrx: 测试提交第一条评论
response = self.client.post(comment_url,
{
'body': '123ffffffffff'
})
self.assertEqual(response.status_code, 302) #jrx: 验证重定向状态码
#jrx: 验证评论需审核(未启用时不显示)
article = Article.objects.get(pk=article.pk)
self.assertEqual(len(article.comment_list()), 0)
self.update_article_comment_status(article) #jrx: 手动启用评论
self.assertEqual(len(article.comment_list()), 1) #jrx: 验证评论数量
#jrx: 测试提交第二条评论
response = self.client.post(comment_url,
{
'body': '123ffffffffff',
})
self.assertEqual(response.status_code, 302)
self.update_article_comment_status(article)
self.assertEqual(len(article.comment_list()), 2) #jrx: 验证评论数量
#jrx: 测试回复评论功能
parent_comment_id = article.comment_list()[0].id #jrx: 获取父评论ID
response = self.client.post(comment_url,
{
'body': '''
# Title1
```python
import os
```
[url](https://www.lylinux.net/)
[ddd](http://www.baidu.com)
''',
'parent_comment_id': parent_comment_id#jrx: 指定父评论
})
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)#jrx: 验证包含回复的总评论数
# jrx: 测试评论树解析和模板标签
comment = Comment.objects.get(id=parent_comment_id)
tree = parse_commenttree(article.comment_list(), comment)#jrx: 解析评论树
self.assertEqual(len(tree), 1)#jrx: 验证评论树结构
data = show_comment_item(comment, True)#jrx: 测试评论项渲染
self.assertIsNotNone(data)
# jrx: 测试最大 ID 工具函数
s = get_max_articleid_commentid()
self.assertIsNotNone(s)
# jrx: 测试评论邮件发送功能
from comments.utils import send_comment_email
send_comment_email(comment)