Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: bot.call_api 在被 called api hook mock 后应该忽略 exception #2374

Merged
merged 2 commits into from
Sep 25, 2023
Merged

Fix: bot.call_api 在被 called api hook mock 后应该忽略 exception #2374

merged 2 commits into from
Sep 25, 2023

Conversation

Ailitonia
Copy link
Contributor

@Ailitonia Ailitonia commented Sep 24, 2023

fix #2373

@codecov
Copy link

codecov bot commented Sep 24, 2023

Codecov Report

Merging #2374 (7e6035f) into master (ede1a20) will increase coverage by 0.84%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #2374      +/-   ##
==========================================
+ Coverage   90.81%   91.65%   +0.84%     
==========================================
  Files          46       46              
  Lines        3428     3429       +1     
==========================================
+ Hits         3113     3143      +30     
+ Misses        315      286      -29     
Flag Coverage Δ
unittests 91.65% <100.00%> (+0.84%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Changed Coverage Δ
nonebot/internal/adapter/bot.py 89.39% <100.00%> (+43.24%) ⬆️

... and 1 file with indirect coverage changes

@github-actions
Copy link
Contributor

github-actions bot commented Sep 24, 2023

🚀 Deployed on https://deploy-preview-2374--nonebot2.netlify.app

@github-actions github-actions bot temporarily deployed to pull request September 24, 2023 10:04 Inactive
@yanyongyu yanyongyu changed the title 🐛 Fix: 修复 bot.call_api 处理 MockApiException 的逻辑问题 (#2373) Fix: 修复 bot.call_api 处理 MockApiException 的逻辑问题 Sep 24, 2023
@yanyongyu yanyongyu added the bug Something isn't working label Sep 24, 2023
@yanyongyu
Copy link
Member

请补充一下测试用例

@Ailitonia
Copy link
Contributor Author

请补充一下测试用例

nonebug 似乎没有检验 call_api 返回后的 result, 还是这样写有问题?

@pytest.mark.asyncio
async def test_on_called_api_mocked(app: App, monkeypatch: pytest.MonkeyPatch):
    runned = False

    async def test_on_called_api_hook(
        bot: Bot,
        exception: Optional[Exception],
        api: str,
        data: Dict[str, Any],
        result: Any,
    ):
        nonlocal runned
        runned = True
        raise MockApiException(result={"mocked": True})

    async def test_call_api_handler(bot: Bot):
        await bot.call_api("test", test=True)

    with app.provider.context({}):
        matcher = on_message()
        matcher.handle()(test_call_api_handler)

        async with app.test_matcher(matcher) as ctx:
            bot = ctx.create_bot()
            bot.on_called_api(test_on_called_api_hook)
            event = make_fake_event()()
            ctx.receive_event(bot, event)
            ctx.should_call_api("test", {"test": True}, {"mocked": True})

    assert runned, "on_called_api_hook should runned"

nonebug 这里对 call_api 返回的 result 没有做任何检验
https://github.com/nonebot/nonebug/blob/6345a32957ab570f60ff3c242e48cfc8e06a7d2f/nonebug/mixin/call_api/__init__.py#L163-L186

@yanyongyu yanyongyu changed the title Fix: 修复 bot.call_api 处理 MockApiException 的逻辑问题 Fix: bot.call_api 在被 called api hook mock后应该忽略 exception Sep 25, 2023
@yanyongyu yanyongyu changed the title Fix: bot.call_api 在被 called api hook mock后应该忽略 exception Fix: bot.call_api 在被 called api hook mock 后应该忽略 exception Sep 25, 2023
@github-actions github-actions bot temporarily deployed to pull request September 25, 2023 02:57 Inactive
@yanyongyu yanyongyu merged commit 60e0752 into nonebot:master Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging this pull request may close these issues.

Bug: on_called_api 后处理异常的逻辑问题
2 participants