import json
from datetime import datetime
from pathlib import Path
from typing import Any
import httpx
import pytest
import respx
from nonebot import get_adapter
from nonebot.adapters.onebot.v11 import Adapter, Bot, Message, MessageSegment
from nonebug import App
from pytest_mock import MockerFixture
from respx import MockRouter
from sqlalchemy import delete
from tests.fake import fake_group_message_event_v11
@pytest.fixture
async def app(app: App):
yield app
# 清理数据库
from nonebot_plugin_orm import get_session
from src.plugins.ff14.plugins.ff14_fflogs.data import FFLOGS_DATA
from src.plugins.ff14.plugins.ff14_fflogs.models import User
async with get_session() as session, session.begin():
await session.execute(delete(User))
# 清除缓存的数据
FFLOGS_DATA._data = None
@pytest.fixture
async def fflogs_data(app: App) -> dict[str, Any]:
path = Path(__file__).parent / "fflogs_data.json"
with path.open("r", encoding="utf-8") as f:
data = json.load(f)
return data
@pytest.fixture
async def fflogs_character_rankings(app: App) -> dict[str, Any]:
path = Path(__file__).parent / "fflogs_character_rankings.json"
with path.open("r", encoding="utf-8") as f:
data = json.load(f)
return data
@pytest.fixture
async def fflogs_job_rankings(app: App) -> dict[str, Any]:
path = Path(__file__).parent / "fflogs_job_rankings.json"
with path.open("r", encoding="utf-8") as f:
data = json.load(f)
return data
@pytest.fixture
async def fflogs_job_rankings_empty(app: App) -> dict[str, Any]:
path = Path(__file__).parent / "fflogs_job_rankings_empty.json"
with path.open("r", encoding="utf-8") as f:
data = json.load(f)
return data
async def test_dps_help(app: App, mocker: MockerFixture):
"""测试 FFLOGS,返回帮助的情况"""
from src.plugins.ff14.plugins.ff14_fflogs import (
__plugin_meta__,
fflogs_cmd,
plugin_config,
)
help_msg = f"{__plugin_meta__.name}\n\n{__plugin_meta__.usage}"
mocker.patch.object(plugin_config, "fflogs_token", "test")
async with app.test_matcher(fflogs_cmd) as ctx:
bot = ctx.create_bot(base=Bot)
event = fake_group_message_event_v11(message=Message("/dps"), user_id=10000)
ctx.receive_event(bot, event)
ctx.should_call_send(event, help_msg, True)
ctx.should_finished(fflogs_cmd)
async with app.test_matcher(fflogs_cmd) as ctx:
bot = ctx.create_bot(base=Bot)
event = fake_group_message_event_v11(message=Message("/dps "), user_id=10000)
ctx.receive_event(bot, event)
ctx.should_call_send(event, help_msg, True)
ctx.should_finished(fflogs_cmd)
async with app.test_matcher(fflogs_cmd) as ctx:
bot = ctx.create_bot(base=Bot)
event = fake_group_message_event_v11(
message=Message("/dps test"), user_id=10000
)
ctx.receive_event(bot, event)
ctx.should_call_send(event, help_msg, True)
ctx.should_finished(fflogs_cmd)
async def test_dps_missing_token(app: App):
"""测试 FFLOGS,缺少 Token 的情况"""
from src.plugins.ff14.plugins.ff14_fflogs import fflogs_cmd
async with app.test_matcher(fflogs_cmd) as ctx:
bot = ctx.create_bot(base=Bot)
event = fake_group_message_event_v11(message=Message("/dps me"))
ctx.receive_event(bot, event)
ctx.should_call_send(
event,
"对不起,Token 未设置,无法查询数据。\n请先在 .env 中配置好 Token 后再尝试查询数据。",
True,
)
ctx.should_finished(fflogs_cmd)
async def test_dps_cache(app: App, mocker: MockerFixture):
"""测试 FFLOGS,设置缓存的情况"""
from src.plugins.ff14.plugins.ff14_fflogs import fflogs_cmd, plugin_config
mocker.patch.object(plugin_config, "fflogs_token", "test")
async with app.test_matcher(fflogs_cmd) as ctx:
bot = ctx.create_bot(base=Bot)
event = fake_group_message_event_v11(message=Message("/dps cache list"))
ctx.receive_event(bot, event)
ctx.should_call_send(event, "当前没有缓存副本。", True)
ctx.should_finished(fflogs_cmd)
async with app.test_matcher(fflogs_cmd) as ctx:
bot = ctx.create_bot(base=Bot)
event = fake_group_message_event_v11(message=Message("/dps cache add p1s"))
ctx.receive_event(bot, event)
ctx.should_call_send(event, "已添加副本 p1s。", True)
ctx.should_finished(fflogs_cmd)
async with app.test_matcher(fflogs_cmd) as ctx:
bot = ctx.create_bot(base=Bot)
event = fake_group_message_event_v11(message=Message("/dps cache list"))
ctx.receive_event(bot, event)
ctx.should_call_send(event, "当前缓存的副本有:\np1s", True)
ctx.should_finished(fflogs_cmd)
async with app.test_matcher(fflogs_cmd) as ctx:
bot = ctx.create_bot(base=Bot)
event = fake_group_message_event_v11(message=Message("/dps cache del p2s"))
ctx.receive_event(bot, event)
ctx.should_call_send(event, "没有缓存 p2s,无法删除。", True)
ctx.should_finished(fflogs_cmd)
async with app.test_matcher(fflogs_cmd) as ctx:
bot = ctx.create_bot(base=Bot)
event = fake_group_message_event_v11(message=Message("/dps cache del p1s"))
ctx.receive_event(bot, event)
ctx.should_call_send(event, "已删除副本 p1s。", True)
ctx.should_finished(fflogs_cmd)
async with app.test_matcher(fflogs_cmd) as ctx:
bot = ctx.create_bot(base=Bot)
event = fake_group_message_event_v11(message=Message("/dps cache list"))
ctx.receive_event(bot, event)
ctx.should_call_send(event, "当前没有缓存副本。", True)
ctx.should_finished(fflogs_cmd)
async def test_dps_permission(app: App, mocker: MockerFixture):
"""测试 FFLOGS,没有权限情况"""
from src.plugins.ff14.plugins.ff14_fflogs import fflogs_cmd, plugin_config
mocker.patch.object(plugin_config, "fflogs_token", "test")
async with app.test_matcher(fflogs_cmd) as ctx:
bot = ctx.create_bot(base=Bot)
event = fake_group_message_event_v11(
message=Message("/dps cache del p2s"), user_id=10000
)
ctx.receive_event(bot, event)
ctx.should_call_send(event, "抱歉,你没有权限设置缓存。", True)
ctx.should_finished(fflogs_cmd)
async def test_dps_bind(app: App, mocker: MockerFixture):
"""测试绑定角色"""
from src.plugins.ff14.plugins.ff14_fflogs import fflogs_cmd, plugin_config
mocker.patch.object(plugin_config, "fflogs_token", "test")
# 查询自己的绑定角色
async with app.test_matcher(fflogs_cmd) as ctx:
adapter = get_adapter(Adapter)
bot = ctx.create_bot(base=Bot, adapter=adapter)
event = fake_group_message_event_v11(message=Message("/dps me"))
ctx.receive_event(bot, event)
ctx.should_call_send(
event,
MessageSegment.at(10)
+ "抱歉,你没有绑定最终幻想14的角色。\n请使用\n/dps me 角色名 服务器名\n绑定自己的角色。",
True,
)
ctx.should_finished(fflogs_cmd)
# 查询别人的绑定角色
async with app.test_matcher(fflogs_cmd) as ctx:
adapter = get_adapter(Adapter)
bot = ctx.create_bot(base=Bot, adapter=adapter)
event = fake_group_message_event_v11(
message=Message("/dps" + MessageSegment.at(10))
)
ctx.receive_event(bot, event)
ctx.should_call_send(
event,
MessageSegment.at(10) + "抱歉,该用户没有绑定最终幻想14的角色。",
True,
)
ctx.should_finished(fflogs_cmd)
# 绑定角色
async with app.test_matcher(fflogs_cmd) as ctx:
adapter =
![avatar](https://profile-avatar.csdnimg.cn/ed455cf87e1b477e899510a00920b7e5_runnymmede.jpg!1)
.whl
- 粉丝: 3979
- 资源: 4923
最新资源
- 基于Java语言的HBase分布式数据库设计源码分析
- BLCN_v_0_0_2.zip
- 基于HTML、CSS、JavaScript的购物商城设计源码
- 基于Vue、JavaScript、CSS、HTML的交通事故管理系统设计源码
- 基于Comsol声波阵面调控技术的压力声学与固体力学模块研究:3258-3824hz扫频在Comsol6.1版本中的应用,基于Comsol声波阵面调控技术的压力声学与固体力学模块研究:3258-382
- 基于Nodejs扩展宿主的coc.nvim设计源码,支持多种编程语言和语言服务器
- ESP-IDFESP32C6使用ESP-IDF5.4驱动ST7789V
- 基于VDLL算法的矢量型GPS信号跟踪算法MATLAB仿真研究:程序与Word设计文档详解,基于VDLL算法的矢量型GPS信号跟踪算法MATLAB仿真研究:程序与Word设计文档详解,基于VDLL的矢
- 循环温度的边界条件设置:双法实现与复杂温度变化的深度探讨,基于循环温度调控的双方法边界条件设置技术及复杂温度变化处理方案,两种方法实现循环温度的边界条件设置 复杂的温度变化 ,循环温度的边界条件设
- 基于Vue框架的智联铁塔前端开发设计源码
- 基于C#游戏逻辑的方块闯关游戏设计源码
- 基于STM3F4源码的VESC非线性磁链观测器:零速启动与详细注释,助您学习磁链观测技术,包含simulink仿真与文献参考,基于STM3F4源码的VESC非线性磁链观测器:零速启动与详细注释,sim
- 基于Java的公寓租赁平台移动端与后台管理系统设计源码
- 西门子Smart SB CM01与台达DT330温控器485通讯程序设计与实现(基于S7-200 Smart PLC控制),西门子Smart SB CM01与台达DT330温控器485通讯程序:PLC
- 基于JavaScript、CSS、HTML的贷款H5页面设计源码
- easy-test-app.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)