mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2026-05-13 20:38:39 +08:00
inject http client to scheduler
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import pytest
|
||||
import respx
|
||||
from httpx import Response
|
||||
from httpx import AsyncClient, Response
|
||||
from nonebug.app import App
|
||||
|
||||
from .utils import get_file, get_json
|
||||
@@ -10,7 +10,7 @@ from .utils import get_file, get_json
|
||||
def arknights(app: App):
|
||||
from nonebot_bison.platform import platform_manager
|
||||
|
||||
return platform_manager["arknights"]
|
||||
return platform_manager["arknights"](AsyncClient())
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
|
||||
@@ -3,7 +3,7 @@ from datetime import datetime
|
||||
|
||||
import pytest
|
||||
import respx
|
||||
from httpx import Response
|
||||
from httpx import AsyncClient, Response
|
||||
from nonebug.app import App
|
||||
from pytz import timezone
|
||||
|
||||
@@ -23,7 +23,7 @@ if typing.TYPE_CHECKING:
|
||||
def bilibili(app: App):
|
||||
from nonebot_bison.platform import platform_manager
|
||||
|
||||
return platform_manager["bilibili"]
|
||||
return platform_manager["bilibili"](AsyncClient())
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
@@ -2,7 +2,7 @@ import typing
|
||||
|
||||
import pytest
|
||||
import respx
|
||||
from httpx import Response
|
||||
from httpx import AsyncClient, Response
|
||||
from nonebug.app import App
|
||||
|
||||
from .utils import get_json
|
||||
@@ -15,7 +15,7 @@ if typing.TYPE_CHECKING:
|
||||
def bili_bangumi(app: App):
|
||||
from nonebot_bison.platform import platform_manager
|
||||
|
||||
return platform_manager["bilibili-bangumi"]
|
||||
return platform_manager["bilibili-bangumi"](AsyncClient())
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import pytest
|
||||
import respx
|
||||
from httpx import Response
|
||||
from httpx import AsyncClient, Response
|
||||
from nonebug.app import App
|
||||
|
||||
from .utils import get_json
|
||||
@@ -10,7 +10,7 @@ from .utils import get_json
|
||||
def bili_live(app: App):
|
||||
from nonebot_bison.platform import platform_manager
|
||||
|
||||
return platform_manager["bilibili-live"]
|
||||
return platform_manager["bilibili-live"](AsyncClient())
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import pytest
|
||||
import respx
|
||||
from httpx import Response
|
||||
from httpx import AsyncClient, Response
|
||||
from nonebug.app import App
|
||||
|
||||
from .utils import get_json
|
||||
@@ -10,7 +10,7 @@ from .utils import get_json
|
||||
def ff14(app: App):
|
||||
from nonebot_bison.platform import platform_manager
|
||||
|
||||
return platform_manager["ff14"]
|
||||
return platform_manager["ff14"](AsyncClient())
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import pytest
|
||||
import respx
|
||||
from httpx import Response
|
||||
from httpx import AsyncClient, Response
|
||||
from nonebug.app import App
|
||||
|
||||
from .utils import get_file, get_json
|
||||
@@ -10,7 +10,7 @@ from .utils import get_file, get_json
|
||||
def mcbbsnews(app: App):
|
||||
from nonebot_bison.platform import platform_manager
|
||||
|
||||
return platform_manager["mcbbsnews"]
|
||||
return platform_manager["mcbbsnews"](AsyncClient())
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
|
||||
@@ -3,20 +3,20 @@ import typing
|
||||
|
||||
import pytest
|
||||
import respx
|
||||
from httpx import Response
|
||||
from httpx import AsyncClient, Response
|
||||
from nonebug.app import App
|
||||
|
||||
from .utils import get_json
|
||||
|
||||
if typing.TYPE_CHECKING:
|
||||
from nonebot_bison.platform.ncm_artist import NcmArtist
|
||||
from nonebot_bison.platform.ncm import NcmArtist
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ncm_artist(app: App):
|
||||
from nonebot_bison.platform import platform_manager
|
||||
|
||||
return platform_manager["ncm-artist"]
|
||||
return platform_manager["ncm-artist"](AsyncClient())
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
|
||||
@@ -3,20 +3,20 @@ import typing
|
||||
|
||||
import pytest
|
||||
import respx
|
||||
from httpx import Response
|
||||
from httpx import AsyncClient, Response
|
||||
from nonebug.app import App
|
||||
|
||||
from .utils import get_json
|
||||
|
||||
if typing.TYPE_CHECKING:
|
||||
from nonebot_bison.platform.ncm_radio import NcmRadio
|
||||
from nonebot_bison.platform.ncm import NcmRadio
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def ncm_radio(app: App):
|
||||
from nonebot_bison.platform import platform_manager
|
||||
|
||||
return platform_manager["ncm-radio"]
|
||||
return platform_manager["ncm-radio"](AsyncClient())
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
|
||||
@@ -53,12 +53,12 @@ def mock_platform_without_cats_tags(app: App):
|
||||
categories = {}
|
||||
has_target = True
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, client):
|
||||
self.sub_index = 0
|
||||
super().__init__()
|
||||
super().__init__(client)
|
||||
|
||||
@staticmethod
|
||||
async def get_target_name(_: "Target"):
|
||||
@classmethod
|
||||
async def get_target_name(cls, client, _: "Target"):
|
||||
return "MockPlatform"
|
||||
|
||||
def get_id(self, post: "RawPost") -> Any:
|
||||
@@ -82,7 +82,7 @@ def mock_platform_without_cats_tags(app: App):
|
||||
else:
|
||||
return raw_post_list_2
|
||||
|
||||
return MockPlatform()
|
||||
return MockPlatform(None)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -112,9 +112,9 @@ def mock_platform(app: App):
|
||||
Category(2): "视频",
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, client):
|
||||
self.sub_index = 0
|
||||
super().__init__()
|
||||
super().__init__(client)
|
||||
|
||||
@staticmethod
|
||||
async def get_target_name(_: "Target"):
|
||||
@@ -147,7 +147,7 @@ def mock_platform(app: App):
|
||||
else:
|
||||
return raw_post_list_2
|
||||
|
||||
return MockPlatform()
|
||||
return MockPlatform(None)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -180,9 +180,9 @@ def mock_platform_no_target(app: App, mock_scheduler_conf):
|
||||
has_target = False
|
||||
categories = {Category(1): "转发", Category(2): "视频", Category(3): "不支持"}
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, client):
|
||||
self.sub_index = 0
|
||||
super().__init__()
|
||||
super().__init__(client)
|
||||
|
||||
@staticmethod
|
||||
async def get_target_name(_: "Target"):
|
||||
@@ -217,7 +217,7 @@ def mock_platform_no_target(app: App, mock_scheduler_conf):
|
||||
else:
|
||||
return raw_post_list_2
|
||||
|
||||
return MockPlatform()
|
||||
return MockPlatform
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -241,12 +241,12 @@ def mock_platform_no_target_2(app: App, mock_scheduler_conf):
|
||||
Category(5): "leixing5",
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, client):
|
||||
self.sub_index = 0
|
||||
super().__init__()
|
||||
super().__init__(client)
|
||||
|
||||
@staticmethod
|
||||
async def get_target_name(_: "Target"):
|
||||
@classmethod
|
||||
async def get_target_name(cls, client, _: "Target"):
|
||||
return "MockPlatform"
|
||||
|
||||
def get_id(self, post: "RawPost") -> Any:
|
||||
@@ -284,7 +284,7 @@ def mock_platform_no_target_2(app: App, mock_scheduler_conf):
|
||||
else:
|
||||
return list_2
|
||||
|
||||
return MockPlatform()
|
||||
return MockPlatform
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -308,9 +308,9 @@ def mock_status_change(app: App):
|
||||
Category(2): "视频",
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, client):
|
||||
self.sub_index = 0
|
||||
super().__init__()
|
||||
super().__init__(client)
|
||||
|
||||
async def get_status(self, _: "Target"):
|
||||
if self.sub_index == 0:
|
||||
@@ -335,7 +335,7 @@ def mock_status_change(app: App):
|
||||
def get_category(self, raw_post):
|
||||
return raw_post["cat"]
|
||||
|
||||
return MockPlatform()
|
||||
return MockPlatform(None)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -388,6 +388,7 @@ 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):
|
||||
mock_platform_no_target = mock_platform_no_target(None)
|
||||
res1 = await mock_platform_no_target.fetch_new_post(
|
||||
"dummy", [user_info_factory([1, 2], [])]
|
||||
)
|
||||
@@ -457,11 +458,14 @@ async def test_group(
|
||||
user_info_factory,
|
||||
):
|
||||
|
||||
from nonebot_bison.platform.platform import NoTargetGroup
|
||||
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
|
||||
|
||||
group_platform = NoTargetGroup([mock_platform_no_target, mock_platform_no_target_2])
|
||||
group_platform_class = make_no_target_group(
|
||||
[mock_platform_no_target, mock_platform_no_target_2]
|
||||
)
|
||||
group_platform = group_platform_class(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], [])])
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import pytest
|
||||
from httpx import AsyncClient
|
||||
from nonebug.app import App
|
||||
|
||||
from .utils import get_json
|
||||
@@ -14,7 +15,7 @@ def test_cases():
|
||||
async def test_filter_user_custom_tag(app: App, test_cases):
|
||||
from nonebot_bison.platform import platform_manager
|
||||
|
||||
bilibili = platform_manager["bilibili"]
|
||||
bilibili = platform_manager["bilibili"](AsyncClient())
|
||||
for case in test_cases:
|
||||
res = bilibili.is_banned_post(**case["case"])
|
||||
assert res == case["result"]
|
||||
@@ -25,7 +26,7 @@ async def test_filter_user_custom_tag(app: App, test_cases):
|
||||
async def test_tag_separator(app: App):
|
||||
from nonebot_bison.platform import platform_manager
|
||||
|
||||
bilibili = platform_manager["bilibili"]
|
||||
bilibili = platform_manager["bilibili"](AsyncClient())
|
||||
tags = ["~111", "222", "333", "~444", "555"]
|
||||
res = bilibili.tag_separator(tags)
|
||||
assert res[0] == ["222", "333", "555"]
|
||||
|
||||
@@ -4,7 +4,7 @@ from datetime import datetime
|
||||
import feedparser
|
||||
import pytest
|
||||
import respx
|
||||
from httpx import Response
|
||||
from httpx import AsyncClient, Response
|
||||
from nonebug.app import App
|
||||
from pytz import timezone
|
||||
|
||||
@@ -18,7 +18,7 @@ if typing.TYPE_CHECKING:
|
||||
def weibo(app: App):
|
||||
from nonebot_bison.platform import platform_manager
|
||||
|
||||
return platform_manager["weibo"]
|
||||
return platform_manager["weibo"](AsyncClient())
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
@@ -35,7 +35,7 @@ async def test_get_name(weibo):
|
||||
profile_router.mock(
|
||||
return_value=Response(200, json=get_json("weibo_ak_profile.json"))
|
||||
)
|
||||
name = await weibo.get_target_name("6279793937")
|
||||
name = await weibo.get_target_name(AsyncClient(), "6279793937")
|
||||
assert name == "明日方舟Arknights"
|
||||
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ from .utils import BotReply, fake_admin_user, fake_group_message_event
|
||||
# 选择platform阶段中止
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_abort_add_on_platform(app: App, db_migration):
|
||||
async def test_abort_add_on_platform(app: App, init_scheduler):
|
||||
from nonebot.adapters.onebot.v11.event import Sender
|
||||
from nonebot.adapters.onebot.v11.message import Message
|
||||
from nonebot_bison.config_manager import add_sub_matcher, common_platform
|
||||
@@ -57,7 +57,7 @@ async def test_abort_add_on_platform(app: App, db_migration):
|
||||
# 输入id阶段中止
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_abort_add_on_id(app: App, db_migration):
|
||||
async def test_abort_add_on_id(app: App, init_scheduler):
|
||||
from nonebot.adapters.onebot.v11.event import Sender
|
||||
from nonebot.adapters.onebot.v11.message import Message
|
||||
from nonebot_bison.config_manager import add_sub_matcher, common_platform
|
||||
@@ -114,7 +114,7 @@ async def test_abort_add_on_id(app: App, db_migration):
|
||||
# 输入订阅类别阶段中止
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_abort_add_on_cats(app: App, db_migration):
|
||||
async def test_abort_add_on_cats(app: App, init_scheduler):
|
||||
from nonebot.adapters.onebot.v11.event import Sender
|
||||
from nonebot.adapters.onebot.v11.message import Message
|
||||
from nonebot_bison.config_manager import add_sub_matcher, common_platform
|
||||
@@ -191,7 +191,7 @@ async def test_abort_add_on_cats(app: App, db_migration):
|
||||
# 输入标签阶段中止
|
||||
@pytest.mark.asyncio
|
||||
@respx.mock
|
||||
async def test_abort_add_on_tag(app: App, db_migration):
|
||||
async def test_abort_add_on_tag(app: App, init_scheduler):
|
||||
from nonebot.adapters.onebot.v11.event import Sender
|
||||
from nonebot.adapters.onebot.v11.message import Message
|
||||
from nonebot_bison.config_manager import add_sub_matcher, common_platform
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from email import message
|
||||
|
||||
import pytest
|
||||
import respx
|
||||
from httpx import Response
|
||||
@@ -189,7 +187,7 @@ async def test_add_with_target_no_cat(app: App, init_scheduler):
|
||||
from nonebot_bison.config import config
|
||||
from nonebot_bison.config_manager import add_sub_matcher, common_platform
|
||||
from nonebot_bison.platform import platform_manager
|
||||
from nonebot_bison.platform.ncm_artist import NcmArtist
|
||||
from nonebot_bison.platform.ncm import NcmArtist
|
||||
|
||||
ncm_router = respx.get("https://music.163.com/api/artist/albums/32540734")
|
||||
ncm_router.mock(return_value=Response(200, json=get_json("ncm_siren.json")))
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import typing
|
||||
|
||||
import pytest
|
||||
from httpx import AsyncClient
|
||||
from nonebug.app import App
|
||||
|
||||
|
||||
@@ -29,7 +30,7 @@ VuePress 由两部分组成:第一部分是一个极简静态网站生成器
|
||||
async def test_arknights(app: App):
|
||||
from nonebot_bison.platform.arknights import Arknights
|
||||
|
||||
ak = Arknights()
|
||||
ak = Arknights(AsyncClient())
|
||||
res = await ak.parse(
|
||||
{"webUrl": "https://ak.hycdn.cn/announce/IOS/announcement/854_1644580545.html"}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user