mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2026-05-09 18:27:56 +08:00
update scheduler api
This commit is contained in:
@@ -92,8 +92,9 @@ def mock_platform(app: App):
|
||||
from nonebot_bison.types import Category, RawPost, Tag, Target
|
||||
from nonebot_bison.utils import SchedulerConfig
|
||||
|
||||
class MockPlatformSchedConf(SchedulerConfig, name="mock"):
|
||||
class MockPlatformSchedConf(SchedulerConfig):
|
||||
|
||||
name = "mock"
|
||||
schedule_type = "interval"
|
||||
schedule_setting = {"seconds": 100}
|
||||
|
||||
@@ -105,7 +106,7 @@ def mock_platform(app: App):
|
||||
is_common = True
|
||||
enable_tag = True
|
||||
has_target = True
|
||||
scheduler_class = "mock"
|
||||
scheduler = MockPlatformSchedConf
|
||||
categories = {
|
||||
Category(1): "转发",
|
||||
Category(2): "视频",
|
||||
@@ -150,16 +151,23 @@ def mock_platform(app: App):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_platform_no_target(app: App):
|
||||
def mock_scheduler_conf(app):
|
||||
from nonebot_bison.utils import SchedulerConfig
|
||||
|
||||
class MockPlatformSchedConf(SchedulerConfig):
|
||||
|
||||
name = "mock"
|
||||
schedule_type = "interval"
|
||||
schedule_setting = {"seconds": 100}
|
||||
|
||||
return MockPlatformSchedConf
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_platform_no_target(app: App, mock_scheduler_conf):
|
||||
from nonebot_bison.platform.platform import CategoryNotSupport, NewMessage
|
||||
from nonebot_bison.post import Post
|
||||
from nonebot_bison.types import Category, RawPost, Tag, Target
|
||||
from nonebot_bison.utils import SchedulerConfig
|
||||
|
||||
class MockPlatformSchedConf(SchedulerConfig, name="mock"):
|
||||
|
||||
schedule_type = "interval"
|
||||
schedule_setting = {"seconds": 100}
|
||||
|
||||
class MockPlatform(NewMessage):
|
||||
|
||||
@@ -167,7 +175,7 @@ def mock_platform_no_target(app: App):
|
||||
name = "Mock Platform"
|
||||
enabled = True
|
||||
is_common = True
|
||||
scheduler_class = "mock"
|
||||
scheduler = mock_scheduler_conf
|
||||
enable_tag = True
|
||||
has_target = False
|
||||
categories = {Category(1): "转发", Category(2): "视频", Category(3): "不支持"}
|
||||
@@ -213,23 +221,18 @@ def mock_platform_no_target(app: App):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_platform_no_target_2(app: App):
|
||||
def mock_platform_no_target_2(app: App, mock_scheduler_conf):
|
||||
from nonebot_bison.platform.platform import NewMessage
|
||||
from nonebot_bison.post import Post
|
||||
from nonebot_bison.types import Category, RawPost, Tag, Target
|
||||
from nonebot_bison.utils import SchedulerConfig
|
||||
|
||||
class MockPlatformSchedConf(SchedulerConfig, name="mock"):
|
||||
|
||||
schedule_type = "interval"
|
||||
schedule_setting = {"seconds": 100}
|
||||
|
||||
class MockPlatform(NewMessage):
|
||||
|
||||
platform_name = "mock_platform"
|
||||
name = "Mock Platform"
|
||||
enabled = True
|
||||
scheduler_class = "mock"
|
||||
scheduler = mock_scheduler_conf
|
||||
is_common = True
|
||||
enable_tag = True
|
||||
has_target = False
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
import typing
|
||||
from datetime import time
|
||||
from typing import Type
|
||||
|
||||
from nonebug import App
|
||||
|
||||
if typing.TYPE_CHECKING:
|
||||
from nonebot_bison.utils.scheduler_config import SchedulerConfig
|
||||
|
||||
async def get_schedule_times(scheduler_class: str, time: int) -> dict[str, int]:
|
||||
|
||||
async def get_schedule_times(
|
||||
scheduler_config: Type["SchedulerConfig"], time: int
|
||||
) -> dict[str, int]:
|
||||
from nonebot_bison.scheduler import scheduler_dict
|
||||
|
||||
scheduler = scheduler_dict[scheduler_class]
|
||||
scheduler = scheduler_dict[scheduler_config]
|
||||
res = {}
|
||||
for _ in range(time):
|
||||
schedulable = await scheduler.get_next_schedulable()
|
||||
@@ -19,6 +26,7 @@ async def get_schedule_times(scheduler_class: str, time: int) -> dict[str, int]:
|
||||
async def test_scheduler_without_time(init_scheduler):
|
||||
from nonebot_bison.config import config
|
||||
from nonebot_bison.config.db_config import WeightConfig
|
||||
from nonebot_bison.platform.bilibili import BilibiliSchedConf
|
||||
from nonebot_bison.scheduler.manager import init_scheduler
|
||||
from nonebot_bison.types import Target as T_Target
|
||||
|
||||
@@ -41,12 +49,12 @@ async def test_scheduler_without_time(init_scheduler):
|
||||
|
||||
await init_scheduler()
|
||||
|
||||
static_res = await get_schedule_times("bilibili.com", 6)
|
||||
static_res = await get_schedule_times(BilibiliSchedConf, 6)
|
||||
assert static_res["bilibili-t1"] == 1
|
||||
assert static_res["bilibili-t2"] == 2
|
||||
assert static_res["bilibili-live-t2"] == 3
|
||||
|
||||
static_res = await get_schedule_times("bilibili.com", 6)
|
||||
static_res = await get_schedule_times(BilibiliSchedConf, 6)
|
||||
assert static_res["bilibili-t1"] == 1
|
||||
assert static_res["bilibili-t2"] == 2
|
||||
assert static_res["bilibili-live-t2"] == 3
|
||||
@@ -55,6 +63,7 @@ async def test_scheduler_without_time(init_scheduler):
|
||||
async def test_scheduler_with_time(app: App, init_scheduler):
|
||||
from nonebot_bison.config import config, db_config
|
||||
from nonebot_bison.config.db_config import TimeWeightConfig, WeightConfig
|
||||
from nonebot_bison.platform.bilibili import BilibiliSchedConf
|
||||
from nonebot_bison.scheduler.manager import init_scheduler
|
||||
from nonebot_bison.types import Target as T_Target
|
||||
|
||||
@@ -85,24 +94,25 @@ async def test_scheduler_with_time(app: App, init_scheduler):
|
||||
await init_scheduler()
|
||||
|
||||
app.monkeypatch.setattr(db_config, "_get_time", lambda: time(1, 30))
|
||||
static_res = await get_schedule_times("bilibili.com", 6)
|
||||
static_res = await get_schedule_times(BilibiliSchedConf, 6)
|
||||
assert static_res["bilibili-t1"] == 1
|
||||
assert static_res["bilibili-t2"] == 2
|
||||
assert static_res["bilibili-live-t2"] == 3
|
||||
|
||||
static_res = await get_schedule_times("bilibili.com", 6)
|
||||
static_res = await get_schedule_times(BilibiliSchedConf, 6)
|
||||
assert static_res["bilibili-t1"] == 1
|
||||
assert static_res["bilibili-t2"] == 2
|
||||
assert static_res["bilibili-live-t2"] == 3
|
||||
|
||||
app.monkeypatch.setattr(db_config, "_get_time", lambda: time(10, 30))
|
||||
|
||||
static_res = await get_schedule_times("bilibili.com", 6)
|
||||
static_res = await get_schedule_times(BilibiliSchedConf, 6)
|
||||
assert static_res["bilibili-t2"] == 6
|
||||
|
||||
|
||||
async def test_scheduler_add_new(init_scheduler):
|
||||
from nonebot_bison.config import config
|
||||
from nonebot_bison.platform.bilibili import BilibiliSchedConf
|
||||
from nonebot_bison.scheduler.manager import init_scheduler
|
||||
from nonebot_bison.types import Target as T_Target
|
||||
|
||||
@@ -118,12 +128,13 @@ async def test_scheduler_add_new(init_scheduler):
|
||||
await config.add_subscribe(
|
||||
123, "group", T_Target("t2"), "target2", "bilibili", [], []
|
||||
)
|
||||
stat_res = await get_schedule_times("bilibili.com", 1)
|
||||
stat_res = await get_schedule_times(BilibiliSchedConf, 1)
|
||||
assert stat_res["bilibili-t2"] == 1
|
||||
|
||||
|
||||
async def test_schedule_delete(init_scheduler):
|
||||
from nonebot_bison.config import config
|
||||
from nonebot_bison.platform.bilibili import BilibiliSchedConf
|
||||
from nonebot_bison.scheduler.manager import init_scheduler
|
||||
from nonebot_bison.types import Target as T_Target
|
||||
|
||||
@@ -136,10 +147,10 @@ async def test_schedule_delete(init_scheduler):
|
||||
|
||||
await init_scheduler()
|
||||
|
||||
stat_res = await get_schedule_times("bilibili.com", 2)
|
||||
stat_res = await get_schedule_times(BilibiliSchedConf, 2)
|
||||
assert stat_res["bilibili-t2"] == 1
|
||||
assert stat_res["bilibili-t1"] == 1
|
||||
|
||||
await config.del_subscribe(123, "group", T_Target("t1"), "bilibili")
|
||||
stat_res = await get_schedule_times("bilibili.com", 2)
|
||||
stat_res = await get_schedule_times(BilibiliSchedConf, 2)
|
||||
assert stat_res["bilibili-t2"] == 2
|
||||
|
||||
Reference in New Issue
Block a user