async def test_history_get_arg(app: App): """测试历史""" from nonebot.adapters.onebot.v11 import Message from src.plugins.repeat import history_cmd, recorder_obj recorder_obj._msg_number_list = {10000: {1: {10: 100}}} recorder_obj._repeat_list = {10000: {1: {10: 10}}} async with app.test_matcher(history_cmd) as ctx: bot = ctx.create_bot() event = fake_group_message_event(message=Message("/history")) year_event = fake_group_message_event(message=Message("2020")) month_event = fake_group_message_event(message=Message("1")) day_event = fake_group_message_event(message=Message("0")) ctx.receive_event(bot, event) ctx.should_call_send(event, "你请输入你要查询的年份", True) ctx.receive_event(bot, year_event) ctx.should_call_send(year_event, "你请输入你要查询的月份", True) ctx.receive_event(bot, month_event) ctx.should_call_send(month_event, "你请输入你要查询的日期(如查询整月排名请输入 0)", True) ctx.receive_event(bot, day_event) ctx.should_call_send(day_event, "2020 年 1 月的数据不存在,请换个试试吧 0.0", True)
async def test_gete_ask_arg_error( app: App, mocker: MockerFixture, ): """测试藏宝选门,两个门的情况,第一次输入错误""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.ff14 import gate_cmd from src.plugins.ff14.gate import EXPR_GATE render_expression = mocker.patch("src.plugins.ff14.gate.render_expression") render_expression.return_value = Message("test") randint = mocker.patch("src.plugins.ff14.gate.randint") randint.return_value = 1 async with app.test_matcher(gate_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/gate")) next_event = fake_group_message_event(message=Message("4")) final_event = fake_group_message_event(message=Message("2")) ctx.receive_event(bot, event) ctx.should_call_send(event, "总共有多少个门呢?", "result") ctx.receive_event(bot, next_event) ctx.should_call_send(next_event, "暂时只支持两个门或者三个门的情况,请重新输入吧。", "result") ctx.should_rejected() ctx.receive_event(bot, final_event) ctx.should_call_send(final_event, Message("test"), "result", at_sender=True) ctx.should_finished() randint.assert_called_once_with(1, 2) render_expression.assert_called_once_with(EXPR_GATE, direction="左边")
async def test_history_get_invalid_arg(app: App): """测试历史""" from nonebot.adapters.onebot.v11 import Message from src.plugins.repeat import history_cmd async with app.test_matcher(history_cmd) as ctx: bot = ctx.create_bot() event = fake_group_message_event(message=Message("/history")) year_event = fake_group_message_event(message=Message("2020")) invalid_month_event = fake_group_message_event(message=Message("test")) month_event = fake_group_message_event(message=Message("1")) day_event = fake_group_message_event(message=Message("0")) ctx.receive_event(bot, event) ctx.should_call_send(event, "你请输入你要查询的年份", True) ctx.receive_event(bot, year_event) ctx.should_call_send(year_event, "你请输入你要查询的月份", True) ctx.receive_event(bot, invalid_month_event) ctx.should_call_send(invalid_month_event, "请只输入数字,不然我没法理解呢!", True) ctx.should_rejected() ctx.receive_event(bot, month_event) ctx.should_call_send(month_event, "你请输入你要查询的日期(如查询整月排名请输入 0)", True) ctx.receive_event(bot, day_event) ctx.should_call_send(day_event, "2020 年 1 月的数据不存在,请换个试试吧 0.0", True)
async def test_music_get_arg( app: App, mocker: MockerFixture, ): """测试启动问候已开启的情况""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message, MessageSegment from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.music import music_cmd get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get) async with app.test_matcher(music_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/music")) next_event = fake_group_message_event( message=Message(MessageSegment.image("12"))) final_event = fake_group_message_event(message=Message("test")) ctx.receive_event(bot, event) ctx.should_call_send(event, "你想听哪首歌呢?", "result") ctx.should_rejected() ctx.receive_event(bot, next_event) ctx.should_call_send(next_event, "歌曲名不能为空呢,请重新输入!", "result") ctx.should_rejected() ctx.receive_event(bot, final_event) ctx.should_call_send(final_event, MessageSegment.music("163", 1825190456), "result") ctx.should_finished() get.assert_called_once_with("http://netease:3000/search?keywords=test")
async def test_roll_get_arg( app: App, mocker: MockerFixture, ): """测试点数,获取参数""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.roll import roll_cmd randint = mocker.patch("src.plugins.roll.roll.randint") randint.return_value = 1 async with app.test_matcher(roll_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/roll")) next_event = fake_group_message_event(message=Message("d100")) ctx.receive_event(bot, event) ctx.should_call_send( event, "欢迎使用 NGA 风格 ROLL 点插件\n请问你想怎么 ROLL 点\n你可以输入 d100\n也可以输入 2d100+2d50", "result", ) ctx.should_rejected() ctx.receive_event(bot, next_event) ctx.should_call_send(next_event, "d100=d100(1)=1", "result", at_sender=True) ctx.should_finished() randint.assert_called_once_with(1, 100)
async def test_ban_group_get_arg( app: App, mocker: MockerFixture, sender: dict, ): """测试群聊天,获取参数禁言 1 分钟 机器人为普通群员,禁言对象为管理员或普通群员 """ from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.ban import EXPR_OK, ban_cmd render_expression = mocker.patch("src.plugins.ban.render_expression") render_expression.return_value = Message("test") async with app.test_matcher(ban_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/ban"), sender=sender) next_event = fake_group_message_event(message=Message("1"), sender=sender) ctx.receive_event(bot, event) ctx.should_call_api("get_group_list", data={}, result=[{ "group_id": 10000 }]) ctx.should_call_api( "get_group_member_info", data={ "group_id": 10000, "user_id": 1 }, result={"role": "owner"}, ) ctx.should_call_send(event, "你想被禁言多少分钟呢?", True) ctx.should_rejected() ctx.receive_event(bot, next_event) ctx.should_call_api( "set_group_ban", data={ "group_id": 10000, "user_id": 10, "duration": 60 }, result=[], ) ctx.should_call_send(next_event, Message("test"), True, at_sender=True) ctx.should_finished() render_expression.assert_called_once_with(EXPR_OK, duration=1)
async def test_price_default(app: App, mocker: MockerFixture): """测试查价,默认值""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.ff14 import price_cmd get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get) async with app.test_matcher(price_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/查价 默认值")) ctx.receive_event(bot, event) ctx.should_call_send(event, "当前设置的默认值为:猫小胖", True) ctx.should_finished() async with app.test_matcher(price_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/查价 萨维奈舞裙")) ctx.receive_event(bot, event) ctx.should_call_send( event, "萨维奈舞裙 在市场的价格是:\n233100*1 服务器: 静语庄园\n233193*1 HQ 服务器: 静语庄园\n233330*1 服务器: 静语庄园\n233334*1 服务器: 静语庄园\n239166*1 HQ 服务器: 紫水栈桥\n239166*1 HQ 服务器: 紫水栈桥\n数据更新时间: 2021年12月29日 05时00分", True, ) ctx.should_finished() async with app.test_matcher(price_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/查价 默认值 静语庄园")) ctx.receive_event(bot, event) ctx.should_call_send(event, "查询区域默认值设置成功!", True) ctx.should_finished() async with app.test_matcher(price_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/查价 默认值")) ctx.receive_event(bot, event) ctx.should_call_send(event, "当前设置的默认值为:静语庄园", True) ctx.should_finished() get.assert_has_calls([ mocker.call("https://cafemaker.wakingsands.com/search?string=萨维奈舞裙"), mocker.call("https://universalis.app/api/v2/猫小胖/10393?listings=6"), ])
async def test_eorzean( app: App, mocker: MockerFixture, ): """测试艾欧泽亚天气""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.weather import weather_cmd mocked_time = mocker.patch("src.plugins.weather.eorzean_api.time") mocked_time.time.return_value = 1641619586 async with app.test_matcher(weather_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/天气 利姆萨·罗敏萨")) ctx.receive_event(bot, event) ctx.should_call_send( event, "利姆萨·罗敏萨\n当前天气:碧空\n还剩13分34秒切换到天气:阴云\n还剩36分54秒切换到天气:碧空\n还剩1时0分14秒切换到天气:碧空\n还剩1时23分34秒切换到天气:晴朗\n还剩1时46分54秒切换到天气:碧空\n还剩2时10分14秒切换到天气:碧空\n还剩2时33分34秒切换到天气:碧空\n还剩2时56分54秒切换到天气:晴朗\n还剩3时20分14秒切换到天气:晴朗", "result", ) ctx.should_finished() mocked_time.time.assert_called_once()
async def test_status( app: App, mocker: MockerFixture, ): """测试状态""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.repeat import recorder_obj, status_cmd recorder_obj.start_time = datetime(2020, 1, 1, 0, 0, 0) mocked_datetime = mocker.patch("src.plugins.repeat.status.datetime") mocked_datetime.now.return_value = datetime(2021, 2, 2, 1, 1, 1) mocked_server_status = mocker.patch("src.plugins.repeat.status.server_status") mocked_server_status.return_value = "test" async with app.test_matcher(status_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/status")) ctx.receive_event(bot, event) ctx.should_call_send(event, "已在线 1 年 1 月 1 天 1 小时 1 分钟 1 秒\ntest", "result") ctx.should_finished() mocked_datetime.now.assert_called_once() mocked_server_status.assert_called_once()
async def test_roll_complex( app: App, mocker: MockerFixture, ): """测试点数,复杂点的输入""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.roll import roll_cmd randint = mocker.patch("src.plugins.roll.roll.randint") randint.return_value = 1 async with app.test_matcher(roll_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/roll d100+2d50")) ctx.receive_event(bot, event) ctx.should_call_send( event, "d100+2d50=d100(1)+d50(1)+d50(1)=3", "result", at_sender=True, ) ctx.should_finished() randint.assert_has_calls( [ mocker.call(1, 100), mocker.call(1, 50), mocker.call(1, 50), ] )
async def test_daily_note_full( app: App, mocker: MockerFixture, ): """测试原神实时便笺,体力满的情况""" from nonebot.adapters.onebot.v11 import Message from src.plugins.genshin import daily_note_cmd from src.plugins.genshin.config import set_cookie set_cookie(10, "full") get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get) async with app.test_matcher(daily_note_cmd) as ctx: bot = ctx.create_bot() event = fake_group_message_event(message=Message("/ys.dailynote")) ctx.receive_event(bot, event) ctx.should_call_send( event, "原粹树脂: 160/160 (已完全恢复)\n洞天宝钱: 2400/2400 (已满)\n每日委托任务: 0/4 (今日完成委托次数不足)\n值得铭记的强敌: 0/3 (本周剩余消耗减半次数)\n探索派遣: 5/5\n角色1: 已完成派遣\n角色2: 已完成派遣\n角色3: 已完成派遣\n角色4: 已完成派遣\n角色5: 已完成派遣", "", ) ctx.should_finished() assert get.call_count == 2
async def test_price_world_not_found(app: App, mocker: MockerFixture): """测试查价,服务器/大区不存在""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.ff14 import price_cmd get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get) async with app.test_matcher(price_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/查价 萨维奈舞裙 静语")) ctx.receive_event(bot, event) ctx.should_call_send( event, "抱歉,没有找到 静语 的数据,请检查大区或服务器名称是否正确。", True, ) ctx.should_finished() get.assert_has_calls([ mocker.call("https://cafemaker.wakingsands.com/search?string=萨维奈舞裙"), mocker.call("https://universalis.app/api/v2/静语/10393?listings=6"), ])
async def test_rank(app: App): """测试历史""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.repeat import rank_cmd, recorder_obj recorder_obj._msg_number_list = {10000: {1: {10: 100}}} recorder_obj._repeat_list = {10000: {1: {10: 10}}} async with app.test_matcher(rank_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/rank")) ctx.receive_event(bot, event) ctx.should_call_api( "get_group_member_info", data={ "group_id": 10000, "user_id": 10, "no_cache": True }, result={"card": "test"}, ) ctx.should_call_send( event, "Love Love Ranking\ntest:10.00%\n\n复读次数排行榜\ntest:10次", "result") ctx.should_finished()
async def test_calendar( app: App, mocker: MockerFixture, ): """测试日程""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.pcr import calendar_cmd get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get) mocked_datetime = mocker.patch("src.plugins.pcr.data.datetime") mocked_datetime.now.return_value = datetime(2022, 1, 8) mocked_datetime.strptime = datetime.strptime async with app.test_matcher(calendar_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/pcr.calendar")) ctx.receive_event(bot, event) ctx.should_call_send( event, "一周日程:\n======20220108======\n⨠圣迹2.0倍\n⨠活动:狂奔!\u3000兰德索尔公会竞速赛\n⨠神殿2.0倍\n======20220109======\n⨠圣迹2.0倍\n⨠活动:狂奔!\u3000兰德索尔公会竞速赛\n⨠神殿2.0倍\n======20220110======\n⨠圣迹2.0倍\n⨠地下城2.0倍\n⨠活动:狂奔!\u3000兰德索尔公会竞速赛\n⨠神殿2.0倍\n======20220111======\n⨠圣迹2.0倍\n⨠地下城2.0倍\n⨠活动:狂奔!\u3000兰德索尔公会竞速赛\n⨠神殿2.0倍\n======20220112======\n⨠N图2.0倍\n⨠地下城2.0倍\n⨠活动:狂奔!\u3000兰德索尔公会竞速赛\n======20220113======\n⨠N图2.0倍\n⨠地下城2.0倍\n⨠活动:狂奔!\u3000兰德索尔公会竞速赛\n======20220114======\n⨠N图2.0倍\n⨠地下城2.0倍\n⨠探索2.0倍\n⨠活动:新春破晓之星大危机\n⨠活动:狂奔!\u3000兰德索尔公会竞速赛\n\n更多日程:https://pcrbot.github.io/pcr-calendar/#cn", "result", ) ctx.should_finished() get.assert_called_once_with( "https://pcrbot.github.io/calendar-updater-action/cn.json") assert mocked_datetime.now.call_count == 2
async def test_price_item_not_found(app: App, mocker: MockerFixture): """测试查价,物品不存在""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.ff14 import price_cmd get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get) async with app.test_matcher(price_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/查价 未命名 猫小胖")) ctx.receive_event(bot, event) ctx.should_call_send( event, "抱歉,没有找到 未命名,请检查物品名称是否正确。", True, ) ctx.should_finished() get.assert_called_once_with( "https://cafemaker.wakingsands.com/search?string=未命名")
async def test_nuannuan( app: App, mocker: MockerFixture, ): """测试时尚品鉴""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.ff14 import nuannuan_cmd with open(Path(__file__).parent / "nuannuan.json", "r", encoding="utf-8") as f: data = json.load(f) async_client = mocker.patch("httpx.AsyncClient.get") r = mocker.MagicMock() r.json = mocker.MagicMock(return_value=data) async_client.return_value = r async with app.test_matcher(nuannuan_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/时尚品鉴")) ctx.receive_event(bot, event) ctx.should_call_send( event, "【FF14/时尚品鉴】第204期 满分攻略 12月24日 最终幻想14\n游玩C哩酱攻略站:www.youwanc.com\n-\n【12月24日 第204周时尚品鉴预告预测】\n主题:高贵的魔法师\n\n【身体防具】*提示内容:决斗者\n——*往期:116 92 57\n狂妄长衣 莽撞长衣 匿盗外套 鬼盗外套 红盗外套 瘟疫使者长衣 瘟疫医生长衣 \n60级副本地脉灵灯天狼星灯塔/草木庭园圣茉夏娜植物园\n\n【腿部防具】*提示内容:暗魔\n——*往期:169 139\n暗魔xx打底裤\n24人副本影之国获得\n\n【脚部防具】*提示内容:祭祀仪式\n——*往期:132 115\n真狮鹫革XX凉鞋/XX战靴\n70\nhttps://www.bilibili.com/video/BV1Pq4y1m7wK", "result", ) ctx.should_finished()
async def test_add_sub_bilibili( app: App, mocker: MockerFixture, ): """测试添加订阅,添加 Bilibili""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.web.nonebot_bison import add_sub_cmd config = get_driver().config config.superusers = {"10"} get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get) async with app.test_matcher(add_sub_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/sub.add")) platform_event = fake_group_message_event(message=Message("bilibili")) id_event = fake_group_message_event(message=Message("4549624")) cats_event = fake_group_message_event(message=Message("一般动态,视频,纯文字,转发")) tags_event = fake_group_message_event(message=Message("全部标签")) ctx.receive_event(bot, event) ctx.should_call_send( event, Message( '请输入想要订阅的平台,目前支持:\nbilibili:B站\nrss:RSS\nweibo:新浪微博\n要查看全部平台请输入:"全部"' ), "", ) ctx.receive_event(bot, platform_event) ctx.should_call_send(platform_event, Message("请输入订阅用户的 ID"), "") ctx.receive_event(bot, id_event) ctx.should_call_send( id_event, Message("请输入要订阅的类别,以逗号分隔,支持的类别有:一般动态,专栏文章,视频,纯文字,转发"), "" ) ctx.receive_event(bot, cats_event) ctx.should_call_send(cats_event, Message('请输入要订阅的标签,以逗号分隔,订阅所有标签输入"全部标签"'), "") ctx.receive_event(bot, tags_event) ctx.should_call_send(tags_event, "添加 Liyuu_ 成功", "") ctx.should_finished() get.assert_called_once()
async def test_daily_note_not_bind(app: App): """测试原神实时便笺,未绑定账号""" from nonebot.adapters.onebot.v11 import Message from src.plugins.genshin import daily_note_cmd async with app.test_matcher(daily_note_cmd) as ctx: bot = ctx.create_bot() event = fake_group_message_event(message=Message("/ys.dailynote")) ctx.receive_event(bot, event) ctx.should_call_send(event, "你还没有绑定账号,请私聊机器人绑定账号绑定后查询", "") ctx.should_finished()
async def test_add_sub_rss( app: App, mocker: MockerFixture, ): """测试添加订阅,添加 RSS""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.web.nonebot_bison import add_sub_cmd config = get_driver().config config.superusers = {"10"} get = mocker.patch("httpx.AsyncClient.get", side_effect=mocked_get) async with app.test_matcher(add_sub_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/sub.add")) next_event = fake_group_message_event(message=Message("rss")) final_event = fake_group_message_event( message=Message("https://devblogs.microsoft.com/python/feed/") ) ctx.receive_event(bot, event) ctx.should_call_send( event, Message( '请输入想要订阅的平台,目前支持:\nbilibili:B站\nrss:RSS\nweibo:新浪微博\n要查看全部平台请输入:"全部"' ), "", ) ctx.receive_event(bot, next_event) ctx.should_call_send(next_event, Message("请输入订阅用户的 ID"), "") ctx.receive_event(bot, final_event) ctx.should_call_send(final_event, "添加 Python 成功", "") get.assert_called_once()
async def test_query_sub_empty(app: App): """测试查询订阅,订阅为空的情况""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.web.nonebot_bison import query_sub_cmd async with app.test_matcher(query_sub_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/sub.query")) ctx.receive_event(bot, event) ctx.should_call_send(event, Message("当前无订阅"), "result") ctx.should_finished()
async def test_hello_not_enabled(app: App): """测试启动问候关闭的情况""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.morning import hello_cmd async with app.test_matcher(hello_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/hello")) ctx.receive_event(bot, event) ctx.should_call_send(event, "启动问候功能关闭中", "result") ctx.should_finished()
async def test_help_not_found(app: App): """测试帮助,插件不存在""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.help import help_cmd async with app.test_matcher(help_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/help test")) ctx.receive_event(bot, event) ctx.should_call_send(event, "请输入支持的命令", "result") ctx.should_finished()
async def test_dps_missing_token(app: App): """测试 FFLOGS,缺少 Token 的情况""" from nonebot.adapters.onebot.v11 import Message from src.plugins.ff14 import fflogs_cmd async with app.test_matcher(fflogs_cmd) as ctx: bot = ctx.create_bot() event = fake_group_message_event(message=Message("/dps me")) ctx.receive_event(bot, event) ctx.should_call_send( event, "对不起,Token 未设置,无法查询数据。\n请先使用命令\n/dps token <token>\n配置好 Token 后再尝试查询数据。", "result", ) ctx.should_finished()
async def test_history(app: App): """测试历史""" from nonebot.adapters.onebot.v11 import Message from src.plugins.repeat import history_cmd, recorder_obj recorder_obj._msg_number_list = {10000: {1: {10: 100}}} recorder_obj._repeat_list = {10000: {1: {10: 10}}} async with app.test_matcher(history_cmd) as ctx: bot = ctx.create_bot() event = fake_group_message_event(message=Message("/history 2020-1-0")) ctx.receive_event(bot, event) ctx.should_call_send(event, "2020 年 1 月的数据不存在,请换个试试吧 0.0", True) ctx.should_finished()
async def test_query_sub(app: App): """测试查询订阅""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.web.nonebot_bison import query_sub_cmd from src.web.nonebot_bison.config import DATA with DATA.open("bison.json", "w", encoding="utf8") as f: json.dump( { "user_target": { "1": { "user": 10000, "user_type": "group", "subs": [{ "target": "https://devblogs.microsoft.com/python/feed/", "target_type": "rss", "target_name": "Python", "cats": [], "tags": [], }], } } }, f, ) async with app.test_matcher(query_sub_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/sub.query")) ctx.receive_event(bot, event) ctx.should_call_send( event, Message( "订阅的帐号为:\nrss Python https://devblogs.microsoft.com/python/feed/" ), "result", ) ctx.should_finished()
async def test_morning_enabled(app: App): """测试每日早安已开启的情况""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.morning import morning_cmd, plugin_config plugin_config.morning_group_id = [10000] async with app.test_matcher(morning_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/morning")) ctx.receive_event(bot, event) ctx.should_call_send(event, "每日早安功能开启中", "result") ctx.should_finished()
async def test_delete_sub_empty(app: App): """测试删除订阅,订阅为空的情况""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.web.nonebot_bison import del_sub_cmd config = get_driver().config config.superusers = {"10"} async with app.test_matcher(del_sub_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/sub.delete")) ctx.receive_event(bot, event) ctx.should_call_send(event, "当前无订阅", "result") ctx.should_finished()
async def test_price_help(app: App): """测试查价,参数不足两个的情况""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.ff14 import price_cmd async with app.test_matcher(price_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/查价")) ctx.receive_event(bot, event) ctx.should_call_send( event, "最终幻想XIV 价格查询\n\n查询大区中的最低价格\n/查价 萨维奈舞裙 猫小胖\n查询服务器中的最低价格\n/查价 萨维奈舞裙 静语庄园\n设置默认查询的区域\n/查价 默认值 静语庄园\n查询当前设置的默认值\n/查价 默认值", True, ) ctx.should_finished()
async def test_hello_enable(app: App): """测试启动问候,在群里启用的情况""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.morning import hello_cmd, plugin_config assert plugin_config.hello_group_id == [] async with app.test_matcher(hello_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/hello 1")) ctx.receive_event(bot, event) ctx.should_call_send(event, "已在本群开启启动问候功能", "result") ctx.should_finished() assert plugin_config.hello_group_id == [10000]
async def test_help_help(app: App): """测试帮助,获取帮助插件帮助""" from nonebot import get_driver from nonebot.adapters.onebot.v11 import Adapter, Bot, Message from nonebug.mixin.call_api.fake import make_fake_adapter, make_fake_bot from src.plugins.help import help_cmd async with app.test_matcher(help_cmd) as ctx: adapter = make_fake_adapter(Adapter)(get_driver(), ctx) bot = make_fake_bot(Bot)(adapter, "1") event = fake_group_message_event(message=Message("/help help")) ctx.receive_event(bot, event) ctx.should_call_send( event, "获取帮助\n\n获取所有支持的命令\n/help all\n获取某个命令的帮助\n/help 命令名", "result", ) ctx.should_finished()