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.
DjangoBlog/servermanager/tests.py

117 lines
4.5 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.

# 马莹导入Django测试相关模块
from django.test import Client, RequestFactory, TestCase
from django.utils import timezone
# 马莹:导入微信机器人文本消息类
from werobot.messages.messages import TextMessage
# 马莹:导入项目内相关模型和工具类
from accounts.models import BlogUser
from blog.models import Category, Article
from servermanager.api.commonapi import ChatGPT
from .models import commands
from .robot import MessageHandler, CommandHandler
from .robot import search, category, recents
# Create your tests here.
# 马莹定义服务器管理模块的测试类继承自Django的TestCase
class ServerManagerTest(TestCase):
def setUp(self):
"""测试初始化方法,在每个测试方法执行前运行"""
# 马莹创建测试客户端用于模拟HTTP请求
self.client = Client()
# 马莹:创建请求工厂,用于构造测试用的请求对象
self.factory = RequestFactory()
def test_chat_gpt(self):
"""测试ChatGPT聊天功能"""
# 马莹调用ChatGPT的chat方法发送"你好"消息
content = ChatGPT.chat("你好")
# 马莹:断言返回结果不为空
self.assertIsNotNone(content)
def test_validate_comment(self):
"""综合测试各类功能:文章搜索、分类、最新文章、命令执行、消息处理等"""
# 马莹:创建超级用户
user = BlogUser.objects.create_superuser(
email="liangliangyy1@gmail.com",
username="liangliangyy1",
password="liangliangyy1")
# 马莹:使用测试客户端登录该超级用户
self.client.login(username='liangliangyy1', password='liangliangyy1')
# 马莹:创建测试分类并保存到数据库
c = Category()
c.name = "categoryccc"
c.save()
# 马莹:创建测试文章并保存到数据库
article = Article()
article.title = "nicetitleccc"
article.body = "nicecontentccc"
article.author = user # 关联作者
article.category = c # 关联分类
article.type = 'a' # 文章类型
article.status = 'p' # 发布状态
article.save()
# 马莹:构造文本消息对象,内容为"nice"
s = TextMessage([])
s.content = "nice"
# 马莹:测试文章搜索功能
rsp = search(s, None)
# 马莹:测试获取分类功能
rsp = category(None, None)
# 马莹:断言分类功能返回结果不为空
self.assertIsNotNone(rsp)
# 马莹:测试获取最新文章功能
rsp = recents(None, None)
# 马莹:断言最新文章功能返回结果不是"暂时还没有文章"
self.assertTrue(rsp != '暂时还没有文章')
# 马莹:创建测试命令并保存到数据库
cmd = commands()
cmd.title = "test"
cmd.command = "ls"
cmd.describe = "test"
cmd.save()
# 马莹:实例化命令处理器
cmdhandler = CommandHandler()
# 马莹:测试执行命令功能
rsp = cmdhandler.run('test')
# 马莹:断言命令执行结果不为空
self.assertIsNotNone(rsp)
# 马莹设置消息的发送者ID
s.source = 'u'
# 马莹:设置消息内容为'test'
s.content = 'test'
# 马莹:实例化消息处理器
msghandler = MessageHandler(s, {})
# 马莹:以下注释代码为管理员权限相关配置(当前测试未启用)
# msghandler.userinfo.isPasswordSet = True
# msghandler.userinfo.isAdmin = True
# 马莹:处理消息内容'test'
msghandler.handler()
# 马莹:修改消息内容为'y'(模拟确认执行命令)
s.content = 'y'
msghandler.handler()
# 马莹:修改消息内容为身份证查询指令(建设中功能)
s.content = 'idcard:12321233'
msghandler.handler()
# 马莹:修改消息内容为天气查询指令(建设中功能)
s.content = 'weather:上海'
msghandler.handler()
# 马莹:修改消息内容为'admin'(进入管理员模式)
s.content = 'admin'
msghandler.handler()
# 马莹:修改消息内容为'123'(输入管理员密码)
s.content = '123'
msghandler.handler()
# 马莹:修改消息内容为'exit'(退出管理员模式)
s.content = 'exit'
msghandler.handler()