From f31a798326515315a1ece3be84c1e9eccfb238a7 Mon Sep 17 00:00:00 2001 From: suyiiyii Date: Fri, 27 Sep 2024 00:45:45 +0800 Subject: [PATCH] =?UTF-8?q?:white=5Fcheck=5Fmark:=20=E4=BF=AE=E4=BF=AE?= =?UTF-8?q?=E5=8D=95=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_bison/config/subs_io/subs_io.py | 18 ++- tests/sub_manager/test_delete_cookie.py | 5 +- tests/subs_io/static/v3/subs_export.json | 69 ++++++++++++ tests/subs_io/static/v3/subs_export.yaml | 48 ++++++++ .../static/v3/subs_export_all_illegal.json | 103 ++++++++++++++++++ .../static/v3/subs_export_has_subdup_err.json | 97 +++++++++++++++++ tests/subs_io/test_cli.py | 18 ++- tests/subs_io/test_subs_io.py | 41 +++++-- 8 files changed, 381 insertions(+), 18 deletions(-) create mode 100644 tests/subs_io/static/v3/subs_export.json create mode 100644 tests/subs_io/static/v3/subs_export.yaml create mode 100644 tests/subs_io/static/v3/subs_export_all_illegal.json create mode 100644 tests/subs_io/static/v3/subs_export_has_subdup_err.json diff --git a/nonebot_bison/config/subs_io/subs_io.py b/nonebot_bison/config/subs_io/subs_io.py index 1d8a204..9b9ce48 100644 --- a/nonebot_bison/config/subs_io/subs_io.py +++ b/nonebot_bison/config/subs_io/subs_io.py @@ -10,6 +10,7 @@ from nonebot.compat import type_validate_python from nonebot_plugin_datastore.db import create_session from sqlalchemy.orm.strategy_options import selectinload +from .. import config from .utils import NBESFVerMatchErr, row2dict from .nbesf_model import NBESFBase, v1, v2, v3 from ..db_model import User, Cookie, Target, Subscribe, CookieTarget @@ -64,13 +65,22 @@ async def subscribes_export(selector: Callable[[Select], Select]) -> v3.SubGroup target_payload = type_validate_python(v3.Target, cookie_target.target) cookie_target_dict[cookie_target.cookie].append(target_payload) - cookies: list[v3.Cookie] = [] - for cookie, targets in cookie_target_dict.items(): - assert isinstance(cookie, Cookie) + def cookie_transform(cookie: Cookie, targets: [Target]) -> v3.Cookie: cookie_dict = row2dict(cookie) cookie_dict["tags"] = cookie.tags cookie_dict["targets"] = targets - cookies.append(v3.Cookie(**cookie_dict)) + return v3.Cookie(**cookie_dict) + + cookies: list[v3.Cookie] = [] + cookie_set = set() + for cookie, targets in cookie_target_dict.items(): + assert isinstance(cookie, Cookie) + cookies.append(cookie_transform(cookie, targets)) + cookie_set.add(cookie.id) + + # 添加未关联的cookie + all_cookies = await config.get_cookie(is_anonymous=False) + cookies.extend([cookie_transform(c, []) for c in all_cookies if c.id not in cookie_set]) sub_group = v3.SubGroup(groups=groups, cookies=cookies) diff --git a/tests/sub_manager/test_delete_cookie.py b/tests/sub_manager/test_delete_cookie.py index 684739a..b3541ea 100644 --- a/tests/sub_manager/test_delete_cookie.py +++ b/tests/sub_manager/test_delete_cookie.py @@ -51,8 +51,7 @@ async def test_del_cookie_err(app: App): should_send_saa( ctx, MessageFactory( - '已添加的 Cookie 为:\n1 weibo.com weibo.com [{"cookie":] ' - "1个关联\n请输入要删除的 Cookie 的序号\n输入'取消'中止" + "已添加的 Cookie 为:\n1 weibo.com unnamed cookie 1个关联\n请输入要删除的 Cookie 的序号\n输入'取消'中止" ), bot, event=event_1, @@ -118,7 +117,7 @@ async def test_del_cookie(app: App): should_send_saa( ctx, MessageFactory( - '已添加的 Cookie 为:\n1 weibo.com weibo.com [{"cookie":]' + "已添加的 Cookie 为:\n1 weibo.com unnamed cookie" " 0个关联\n请输入要删除的 Cookie 的序号\n输入'取消'中止" ), bot, diff --git a/tests/subs_io/static/v3/subs_export.json b/tests/subs_io/static/v3/subs_export.json new file mode 100644 index 0000000..b836f41 --- /dev/null +++ b/tests/subs_io/static/v3/subs_export.json @@ -0,0 +1,69 @@ +{ + "version": 3, + "groups": [ + { + "user_target": { + "platform_type": "QQ Group", + "group_id": 1232 + }, + "subs": [ + { + "categories": [], + "tags": [], + "target": { + "target_name": "weibo_name", + "target": "weibo_id", + "platform_name": "weibo", + "default_schedule_weight": 10 + } + } + ] + }, + { + "user_target": { + "platform_type": "QQ Group", + "group_id": 2342 + }, + "subs": [ + { + "categories": [], + "tags": ["kaltsit", "amiya"], + "target": { + "target_name": "weibo_name", + "target": "weibo_id", + "platform_name": "weibo", + "default_schedule_weight": 10 + } + }, + { + "categories": [1, 2], + "tags": [], + "target": { + "target_name": "bilibili_name", + "target": "bilibili_id", + "platform_name": "bilibili", + "default_schedule_weight": 10 + } + } + ] + } + ], + "cookies": [ + { + "site_name": "weibo.com", + "content": "{\"cookie\": \"test\"}", + "cookie_name": "test cookie", + "cd_milliseconds": 0, + "is_universal": false, + "tags": {}, + "targets": [ + { + "target_name": "weibo_name", + "target": "weibo_id", + "platform_name": "weibo", + "default_schedule_weight": 10 + } + ] + } + ] +} diff --git a/tests/subs_io/static/v3/subs_export.yaml b/tests/subs_io/static/v3/subs_export.yaml new file mode 100644 index 0000000..d02b0a4 --- /dev/null +++ b/tests/subs_io/static/v3/subs_export.yaml @@ -0,0 +1,48 @@ +version: 3 +groups: + - subs: + - categories: [] + tags: [] + target: + default_schedule_weight: 10 + platform_name: weibo + target: weibo_id + target_name: weibo_name + user_target: + platform_type: QQ Group + group_id: 123552 + - subs: + - categories: [] + tags: + - kaltsit + - amiya + target: + default_schedule_weight: 10 + platform_name: weibo + target: weibo_id + target_name: weibo_name + - categories: + - 1 + - 2 + tags: [] + target: + default_schedule_weight: 10 + platform_name: bilibili + target: bilibili_id + target_name: bilibili_name + user_target: + platform_type: QQ Group + group_id: 234662 + +cookies: + - site_name: weibo.com + content: '{"cookie": "test"}' + cookie_name: test cookie + cd_milliseconds: 0 + is_universal: false + tags: {} + targets: + - target_name: weibo_name + target: weibo_id + platform_name: weibo + default_schedule_weight: 10 diff --git a/tests/subs_io/static/v3/subs_export_all_illegal.json b/tests/subs_io/static/v3/subs_export_all_illegal.json new file mode 100644 index 0000000..d1b8dfd --- /dev/null +++ b/tests/subs_io/static/v3/subs_export_all_illegal.json @@ -0,0 +1,103 @@ +{ + "version": 2, + "groups": [ + { + "user_target": { + "platform_type": "QQ Group", + "group_id": 123 + }, + "subs": [ + { + "categories": [], + "tags": [], + "target": { + "target_name": "weibo_name", + "target": "weibo_id", + "platform_name": "weibo", + "default_schedule_weight": 10 + } + } + ] + }, + { + "user_target": { + "platform_type": "QQ Group", + "group_id": 234 + }, + "subs": [ + { + "tags": ["kaltsit", "amiya"], + "target": { + "target_name": "weibo_name", + "target": "weibo_id", + "platform_name": "weibo", + "default_schedule_weight": 10 + } + }, + { + "categories": [1, 2], + "tags": [], + "target": [ + { + "target_name": "bilibili_name", + "target": "bilibili_id", + "platform_name": "bilibili", + "default_schedule_weight": 10 + } + ] + } + ] + }, + { + "user_target": { + "platform_type": "QQ Group", + "group_id": 123 + }, + "subs": { + "categories": [], + "tags": [], + "target": { + "target_name": "weibo_name2", + "target": "weibo_id2", + "platform_name": "weibo", + "default_schedule_weight": 10 + } + } + }, + { + "user_target": { + "platform_type": "QQ Group", + "group_id": 123 + }, + "subs": [ + { + "categories": [], + "tags": [], + "target": { + "target_name": "weibo_name2", + "platform_name": "weibo", + "default_schedule_weight": 10 + } + } + ] + } + ], + "cookies": [ + { + "site_name": "weibo.com1111", + "content": "{\"cookie\": 111}", + "cookie_name": "test cookie1", + "cd_milliseconds": -1, + "is_universal": false, + "tags": {}, + "targets": [ + { + "target_name": "weibo_name", + "target": "weibo_id", + "platform_name": "weibo", + "default_schedule_weight": 10 + } + ] + } + ] +} diff --git a/tests/subs_io/static/v3/subs_export_has_subdup_err.json b/tests/subs_io/static/v3/subs_export_has_subdup_err.json new file mode 100644 index 0000000..cb6740c --- /dev/null +++ b/tests/subs_io/static/v3/subs_export_has_subdup_err.json @@ -0,0 +1,97 @@ +{ + "version": 3, + "groups": [ + { + "user_target": { + "platform_type": "QQ Group", + "group_id": 1232 + }, + "subs": [ + { + "categories": [], + "tags": [], + "target": { + "target_name": "weibo_name", + "target": "weibo_id", + "platform_name": "weibo", + "default_schedule_weight": 10 + } + } + ] + }, + { + "user_target": { + "platform_type": "QQ Group", + "group_id": 2342 + }, + "subs": [ + { + "categories": [], + "tags": ["kaltsit", "amiya"], + "target": { + "target_name": "weibo_name", + "target": "weibo_id", + "platform_name": "weibo", + "default_schedule_weight": 10 + } + }, + { + "categories": [1, 2], + "tags": [], + "target": { + "target_name": "bilibili_name", + "target": "bilibili_id", + "platform_name": "bilibili", + "default_schedule_weight": 10 + } + } + ] + }, + { + "user_target": { + "platform_type": "QQ Group", + "group_id": 1232 + }, + "subs": [ + { + "categories": [], + "tags": [], + "target": { + "target_name": "weibo_name", + "target": "weibo_id", + "platform_name": "weibo", + "default_schedule_weight": 10 + } + }, + { + "categories": [2, 6], + "tags": ["poca"], + "target": { + "target_name": "weibo_name2", + "target": "weibo_id2", + "platform_name": "weibo", + "default_schedule_weight": 10 + } + } + ] + } + ], + "cookies": [ + { + "site_name": "weibo.com", + "content": "{\"cookie\": \"test\"}", + "cookie_name": "test cookie", + "cd_milliseconds": 0, + "is_universal": false, + "tags": {}, + "targets": [ + { + "target_name": "weibo_name", + "target": "weibo_id", + "platform_name": "weibo", + "default_schedule_weight": 10 + } + ] + } + ] +} diff --git a/tests/subs_io/test_cli.py b/tests/subs_io/test_cli.py index 0ffad3a..d74bac5 100644 --- a/tests/subs_io/test_cli.py +++ b/tests/subs_io/test_cli.py @@ -40,6 +40,7 @@ def test_cli_help(app: App): async def test_subs_export(app: App, tmp_path: Path): from nonebot_plugin_saa import TargetQQGroup + from nonebot_bison.config.db_model import Cookie from nonebot_bison.config.db_config import config from nonebot_bison.types import Target as TTarget from nonebot_bison.script.cli import cli, run_sync @@ -70,6 +71,14 @@ async def test_subs_export(app: App, tmp_path: Path): cats=[1, 2], tags=[], ) + cookie_id = await config.add_cookie( + Cookie( + site_name="weibo.com", + content='{"cookie": "test"}', + cookie_name="test cookie", + ) + ) + await config.add_cookie_target("weibo_id", "weibo", cookie_id) assert len(await config.list_subs_with_all_info()) == 3 @@ -84,8 +93,9 @@ async def test_subs_export(app: App, tmp_path: Path): assert result.exit_code == 0 file_path = Path.cwd() / "bison_subscribes_export_1.json" assert file_path.exists() - assert '"version": 2' in file_path.read_text() + assert '"version": 3' in file_path.read_text() assert '"group_id": 123' in file_path.read_text() + assert '"content": "{\\"cookie\\": \\"test\\"}",\n' in file_path.read_text() # 是否导出到指定已存在文件夹 data_dir = tmp_path / "data" @@ -94,8 +104,9 @@ async def test_subs_export(app: App, tmp_path: Path): assert result.exit_code == 0 file_path2 = data_dir / "bison_subscribes_export_1.json" assert file_path2.exists() - assert '"version": 2' in file_path2.read_text() + assert '"version": 3' in file_path2.read_text() assert '"group_id": 123' in file_path2.read_text() + assert '"content": "{\\"cookie\\": \\"test\\"}",\n' in file_path.read_text() # 是否拒绝导出到不存在的文件夹 result = await run_sync(runner.invoke)(cli, ["export", "-p", str(tmp_path / "data2")]) @@ -106,9 +117,10 @@ async def test_subs_export(app: App, tmp_path: Path): assert result.exit_code == 0 file_path3 = tmp_path / "bison_subscribes_export_1.yaml" assert file_path3.exists() - assert "version: 2" in file_path3.read_text() + assert "version: 3" in file_path3.read_text() assert "group_id: 123" in file_path3.read_text() assert "platform_type: QQ Group" in file_path3.read_text() + assert '"content": "{\\"cookie\\": \\"test\\"}",\n' in file_path.read_text() # 是否允许以未支持的格式导出 result = await run_sync(runner.invoke)(cli, ["export", "-p", str(tmp_path), "--format", "toml"]) diff --git a/tests/subs_io/test_subs_io.py b/tests/subs_io/test_subs_io.py index bb89d9a..364f1fb 100644 --- a/tests/subs_io/test_subs_io.py +++ b/tests/subs_io/test_subs_io.py @@ -8,9 +8,9 @@ from .utils import get_json async def test_subs_export(app: App, init_scheduler): from nonebot_plugin_saa import TargetQQGroup - from nonebot_bison.config.db_model import User from nonebot_bison.config.db_config import config from nonebot_bison.types import Target as TTarget + from nonebot_bison.config.db_model import User, Cookie from nonebot_bison.config.subs_io import subscribes_export await config.add_subscribe( @@ -37,12 +37,20 @@ async def test_subs_export(app: App, init_scheduler): cats=[1, 2], tags=[], ) + cookie_id = await config.add_cookie( + Cookie( + site_name="weibo.com", + content='{"cookie": "test"}', + cookie_name="test cookie", + ) + ) + await config.add_cookie_target("weibo_id", "weibo", cookie_id) data = await config.list_subs_with_all_info() assert len(data) == 3 nbesf_data = await subscribes_export(lambda x: x) - assert model_dump(nbesf_data) == get_json("v2/subs_export.json") + assert model_dump(nbesf_data) == get_json("v3/subs_export.json") nbesf_data_user_234 = await subscribes_export( lambda stmt: stmt.where(User.user_target == {"platform_type": "QQ Group", "group_id": 2342}) @@ -102,16 +110,30 @@ async def test_subs_import_dup_err(app: App, init_scheduler): async def test_subs_import_version_disorder(app: App, init_scheduler): from nonebot_bison.config.subs_io import subscribes_import - from nonebot_bison.config.subs_io.nbesf_model import v1, v2 from nonebot_bison.config.subs_io.utils import NBESFParseErr - - # use v2 parse v1 - with pytest.raises(NBESFParseErr): - v1.nbesf_parser(get_json("v2/subs_export_has_subdup_err.json")) + from nonebot_bison.config.subs_io.nbesf_model import v1, v2, v3 # use v1 parse v2 + with pytest.raises(NBESFParseErr): + v1.nbesf_parser(get_json("v2/subs_export_has_subdup_err.json")) + # use v1 parse v3 + with pytest.raises(NBESFParseErr): + v1.nbesf_parser(get_json("v3/subs_export_has_subdup_err.json")) + + # use v2 parse v1 with pytest.raises(NBESFParseErr): v2.nbesf_parser(get_json("v1/subs_export_has_subdup_err.json")) + # # use v2 parse v3 + # with pytest.raises(NBESFParseErr): + # v2.nbesf_parser(get_json("v3/subs_export_has_subdup_err.json")) + + # use v3 parse v1 + with pytest.raises(NBESFParseErr): + v3.nbesf_parser(get_json("v1/subs_export_has_subdup_err.json")) + # # use v3 parse v2 + # with pytest.raises(NBESFParseErr): + # v3.nbesf_parser(get_json("v2/subs_export_has_subdup_err.json")) + # TODO: v3 parse v2 不会报错,但是v3 parse v1 会报错,似乎是有问题 ( with pytest.raises(AssertionError): # noqa: PT012 nbesf_data = v2.nbesf_parser(get_json("v2/subs_export_has_subdup_err.json")) @@ -121,7 +143,7 @@ async def test_subs_import_version_disorder(app: App, init_scheduler): async def test_subs_import_all_fail(app: App, init_scheduler): """只要文件格式有任何一个错误, 都不会进行订阅""" - from nonebot_bison.config.subs_io.nbesf_model import v1, v2 + from nonebot_bison.config.subs_io.nbesf_model import v1, v2, v3 from nonebot_bison.config.subs_io.nbesf_model.v1 import NBESFParseErr with pytest.raises(NBESFParseErr): @@ -129,3 +151,6 @@ async def test_subs_import_all_fail(app: App, init_scheduler): with pytest.raises(NBESFParseErr): v2.nbesf_parser(get_json("v2/subs_export_all_illegal.json")) + + with pytest.raises(NBESFParseErr): + v3.nbesf_parser(get_json("v3/subs_export_all_illegal.json"))