2022-05-26 16:35:47 +08:00
|
|
|
import pytest
|
2024-02-05 14:00:49 +08:00
|
|
|
from pydantic import BaseModel
|
2022-05-26 16:35:47 +08:00
|
|
|
|
2023-02-22 23:32:48 +08:00
|
|
|
import nonebot
|
|
|
|
from nonebot.plugin import PluginManager, _managers
|
2022-05-26 16:35:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2023-02-22 23:32:48 +08:00
|
|
|
async def test_get_plugin():
|
2022-05-26 16:35:47 +08:00
|
|
|
# check simple plugin
|
|
|
|
plugin = nonebot.get_plugin("export")
|
|
|
|
assert plugin
|
2024-04-20 14:47:12 +08:00
|
|
|
assert plugin.id_ == "export"
|
|
|
|
assert plugin.name == "export"
|
2022-05-26 16:35:47 +08:00
|
|
|
assert plugin.module_name == "plugins.export"
|
|
|
|
|
|
|
|
# check sub plugin
|
2024-04-20 14:47:12 +08:00
|
|
|
plugin = nonebot.get_plugin("nested:nested_subplugin")
|
2022-05-26 16:35:47 +08:00
|
|
|
assert plugin
|
2024-04-20 14:47:12 +08:00
|
|
|
assert plugin.id_ == "nested:nested_subplugin"
|
|
|
|
assert plugin.name == "nested_subplugin"
|
2022-05-26 16:35:47 +08:00
|
|
|
assert plugin.module_name == "plugins.nested.plugins.nested_subplugin"
|
|
|
|
|
2024-04-20 14:47:12 +08:00
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_get_plugin_by_module_name():
|
|
|
|
# check get plugin by exact module name
|
|
|
|
plugin = nonebot.get_plugin_by_module_name("plugins.nested")
|
|
|
|
assert plugin
|
|
|
|
assert plugin.id_ == "nested"
|
|
|
|
assert plugin.name == "nested"
|
|
|
|
assert plugin.module_name == "plugins.nested"
|
|
|
|
|
|
|
|
# check get plugin by sub module name
|
2022-05-26 16:35:47 +08:00
|
|
|
plugin = nonebot.get_plugin_by_module_name("plugins.nested.utils")
|
|
|
|
assert plugin
|
2024-04-20 14:47:12 +08:00
|
|
|
assert plugin.id_ == "nested"
|
|
|
|
assert plugin.name == "nested"
|
2022-05-26 16:35:47 +08:00
|
|
|
assert plugin.module_name == "plugins.nested"
|
|
|
|
|
2024-04-20 14:47:12 +08:00
|
|
|
# check get plugin by sub plugin exact module name
|
|
|
|
plugin = nonebot.get_plugin_by_module_name(
|
|
|
|
"plugins.nested.plugins.nested_subplugin"
|
|
|
|
)
|
|
|
|
assert plugin
|
|
|
|
assert plugin.id_ == "nested:nested_subplugin"
|
|
|
|
assert plugin.name == "nested_subplugin"
|
|
|
|
assert plugin.module_name == "plugins.nested.plugins.nested_subplugin"
|
|
|
|
|
2022-05-26 16:35:47 +08:00
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2023-02-22 23:32:48 +08:00
|
|
|
async def test_get_available_plugin():
|
|
|
|
old_managers = _managers.copy()
|
|
|
|
_managers.clear()
|
|
|
|
try:
|
|
|
|
_managers.append(PluginManager(["plugins.export", "plugin.require"]))
|
|
|
|
|
|
|
|
# check get available plugins
|
2024-04-20 14:47:12 +08:00
|
|
|
plugin_ids = nonebot.get_available_plugin_names()
|
|
|
|
assert plugin_ids == {"export", "require"}
|
2023-02-22 23:32:48 +08:00
|
|
|
finally:
|
|
|
|
_managers.clear()
|
|
|
|
_managers.extend(old_managers)
|
2024-02-05 14:00:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_get_plugin_config():
|
|
|
|
class Config(BaseModel):
|
|
|
|
plugin_config: int
|
|
|
|
|
|
|
|
# check get plugin config
|
|
|
|
config = nonebot.get_plugin_config(Config)
|
|
|
|
assert isinstance(config, Config)
|
|
|
|
assert config.plugin_config == 1
|