diff --git a/bot.py b/bot.py index aa0cfdf..8af708d 100644 --- a/bot.py +++ b/bot.py @@ -12,4 +12,4 @@ nonebot.load_builtin_plugins("echo") nonebot.load_plugins("src/plugins") if __name__ == "__main__": - nonebot.run(app="bot:app") + nonebot.run() diff --git a/src/plugins/nonebot_bison/__init__.py b/src/plugins/nonebot_bison/__init__.py index 9083158..2fb5951 100644 --- a/src/plugins/nonebot_bison/__init__.py +++ b/src/plugins/nonebot_bison/__init__.py @@ -15,3 +15,4 @@ from . import ( __help__version__ = "0.4.3" __help__plugin__name__ = "nonebot_bison" __usage__ = "本bot可以提供b站、微博等社交媒体的消息订阅,详情" "请查看本bot文档,或者at本bot发送“添加订阅”订阅第一个帐号" +__module_name__ = "nonebot-bison" diff --git a/src/plugins/nonebot_bison/scheduler.py b/src/plugins/nonebot_bison/scheduler.py index 956c51f..03ddd48 100644 --- a/src/plugins/nonebot_bison/scheduler.py +++ b/src/plugins/nonebot_bison/scheduler.py @@ -12,7 +12,7 @@ from .platform import platform_manager from .plugin_config import plugin_config from .send import send_msgs, do_send_msgs -scheduler = AsyncIOScheduler() +scheduler = AsyncIOScheduler(timezone="Asia/Shanghai") @get_driver().on_startup diff --git a/src/plugins/nonebot_bison/utils.py b/src/plugins/nonebot_bison/utils.py index 4035ac3..53a00c0 100644 --- a/src/plugins/nonebot_bison/utils.py +++ b/src/plugins/nonebot_bison/utils.py @@ -219,3 +219,4 @@ if plugin_config.bison_filter_log: if config.log_level is None else config.log_level ) + logger.warning("test") diff --git a/tests/conftest.py b/tests/conftest.py index 06a7148..dc8e4ac 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,36 +1,23 @@ import typing +from pathlib import Path import pytest import nonebot -from nonebug.fixture import nonebug_init, nonebug_clear - -if typing.TYPE_CHECKING: - import sys - - sys.path.append("./src/plugins") - import nonebot_bison - - -@pytest.fixture # (scope="module") -def plugin_module(tmpdir): - nonebot.init(bison_config_path=str(tmpdir)) - nonebot.load_plugins("src/plugins") - plugins = nonebot.get_loaded_plugins() - plugin = list(filter(lambda x: x.name == "nonebot_bison", plugins))[0] - return plugin.module +from nonebug.app import App @pytest.fixture -def dummy_user_subinfo(plugin_module: "nonebot_bison"): - user = plugin_module.types.User("123", "group") - return plugin_module.types.UserSubInfo( - user=user, category_getter=lambda _: [], tag_getter=lambda _: [] - ) +async def app(nonebug_init: None, tmp_path: Path, monkeypatch: pytest.MonkeyPatch): + import nonebot + + config = nonebot.get_driver().config + config.bison_config_path = str(tmp_path) + return App(monkeypatch) @pytest.fixture -def n_plugin_module(tmpdir): - nonebot.init(bison_config_path=str(tmpdir)) - nonebot.load_plugins("src/plugins") - yield None - nonebug_clear() +def dummy_user_subinfo(app: App): + from nonebot_bison.types import User, UserSubInfo + + user = User("123", "group") + return UserSubInfo(user=user, category_getter=lambda _: [], tag_getter=lambda _: []) diff --git a/tests/platforms/arknights-detail-805 b/tests/platforms/static/arknights-detail-805 similarity index 100% rename from tests/platforms/arknights-detail-805 rename to tests/platforms/static/arknights-detail-805 diff --git a/tests/platforms/arknights-pre-0.json b/tests/platforms/static/arknights-pre-0.json similarity index 100% rename from tests/platforms/arknights-pre-0.json rename to tests/platforms/static/arknights-pre-0.json diff --git a/tests/platforms/arknights-server-open.json b/tests/platforms/static/arknights-server-open.json similarity index 100% rename from tests/platforms/arknights-server-open.json rename to tests/platforms/static/arknights-server-open.json diff --git a/tests/platforms/arknights-version-0.json b/tests/platforms/static/arknights-version-0.json similarity index 100% rename from tests/platforms/arknights-version-0.json rename to tests/platforms/static/arknights-version-0.json diff --git a/tests/platforms/arknights_list_0.json b/tests/platforms/static/arknights_list_0.json similarity index 100% rename from tests/platforms/arknights_list_0.json rename to tests/platforms/static/arknights_list_0.json diff --git a/tests/platforms/arknights_list_1.json b/tests/platforms/static/arknights_list_1.json similarity index 100% rename from tests/platforms/arknights_list_1.json rename to tests/platforms/static/arknights_list_1.json diff --git a/tests/platforms/bilibili_bing_list.json b/tests/platforms/static/bilibili_bing_list.json similarity index 100% rename from tests/platforms/bilibili_bing_list.json rename to tests/platforms/static/bilibili_bing_list.json diff --git a/tests/platforms/monster-siren_list_0.json b/tests/platforms/static/monster-siren_list_0.json similarity index 100% rename from tests/platforms/monster-siren_list_0.json rename to tests/platforms/static/monster-siren_list_0.json diff --git a/tests/platforms/monster-siren_list_1.json b/tests/platforms/static/monster-siren_list_1.json similarity index 100% rename from tests/platforms/monster-siren_list_1.json rename to tests/platforms/static/monster-siren_list_1.json diff --git a/tests/platforms/ncm_radio_ark.json b/tests/platforms/static/ncm_radio_ark.json similarity index 100% rename from tests/platforms/ncm_radio_ark.json rename to tests/platforms/static/ncm_radio_ark.json diff --git a/tests/platforms/ncm_siren.json b/tests/platforms/static/ncm_siren.json similarity index 100% rename from tests/platforms/ncm_siren.json rename to tests/platforms/static/ncm_siren.json diff --git a/tests/platforms/weibo_ak_list_0.json b/tests/platforms/static/weibo_ak_list_0.json similarity index 100% rename from tests/platforms/weibo_ak_list_0.json rename to tests/platforms/static/weibo_ak_list_0.json diff --git a/tests/platforms/weibo_ak_list_1.json b/tests/platforms/static/weibo_ak_list_1.json similarity index 100% rename from tests/platforms/weibo_ak_list_1.json rename to tests/platforms/static/weibo_ak_list_1.json diff --git a/tests/platforms/weibo_detail_4645748019299849 b/tests/platforms/static/weibo_detail_4645748019299849 similarity index 100% rename from tests/platforms/weibo_detail_4645748019299849 rename to tests/platforms/static/weibo_detail_4645748019299849 diff --git a/tests/platforms/weibo_detail_4649031014551911 b/tests/platforms/static/weibo_detail_4649031014551911 similarity index 100% rename from tests/platforms/weibo_detail_4649031014551911 rename to tests/platforms/static/weibo_detail_4649031014551911 diff --git a/tests/platforms/weibo_ys_list_0.json b/tests/platforms/static/weibo_ys_list_0.json similarity index 100% rename from tests/platforms/weibo_ys_list_0.json rename to tests/platforms/static/weibo_ys_list_0.json diff --git a/tests/platforms/test_arknights.py b/tests/platforms/test_arknights.py index d5a336f..adb7587 100644 --- a/tests/platforms/test_arknights.py +++ b/tests/platforms/test_arknights.py @@ -1,21 +1,16 @@ -import typing - import respx import pytest from httpx import Response - -if typing.TYPE_CHECKING: - import sys - - sys.path.append("./src/plugins") - import nonebot_bison +from nonebug.app import App from .utils import get_file, get_json @pytest.fixture -def arknights(plugin_module: "nonebot_bison"): - return plugin_module.platform.platform_manager["arknights"] +def arknights(app: App): + from nonebot_bison.platform import platform_manager + + return platform_manager["arknights"] @pytest.fixture(scope="module") diff --git a/tests/platforms/test_bilibili.py b/tests/platforms/test_bilibili.py index 4b99c83..5fc2b28 100644 --- a/tests/platforms/test_bilibili.py +++ b/tests/platforms/test_bilibili.py @@ -1,13 +1,6 @@ -import typing - import pytest from httpx import Response - -if typing.TYPE_CHECKING: - import sys - - sys.path.append("./src/plugins") - import nonebot_bison +from nonebug.app import App from .utils import get_json @@ -18,8 +11,10 @@ def bing_dy_list(): @pytest.fixture -def bilibili(plugin_module: "nonebot_bison"): - return plugin_module.platform.platform_manager["bilibili"] +def bilibili(app: App): + from nonebot_bison.platform import platform_manager + + return platform_manager["bilibili"] @pytest.mark.asyncio diff --git a/tests/platforms/test_ncm_artist.py b/tests/platforms/test_ncm_artist.py index 96e272f..a6e5f0d 100644 --- a/tests/platforms/test_ncm_artist.py +++ b/tests/platforms/test_ncm_artist.py @@ -1,22 +1,18 @@ import time -import typing import respx import pytest from httpx import Response +from nonebug.app import App from .utils import get_json -if typing.TYPE_CHECKING: - import sys - - sys.path.append("./src/plugins") - import nonebot_bison - @pytest.fixture -def ncm_artist(plugin_module: "nonebot_bison"): - return plugin_module.platform.platform_manager["ncm-artist"] +def ncm_artist(app: App): + from nonebot_bison.platform import platform_manager + + return platform_manager["ncm-artist"] @pytest.fixture(scope="module") diff --git a/tests/platforms/test_ncm_radio.py b/tests/platforms/test_ncm_radio.py index f6e3d86..cc88a85 100644 --- a/tests/platforms/test_ncm_radio.py +++ b/tests/platforms/test_ncm_radio.py @@ -1,22 +1,18 @@ import time -import typing import respx import pytest from httpx import Response +from nonebug.app import App from .utils import get_json -if typing.TYPE_CHECKING: - import sys - - sys.path.append("./src/plugins") - import nonebot_bison - @pytest.fixture -def ncm_radio(plugin_module: "nonebot_bison"): - return plugin_module.platform.platform_manager["ncm-radio"] +def ncm_radio(app: App): + from nonebot_bison.platform import platform_manager + + return platform_manager["ncm-radio"] @pytest.fixture(scope="module") diff --git a/tests/platforms/test_platform.py b/tests/platforms/test_platform.py index ae55b38..ff4930e 100644 --- a/tests/platforms/test_platform.py +++ b/tests/platforms/test_platform.py @@ -1,18 +1,8 @@ -import sys -import typing +from time import time from typing import Any, Optional import pytest - -if typing.TYPE_CHECKING: - import sys - - sys.path.append("./src/plugins") - import nonebot_bison - from nonebot_bison.types import * - from nonebot_bison.post import Post - -from time import time +from nonebug.app import App now = time() passed = now - 3 * 60 * 60 @@ -29,22 +19,30 @@ raw_post_list_2 = raw_post_list_1 + [ @pytest.fixture -def dummy_user(plugin_module: "nonebot_bison"): - user = plugin_module.types.User("123", "group") +def dummy_user(app: App): + from nonebot_bison.types import User + + user = User("123", "group") return user @pytest.fixture -def user_info_factory(plugin_module: "nonebot_bison", dummy_user): +def user_info_factory(app: App, dummy_user): + from nonebot_bison.types import UserSubInfo + def _user_info(category_getter, tag_getter): - return plugin_module.types.UserSubInfo(dummy_user, category_getter, tag_getter) + return UserSubInfo(dummy_user, category_getter, tag_getter) return _user_info @pytest.fixture -def mock_platform_without_cats_tags(plugin_module: "nonebot_bison"): - class MockPlatform(plugin_module.platform.platform.NewMessage): +def mock_platform_without_cats_tags(app: App): + from nonebot_bison.post import Post + from nonebot_bison.types import Target, RawPost + from nonebot_bison.platform.platform import NewMessage + + class MockPlatform(NewMessage): platform_name = "mock_platform" name = "Mock Platform" @@ -70,7 +68,7 @@ def mock_platform_without_cats_tags(plugin_module: "nonebot_bison"): return raw_post["date"] async def parse(self, raw_post: "RawPost") -> "Post": - return plugin_module.post.Post( + return Post( "mock_platform", raw_post["text"], "http://t.tt/" + str(self.get_id(raw_post)), @@ -88,8 +86,12 @@ def mock_platform_without_cats_tags(plugin_module: "nonebot_bison"): @pytest.fixture -def mock_platform(plugin_module: "nonebot_bison"): - class MockPlatform(plugin_module.platform.platform.NewMessage): +def mock_platform(app: App): + from nonebot_bison.post import Post + from nonebot_bison.platform.platform import NewMessage + from nonebot_bison.types import Tag, Target, RawPost, Category + + class MockPlatform(NewMessage): platform_name = "mock_platform" name = "Mock Platform" @@ -99,8 +101,8 @@ def mock_platform(plugin_module: "nonebot_bison"): enable_tag = True has_target = True categories = { - 1: "转发", - 2: "视频", + Category(1): "转发", + Category(2): "视频", } def __init__(self): @@ -124,7 +126,7 @@ def mock_platform(plugin_module: "nonebot_bison"): return raw_post["category"] async def parse(self, raw_post: "RawPost") -> "Post": - return plugin_module.post.Post( + return Post( "mock_platform", raw_post["text"], "http://t.tt/" + str(self.get_id(raw_post)), @@ -142,8 +144,12 @@ def mock_platform(plugin_module: "nonebot_bison"): @pytest.fixture -def mock_platform_no_target(plugin_module: "nonebot_bison"): - class MockPlatform(plugin_module.platform.platform.NewMessage): +def mock_platform_no_target(app: App): + from nonebot_bison.post import Post + from nonebot_bison.types import Tag, Target, RawPost, Category + from nonebot_bison.platform.platform import NewMessage, CategoryNotSupport + + class MockPlatform(NewMessage): platform_name = "mock_platform" name = "Mock Platform" @@ -153,7 +159,7 @@ def mock_platform_no_target(plugin_module: "nonebot_bison"): schedule_kw = {"seconds": 30} enable_tag = True has_target = False - categories = {1: "转发", 2: "视频", 3: "不支持"} + categories = {Category(1): "转发", Category(2): "视频", Category(3): "不支持"} def __init__(self): self.sub_index = 0 @@ -174,11 +180,11 @@ def mock_platform_no_target(plugin_module: "nonebot_bison"): def get_category(self, raw_post: "RawPost") -> "Category": if raw_post["category"] == 3: - raise plugin_module.platform.platform.CategoryNotSupport() + raise CategoryNotSupport() return raw_post["category"] async def parse(self, raw_post: "RawPost") -> "Post": - return plugin_module.post.Post( + return Post( "mock_platform", raw_post["text"], "http://t.tt/" + str(self.get_id(raw_post)), @@ -196,8 +202,12 @@ def mock_platform_no_target(plugin_module: "nonebot_bison"): @pytest.fixture -def mock_platform_no_target_2(plugin_module: "nonebot_bison"): - class MockPlatform(plugin_module.platform.platform.NewMessage): +def mock_platform_no_target_2(app: App): + from nonebot_bison.post import Post + from nonebot_bison.platform.platform import NewMessage + from nonebot_bison.types import Tag, Target, RawPost, Category + + class MockPlatform(NewMessage): platform_name = "mock_platform" name = "Mock Platform" @@ -208,8 +218,8 @@ def mock_platform_no_target_2(plugin_module: "nonebot_bison"): enable_tag = True has_target = False categories = { - 4: "leixing4", - 5: "leixing5", + Category(4): "leixing4", + Category(5): "leixing5", } def __init__(self): @@ -233,7 +243,7 @@ def mock_platform_no_target_2(plugin_module: "nonebot_bison"): return raw_post["category"] async def parse(self, raw_post: "RawPost") -> "Post": - return plugin_module.post.Post( + return Post( "mock_platform_2", raw_post["text"], "http://t.tt/" + str(self.get_id(raw_post)), @@ -259,8 +269,12 @@ def mock_platform_no_target_2(plugin_module: "nonebot_bison"): @pytest.fixture -def mock_status_change(plugin_module: "nonebot_bison"): - class MockPlatform(plugin_module.platform.platform.StatusChange): +def mock_status_change(app: App): + from nonebot_bison.post import Post + from nonebot_bison.platform.platform import StatusChange + from nonebot_bison.types import Tag, Target, RawPost, Category + + class MockPlatform(StatusChange): platform_name = "mock_platform" name = "Mock Platform" @@ -271,8 +285,8 @@ def mock_status_change(plugin_module: "nonebot_bison"): schedule_kw = {"seconds": 10} has_target = False categories = { - 1: "转发", - 2: "视频", + Category(1): "转发", + Category(2): "视频", } def __init__(self): @@ -297,7 +311,7 @@ def mock_status_change(plugin_module: "nonebot_bison"): return [] async def parse(self, raw_post) -> "Post": - return plugin_module.post.Post("mock_status", raw_post["text"], "") + return Post("mock_status", raw_post["text"], "") def get_category(self, raw_post): return raw_post["cat"] @@ -420,14 +434,17 @@ async def test_status_change(mock_status_change, user_info_factory): @pytest.mark.asyncio async def test_group( - plugin_module: "nonebot_bison", + app: App, mock_platform_no_target, mock_platform_no_target_2, user_info_factory, ): - group_platform = plugin_module.platform.platform.NoTargetGroup( - [mock_platform_no_target, mock_platform_no_target_2] - ) + + from nonebot_bison.post import Post + from nonebot_bison.platform.platform import NoTargetGroup + from nonebot_bison.types import Tag, Target, RawPost, Category + + group_platform = NoTargetGroup([mock_platform_no_target, mock_platform_no_target_2]) res1 = await group_platform.fetch_new_post( "dummy", [user_info_factory(lambda _: [1, 4], lambda _: [])] ) diff --git a/tests/platforms/test_weibo.py b/tests/platforms/test_weibo.py index 4139bed..1edb4d6 100644 --- a/tests/platforms/test_weibo.py +++ b/tests/platforms/test_weibo.py @@ -1,4 +1,3 @@ -import typing from datetime import datetime import respx @@ -6,19 +5,16 @@ import pytest import feedparser from pytz import timezone from httpx import Response - -if typing.TYPE_CHECKING: - import sys - - sys.path.append("./src/plugins") - import nonebot_bison +from nonebug.app import App from .utils import get_file, get_json @pytest.fixture -def weibo(plugin_module: "nonebot_bison"): - return plugin_module.platform.platform_manager["weibo"] +def weibo(app: App): + from nonebot_bison.platform import platform_manager + + return platform_manager["weibo"] @pytest.fixture(scope="module") diff --git a/tests/platforms/utils.py b/tests/platforms/utils.py index b80374c..0fd8c7b 100644 --- a/tests/platforms/utils.py +++ b/tests/platforms/utils.py @@ -1,7 +1,7 @@ import json from pathlib import Path -path = Path(__file__).parent +path = Path(__file__).parent / "static" def get_json(file_name: str): diff --git a/tests/test_config_manager.py b/tests/test_config_manager.py index bd7a638..1673309 100644 --- a/tests/test_config_manager.py +++ b/tests/test_config_manager.py @@ -1,23 +1,28 @@ import typing import pytest +from nonebug.app import App if typing.TYPE_CHECKING: import sys sys.path.append("./src/plugins") import nonebot_bison + from nonebot_bison.config import Config @pytest.fixture -def config(plugin_module): - plugin_module.config.start_up() - return plugin_module.config.Config() +def config(app: App): + from nonebot_bison import config + + config.start_up() + return config.Config() -def test_create_and_get( - config: "nonebot_bison.config.Config", plugin_module: "nonebot_bison" -): +def test_create_and_get(config: "Config", app: App): + from nonebot_bison import types + from nonebot_bison.types import Target + config.add_subscribe( user="123", user_type="group", @@ -29,8 +34,8 @@ def test_create_and_get( ) confs = config.list_subscribe("123", "group") assert len(confs) == 1 - assert config.target_user_cache["weibo"]["weibo_id"] == [ - plugin_module.types.User("123", "group") + assert config.target_user_cache["weibo"][Target("weibo_id")] == [ + types.User("123", "group") ] assert confs[0]["cats"] == [] config.update_subscribe( diff --git a/tests/test_merge_pic.py b/tests/test_merge_pic.py index 2008f1f..9e1a34b 100644 --- a/tests/test_merge_pic.py +++ b/tests/test_merge_pic.py @@ -1,6 +1,7 @@ import typing import pytest +from nonebug.app import App if typing.TYPE_CHECKING: import sys @@ -40,48 +41,56 @@ merge_source_9_2 = [ @pytest.mark.asyncio -async def test_9_merge(plugin_module: "nonebot_bison"): - post = plugin_module.post.Post("", "", "", pics=merge_source_9) +async def test_9_merge(app: App): + from nonebot_bison.post import Post + + post = Post("", "", "", pics=merge_source_9) await post._pic_merge() assert len(post.pics) == 5 await post.generate_messages() @pytest.mark.asyncio -async def test_9_merge_2(plugin_module: "nonebot_bison"): - post = plugin_module.post.Post("", "", "", pics=merge_source_9_2) +async def test_9_merge_2(app: App): + from nonebot_bison.post import Post + + post = Post("", "", "", pics=merge_source_9_2) await post._pic_merge() assert len(post.pics) == 4 await post.generate_messages() @pytest.mark.asyncio -async def test_6_merge(plugin_module): - post = plugin_module.post.Post( - "", "", "", pics=merge_source_9[0:6] + merge_source_9[9:] - ) +async def test_6_merge(app: App): + from nonebot_bison.post import Post + + post = Post("", "", "", pics=merge_source_9[0:6] + merge_source_9[9:]) await post._pic_merge() assert len(post.pics) == 5 @pytest.mark.asyncio -async def test_3_merge(plugin_module): - post = plugin_module.post.Post( - "", "", "", pics=merge_source_9[0:3] + merge_source_9[9:] - ) +async def test_3_merge(app: App): + from nonebot_bison.post import Post + + post = Post("", "", "", pics=merge_source_9[0:3] + merge_source_9[9:]) await post._pic_merge() assert len(post.pics) == 5 @pytest.mark.asyncio -async def test_6_merge_only(plugin_module): - post = plugin_module.post.Post("", "", "", pics=merge_source_9[0:6]) +async def test_6_merge_only(app: App): + from nonebot_bison.post import Post + + post = Post("", "", "", pics=merge_source_9[0:6]) await post._pic_merge() assert len(post.pics) == 1 @pytest.mark.asyncio -async def test_3_merge_only(plugin_module): - post = plugin_module.post.Post("", "", "", pics=merge_source_9[0:3]) +async def test_3_merge_only(app: App): + from nonebot_bison.post import Post + + post = Post("", "", "", pics=merge_source_9[0:3]) await post._pic_merge() assert len(post.pics) == 1 diff --git a/tests/test_new.py b/tests/test_new.py deleted file mode 100644 index 8f3bc87..0000000 --- a/tests/test_new.py +++ /dev/null @@ -1,16 +0,0 @@ -import pytest - - -def test_add(n_plugin_module): - from nonebot_bison.config import Config - - config = Config() - config.add_subscribe( - user="123", - user_type="group", - target="weibo_id", - target_name="weibo_name", - target_type="weibo", - cats=[], - tags=[], - ) diff --git a/tests/test_render.py b/tests/test_render.py index edeb478..646e3b7 100644 --- a/tests/test_render.py +++ b/tests/test_render.py @@ -1,6 +1,7 @@ import typing import pytest +from nonebug.app import App if typing.TYPE_CHECKING: import sys @@ -11,8 +12,10 @@ if typing.TYPE_CHECKING: @pytest.mark.asyncio @pytest.mark.render -async def test_render(plugin_module: "nonebot_bison"): - render = plugin_module.utils.Render() +async def test_render(app: App): + from nonebot_bison.utils import Render + + render = Render() res = await render.text_to_pic( """a\nbbbbbbbbbbbbbbbbbbbbbb\ncd

中文