mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2026-05-09 18:27:56 +08:00
🎨 按ruff调整测试代码
This commit is contained in:
+17
-18
@@ -1,8 +1,10 @@
|
||||
from typing import cast
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from click.testing import CliRunner
|
||||
from nonebug.app import App
|
||||
from click.core import BaseCommand
|
||||
from click.testing import CliRunner
|
||||
|
||||
from .utils import get_file
|
||||
|
||||
@@ -10,6 +12,7 @@ from .utils import get_file
|
||||
def test_cli_help(app: App):
|
||||
from nonebot_bison.script.cli import cli
|
||||
|
||||
cli = cast(BaseCommand, cli)
|
||||
runner = CliRunner()
|
||||
|
||||
result = runner.invoke(cli, ["--help"])
|
||||
@@ -38,8 +41,10 @@ async def test_subs_export(app: App, tmp_path: Path):
|
||||
from nonebot_plugin_saa import TargetQQGroup
|
||||
|
||||
from nonebot_bison.config.db_config import config
|
||||
from nonebot_bison.script.cli import cli, run_sync
|
||||
from nonebot_bison.types import Target as TTarget
|
||||
from nonebot_bison.script.cli import cli, run_sync
|
||||
|
||||
cli = cast(BaseCommand, cli)
|
||||
|
||||
await config.add_subscribe(
|
||||
TargetQQGroup(group_id=123),
|
||||
@@ -74,7 +79,7 @@ async def test_subs_export(app: App, tmp_path: Path):
|
||||
|
||||
runner = CliRunner()
|
||||
# 是否默认导出到工作目录
|
||||
with runner.isolated_filesystem(temp_dir=tmp_path) as td:
|
||||
with runner.isolated_filesystem(temp_dir=tmp_path):
|
||||
result = await run_sync(runner.invoke)(cli, ["export"])
|
||||
assert result.exit_code == 0
|
||||
file_path = Path.cwd() / "bison_subscribes_export_1.json"
|
||||
@@ -93,15 +98,11 @@ async def test_subs_export(app: App, tmp_path: Path):
|
||||
assert '"group_id": 123' in file_path2.read_text()
|
||||
|
||||
# 是否拒绝导出到不存在的文件夹
|
||||
result = await run_sync(runner.invoke)(
|
||||
cli, ["export", "-p", str(tmp_path / "data2")]
|
||||
)
|
||||
result = await run_sync(runner.invoke)(cli, ["export", "-p", str(tmp_path / "data2")])
|
||||
assert result.exit_code == 1
|
||||
|
||||
# 是否可以以yaml格式导出
|
||||
result = await run_sync(runner.invoke)(
|
||||
cli, ["export", "-p", str(tmp_path), "--format", "yaml"]
|
||||
)
|
||||
result = await run_sync(runner.invoke)(cli, ["export", "-p", str(tmp_path), "--format", "yaml"])
|
||||
assert result.exit_code == 0
|
||||
file_path3 = tmp_path / "bison_subscribes_export_1.yaml"
|
||||
assert file_path3.exists()
|
||||
@@ -110,9 +111,7 @@ async def test_subs_export(app: App, tmp_path: Path):
|
||||
assert "platform_type: QQ Group" in file_path3.read_text()
|
||||
|
||||
# 是否允许以未支持的格式导出
|
||||
result = await run_sync(runner.invoke)(
|
||||
cli, ["export", "-p", str(tmp_path), "--format", "toml"]
|
||||
)
|
||||
result = await run_sync(runner.invoke)(cli, ["export", "-p", str(tmp_path), "--format", "toml"])
|
||||
assert result.exit_code == 2
|
||||
|
||||
|
||||
@@ -120,6 +119,8 @@ async def test_subs_import_v1(app: App, tmp_path):
|
||||
from nonebot_bison.config.db_config import config
|
||||
from nonebot_bison.script.cli import cli, run_sync
|
||||
|
||||
cli = cast(BaseCommand, cli)
|
||||
|
||||
assert len(await config.list_subs_with_all_info()) == 0
|
||||
|
||||
mock_file: Path = tmp_path / "1.json"
|
||||
@@ -140,9 +141,7 @@ async def test_subs_import_v1(app: App, tmp_path):
|
||||
mock_file: Path = tmp_path / "2.yaml"
|
||||
mock_file.write_text(get_file("v1/subs_export.yaml"))
|
||||
|
||||
result = await run_sync(runner.invoke)(
|
||||
cli, ["import", "-p", str(mock_file), "--format=yml"]
|
||||
)
|
||||
result = await run_sync(runner.invoke)(cli, ["import", "-p", str(mock_file), "--format=yml"])
|
||||
assert result.exit_code == 0
|
||||
assert len(await config.list_subs_with_all_info()) == 6
|
||||
|
||||
@@ -151,6 +150,8 @@ async def test_sub_import_v2(app: App, tmp_path):
|
||||
from nonebot_bison.config.db_config import config
|
||||
from nonebot_bison.script.cli import cli, run_sync
|
||||
|
||||
cli = cast(BaseCommand, cli)
|
||||
|
||||
assert len(await config.list_subs_with_all_info()) == 0
|
||||
|
||||
mock_file: Path = tmp_path / "1.json"
|
||||
@@ -171,8 +172,6 @@ async def test_sub_import_v2(app: App, tmp_path):
|
||||
mock_file: Path = tmp_path / "2.yaml"
|
||||
mock_file.write_text(get_file("v2/subs_export.yaml"))
|
||||
|
||||
result = await run_sync(runner.invoke)(
|
||||
cli, ["import", "-p", str(mock_file), "--format=yml"]
|
||||
)
|
||||
result = await run_sync(runner.invoke)(cli, ["import", "-p", str(mock_file), "--format=yml"])
|
||||
assert result.exit_code == 0
|
||||
assert len(await config.list_subs_with_all_info()) == 6
|
||||
|
||||
@@ -5,14 +5,12 @@ from .utils import get_json
|
||||
|
||||
|
||||
async def test_subs_export(app: App, init_scheduler):
|
||||
import time
|
||||
|
||||
from nonebot_plugin_saa import TargetQQGroup
|
||||
|
||||
from nonebot_bison.config.db_config import config
|
||||
from nonebot_bison.config.db_model import User
|
||||
from nonebot_bison.config.subs_io import subscribes_export
|
||||
from nonebot_bison.config.db_config import config
|
||||
from nonebot_bison.types import Target as TTarget
|
||||
from nonebot_bison.config.subs_io import subscribes_export
|
||||
|
||||
await config.add_subscribe(
|
||||
TargetQQGroup(group_id=1232),
|
||||
@@ -43,13 +41,10 @@ async def test_subs_export(app: App, init_scheduler):
|
||||
assert len(data) == 3
|
||||
|
||||
nbesf_data = await subscribes_export(lambda x: x)
|
||||
print(nbesf_data.dict())
|
||||
assert nbesf_data.dict() == get_json("v2/subs_export.json")
|
||||
|
||||
nbesf_data_user_234 = await subscribes_export(
|
||||
lambda stmt: stmt.where(
|
||||
User.user_target == {"platform_type": "QQ Group", "group_id": 2342}
|
||||
)
|
||||
lambda stmt: stmt.where(User.user_target == {"platform_type": "QQ Group", "group_id": 2342})
|
||||
)
|
||||
assert len(nbesf_data_user_234.groups) == 1
|
||||
assert len(nbesf_data_user_234.groups[0].subs) == 2
|
||||
@@ -81,7 +76,6 @@ async def test_subs_import(app: App, init_scheduler):
|
||||
|
||||
|
||||
async def test_subs_import_dup_err(app: App, init_scheduler):
|
||||
|
||||
from nonebot_bison.config.db_config import config
|
||||
from nonebot_bison.config.subs_io import subscribes_import
|
||||
from nonebot_bison.config.subs_io.nbesf_model import v1, v2
|
||||
@@ -106,20 +100,19 @@ 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.db_config import config
|
||||
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):
|
||||
nbesf_data_v1 = v1.nbesf_parser(get_json("v2/subs_export_has_subdup_err.json"))
|
||||
v1.nbesf_parser(get_json("v2/subs_export_has_subdup_err.json"))
|
||||
|
||||
# use v1 parse v2
|
||||
with pytest.raises(NBESFParseErr):
|
||||
nbesf_data_v2 = v2.nbesf_parser(get_json("v1/subs_export_has_subdup_err.json"))
|
||||
v2.nbesf_parser(get_json("v1/subs_export_has_subdup_err.json"))
|
||||
|
||||
with pytest.raises(AssertionError):
|
||||
with pytest.raises(AssertionError): # noqa: PT012
|
||||
nbesf_data = v2.nbesf_parser(get_json("v2/subs_export_has_subdup_err.json"))
|
||||
nbesf_data.version = 1
|
||||
await subscribes_import(nbesf_data)
|
||||
@@ -131,7 +124,7 @@ async def test_subs_import_all_fail(app: App, init_scheduler):
|
||||
from nonebot_bison.config.subs_io.nbesf_model.v1 import NBESFParseErr
|
||||
|
||||
with pytest.raises(NBESFParseErr):
|
||||
nbesf_data = v1.nbesf_parser(get_json("v1/subs_export_all_illegal.json"))
|
||||
v1.nbesf_parser(get_json("v1/subs_export_all_illegal.json"))
|
||||
|
||||
with pytest.raises(NBESFParseErr):
|
||||
nbesf_data = v2.nbesf_parser(get_json("v2/subs_export_all_illegal.json"))
|
||||
v2.nbesf_parser(get_json("v2/subs_export_all_illegal.json"))
|
||||
|
||||
@@ -6,13 +6,13 @@ path = Path(__file__).parent / "static"
|
||||
|
||||
def get_json(file_name: str):
|
||||
"""返回该目录下static目录下的<file_name>解析得到的json"""
|
||||
with open(path / file_name, "r", encoding="utf8") as f:
|
||||
with open(path / file_name, encoding="utf8") as f:
|
||||
json_ = json.load(f)
|
||||
return json_
|
||||
|
||||
|
||||
def get_file(file_name: str):
|
||||
"""返回该目录下static目录下的<file_name>的文件内容"""
|
||||
with open(path / file_name, "r", encoding="utf8") as f:
|
||||
with open(path / file_name, encoding="utf8") as f:
|
||||
file_text = f.read()
|
||||
return file_text
|
||||
|
||||
Reference in New Issue
Block a user