mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2026-05-09 18:27:56 +08:00
✨ add context to log http error
This commit is contained in:
@@ -9,8 +9,9 @@ from .utils import get_file, get_json
|
||||
@pytest.fixture
|
||||
def arknights(app: App):
|
||||
from nonebot_bison.platform import platform_manager
|
||||
from nonebot_bison.utils import ProcessContext
|
||||
|
||||
return platform_manager["arknights"](AsyncClient())
|
||||
return platform_manager["arknights"](ProcessContext(), AsyncClient())
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
|
||||
@@ -22,8 +22,9 @@ if typing.TYPE_CHECKING:
|
||||
@pytest.fixture
|
||||
def bilibili(app: App):
|
||||
from nonebot_bison.platform import platform_manager
|
||||
from nonebot_bison.utils import ProcessContext
|
||||
|
||||
return platform_manager["bilibili"](AsyncClient())
|
||||
return platform_manager["bilibili"](ProcessContext(), AsyncClient())
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
@@ -14,8 +14,9 @@ if typing.TYPE_CHECKING:
|
||||
@pytest.fixture
|
||||
def bili_bangumi(app: App):
|
||||
from nonebot_bison.platform import platform_manager
|
||||
from nonebot_bison.utils import ProcessContext
|
||||
|
||||
return platform_manager["bilibili-bangumi"](AsyncClient())
|
||||
return platform_manager["bilibili-bangumi"](ProcessContext(), AsyncClient())
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
@@ -9,8 +9,9 @@ from .utils import get_json
|
||||
@pytest.fixture
|
||||
def bili_live(app: App):
|
||||
from nonebot_bison.platform import platform_manager
|
||||
from nonebot_bison.utils import ProcessContext
|
||||
|
||||
return platform_manager["bilibili-live"](AsyncClient())
|
||||
return platform_manager["bilibili-live"](ProcessContext(), AsyncClient())
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
||||
@@ -9,8 +9,9 @@ from .utils import get_json
|
||||
@pytest.fixture
|
||||
def ff14(app: App):
|
||||
from nonebot_bison.platform import platform_manager
|
||||
from nonebot_bison.utils import ProcessContext
|
||||
|
||||
return platform_manager["ff14"](AsyncClient())
|
||||
return platform_manager["ff14"](ProcessContext(), AsyncClient())
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
|
||||
@@ -9,8 +9,9 @@ from .utils import get_file, get_json
|
||||
@pytest.fixture
|
||||
def mcbbsnews(app: App):
|
||||
from nonebot_bison.platform import platform_manager
|
||||
from nonebot_bison.utils import ProcessContext
|
||||
|
||||
return platform_manager["mcbbsnews"](AsyncClient())
|
||||
return platform_manager["mcbbsnews"](ProcessContext(), AsyncClient())
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
|
||||
@@ -15,8 +15,9 @@ if typing.TYPE_CHECKING:
|
||||
@pytest.fixture
|
||||
def ncm_artist(app: App):
|
||||
from nonebot_bison.platform import platform_manager
|
||||
from nonebot_bison.utils import ProcessContext
|
||||
|
||||
return platform_manager["ncm-artist"](AsyncClient())
|
||||
return platform_manager["ncm-artist"](ProcessContext(), AsyncClient())
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
|
||||
@@ -15,8 +15,9 @@ if typing.TYPE_CHECKING:
|
||||
@pytest.fixture
|
||||
def ncm_radio(app: App):
|
||||
from nonebot_bison.platform import platform_manager
|
||||
from nonebot_bison.utils import ProcessContext
|
||||
|
||||
return platform_manager["ncm-radio"](AsyncClient())
|
||||
return platform_manager["ncm-radio"](ProcessContext(), AsyncClient())
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
from time import time
|
||||
from typing import Any, Optional
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
import pytest
|
||||
from httpx import AsyncClient
|
||||
from nonebug.app import App
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from nonebot_bison.platform import Platform
|
||||
|
||||
now = time()
|
||||
passed = now - 3 * 60 * 60
|
||||
|
||||
@@ -56,9 +59,6 @@ def mock_platform_without_cats_tags(app: App):
|
||||
|
||||
sub_index = 0
|
||||
|
||||
def __init__(self, client):
|
||||
super().__init__(client)
|
||||
|
||||
@classmethod
|
||||
async def get_target_name(cls, client, _: "Target"):
|
||||
return "MockPlatform"
|
||||
@@ -117,9 +117,6 @@ def mock_platform(app: App):
|
||||
|
||||
sub_index = 0
|
||||
|
||||
def __init__(self, client):
|
||||
super().__init__(client)
|
||||
|
||||
@staticmethod
|
||||
async def get_target_name(_: "Target"):
|
||||
return "MockPlatform"
|
||||
@@ -187,9 +184,6 @@ def mock_platform_no_target(app: App, mock_scheduler_conf):
|
||||
|
||||
sub_index = 0
|
||||
|
||||
def __init__(self, client):
|
||||
super().__init__(client)
|
||||
|
||||
@staticmethod
|
||||
async def get_target_name(_: "Target"):
|
||||
return "MockPlatform"
|
||||
@@ -250,9 +244,6 @@ def mock_platform_no_target_2(app: App, mock_scheduler_conf):
|
||||
|
||||
sub_index = 0
|
||||
|
||||
def __init__(self, client):
|
||||
super().__init__(client)
|
||||
|
||||
@classmethod
|
||||
async def get_target_name(cls, client, _: "Target"):
|
||||
return "MockPlatform"
|
||||
@@ -319,9 +310,6 @@ def mock_status_change(app: App):
|
||||
|
||||
sub_index = 0
|
||||
|
||||
def __init__(self, client):
|
||||
super().__init__(client)
|
||||
|
||||
@classmethod
|
||||
async def get_status(cls, _: "Target"):
|
||||
if cls.sub_index == 0:
|
||||
@@ -353,11 +341,15 @@ def mock_status_change(app: App):
|
||||
async def test_new_message_target_without_cats_tags(
|
||||
mock_platform_without_cats_tags, user_info_factory
|
||||
):
|
||||
res1 = await mock_platform_without_cats_tags(AsyncClient()).fetch_new_post(
|
||||
"dummy", [user_info_factory([1, 2], [])]
|
||||
)
|
||||
from nonebot_bison.utils import ProcessContext
|
||||
|
||||
res1 = await mock_platform_without_cats_tags(
|
||||
ProcessContext(), AsyncClient()
|
||||
).fetch_new_post("dummy", [user_info_factory([1, 2], [])])
|
||||
assert len(res1) == 0
|
||||
res2 = await mock_platform_without_cats_tags(AsyncClient()).fetch_new_post(
|
||||
res2 = await mock_platform_without_cats_tags(
|
||||
ProcessContext(), AsyncClient()
|
||||
).fetch_new_post(
|
||||
"dummy",
|
||||
[
|
||||
user_info_factory([], []),
|
||||
@@ -372,11 +364,13 @@ async def test_new_message_target_without_cats_tags(
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_new_message_target(mock_platform, user_info_factory):
|
||||
res1 = await mock_platform(AsyncClient()).fetch_new_post(
|
||||
from nonebot_bison.utils import ProcessContext
|
||||
|
||||
res1 = await mock_platform(ProcessContext(), AsyncClient()).fetch_new_post(
|
||||
"dummy", [user_info_factory([1, 2], [])]
|
||||
)
|
||||
assert len(res1) == 0
|
||||
res2 = await mock_platform(AsyncClient()).fetch_new_post(
|
||||
res2 = await mock_platform(ProcessContext(), AsyncClient()).fetch_new_post(
|
||||
"dummy",
|
||||
[
|
||||
user_info_factory([1, 2], []),
|
||||
@@ -401,11 +395,15 @@ async def test_new_message_target(mock_platform, user_info_factory):
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_new_message_no_target(mock_platform_no_target, user_info_factory):
|
||||
res1 = await mock_platform_no_target(AsyncClient()).fetch_new_post(
|
||||
"dummy", [user_info_factory([1, 2], [])]
|
||||
)
|
||||
from nonebot_bison.utils import ProcessContext
|
||||
|
||||
res1 = await mock_platform_no_target(
|
||||
ProcessContext(), AsyncClient()
|
||||
).fetch_new_post("dummy", [user_info_factory([1, 2], [])])
|
||||
assert len(res1) == 0
|
||||
res2 = await mock_platform_no_target(AsyncClient()).fetch_new_post(
|
||||
res2 = await mock_platform_no_target(
|
||||
ProcessContext(), AsyncClient()
|
||||
).fetch_new_post(
|
||||
"dummy",
|
||||
[
|
||||
user_info_factory([1, 2], []),
|
||||
@@ -426,26 +424,28 @@ async def test_new_message_no_target(mock_platform_no_target, user_info_factory)
|
||||
assert "p2" in id_set_1 and "p3" in id_set_1
|
||||
assert "p2" in id_set_2
|
||||
assert "p2" in id_set_3
|
||||
res3 = await mock_platform_no_target(AsyncClient()).fetch_new_post(
|
||||
"dummy", [user_info_factory([1, 2], [])]
|
||||
)
|
||||
res3 = await mock_platform_no_target(
|
||||
ProcessContext(), AsyncClient()
|
||||
).fetch_new_post("dummy", [user_info_factory([1, 2], [])])
|
||||
assert len(res3) == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_status_change(mock_status_change, user_info_factory):
|
||||
res1 = await mock_status_change(AsyncClient()).fetch_new_post(
|
||||
from nonebot_bison.utils import ProcessContext
|
||||
|
||||
res1 = await mock_status_change(ProcessContext(), AsyncClient()).fetch_new_post(
|
||||
"dummy", [user_info_factory([1, 2], [])]
|
||||
)
|
||||
assert len(res1) == 0
|
||||
res2 = await mock_status_change(AsyncClient()).fetch_new_post(
|
||||
res2 = await mock_status_change(ProcessContext(), AsyncClient()).fetch_new_post(
|
||||
"dummy", [user_info_factory([1, 2], [])]
|
||||
)
|
||||
assert len(res2) == 1
|
||||
posts = res2[0][1]
|
||||
assert len(posts) == 1
|
||||
assert posts[0].text == "on"
|
||||
res3 = await mock_status_change(AsyncClient()).fetch_new_post(
|
||||
res3 = await mock_status_change(ProcessContext(), AsyncClient()).fetch_new_post(
|
||||
"dummy",
|
||||
[
|
||||
user_info_factory([1, 2], []),
|
||||
@@ -456,7 +456,7 @@ async def test_status_change(mock_status_change, user_info_factory):
|
||||
assert len(res3[0][1]) == 1
|
||||
assert res3[0][1][0].text == "off"
|
||||
assert len(res3[1][1]) == 0
|
||||
res4 = await mock_status_change(AsyncClient()).fetch_new_post(
|
||||
res4 = await mock_status_change(ProcessContext(), AsyncClient()).fetch_new_post(
|
||||
"dummy", [user_info_factory([1, 2], [])]
|
||||
)
|
||||
assert len(res4) == 0
|
||||
@@ -473,11 +473,12 @@ async def test_group(
|
||||
from nonebot_bison.platform.platform import make_no_target_group
|
||||
from nonebot_bison.post import Post
|
||||
from nonebot_bison.types import Category, RawPost, Tag, Target
|
||||
from nonebot_bison.utils import ProcessContext
|
||||
|
||||
group_platform_class = make_no_target_group(
|
||||
[mock_platform_no_target, mock_platform_no_target_2]
|
||||
)
|
||||
group_platform = group_platform_class(None)
|
||||
group_platform = group_platform_class(ProcessContext(), None)
|
||||
res1 = await group_platform.fetch_new_post("dummy", [user_info_factory([1, 4], [])])
|
||||
assert len(res1) == 0
|
||||
res2 = await group_platform.fetch_new_post("dummy", [user_info_factory([1, 4], [])])
|
||||
|
||||
@@ -14,8 +14,9 @@ def test_cases():
|
||||
@pytest.mark.asyncio
|
||||
async def test_filter_user_custom_tag(app: App, test_cases):
|
||||
from nonebot_bison.platform import platform_manager
|
||||
from nonebot_bison.utils import ProcessContext
|
||||
|
||||
bilibili = platform_manager["bilibili"](AsyncClient())
|
||||
bilibili = platform_manager["bilibili"](ProcessContext(), AsyncClient())
|
||||
for case in test_cases:
|
||||
res = bilibili.is_banned_post(**case["case"])
|
||||
assert res == case["result"]
|
||||
@@ -25,8 +26,9 @@ async def test_filter_user_custom_tag(app: App, test_cases):
|
||||
@pytest.mark.asyncio
|
||||
async def test_tag_separator(app: App):
|
||||
from nonebot_bison.platform import platform_manager
|
||||
from nonebot_bison.utils import ProcessContext
|
||||
|
||||
bilibili = platform_manager["bilibili"](AsyncClient())
|
||||
bilibili = platform_manager["bilibili"](ProcessContext(), AsyncClient())
|
||||
tags = ["~111", "222", "333", "~444", "555"]
|
||||
res = bilibili.tag_separator(tags)
|
||||
assert res[0] == ["222", "333", "555"]
|
||||
|
||||
@@ -21,8 +21,9 @@ image_cdn_router = respx.route(
|
||||
@pytest.fixture
|
||||
def weibo(app: App):
|
||||
from nonebot_bison.platform import platform_manager
|
||||
from nonebot_bison.utils import ProcessContext
|
||||
|
||||
return platform_manager["weibo"](AsyncClient())
|
||||
return platform_manager["weibo"](ProcessContext(), AsyncClient())
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import httpx
|
||||
import respx
|
||||
from nonebug.app import App
|
||||
|
||||
|
||||
@respx.mock
|
||||
async def test_http_error(app: App):
|
||||
from nonebot_bison.utils import ProcessContext, http_client
|
||||
|
||||
example_route = respx.get("https://example.com")
|
||||
example_route.mock(httpx.Response(403, json={"error": "gg"}))
|
||||
|
||||
ctx = ProcessContext()
|
||||
async with http_client() as client:
|
||||
ctx.register_to_client(client)
|
||||
await client.get("https://example.com")
|
||||
|
||||
assert ctx.gen_req_records() == [
|
||||
"https://example.com Headers({'host': 'example.com', 'accept': '*/*', 'accept-encoding': 'gzip, deflate', 'connection': 'keep-alive', 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'}) | [403] Headers({'content-length': '15', 'content-type': 'application/json'}) {\"error\": \"gg\"}"
|
||||
]
|
||||
Reference in New Issue
Block a user