2021-12-10 17:01:56 +08:00
|
|
|
import os
|
2021-12-12 18:19:08 +08:00
|
|
|
import sys
|
|
|
|
from typing import TYPE_CHECKING, Set
|
2021-12-10 17:01:56 +08:00
|
|
|
|
|
|
|
import pytest
|
2021-12-16 17:28:57 +08:00
|
|
|
|
2021-12-12 18:19:08 +08:00
|
|
|
from utils import load_plugin
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from nonebot.plugin import Plugin
|
2021-12-10 17:01:56 +08:00
|
|
|
|
2021-12-16 17:28:57 +08:00
|
|
|
os.environ["CONFIG_FROM_ENV"] = '{"test": "test"}'
|
2021-12-10 17:01:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
2021-12-16 17:28:57 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"nonebug_init",
|
|
|
|
[{"config_from_init": "init", "driver": "nonebot.drivers.fastapi:FullDriver"}],
|
|
|
|
indirect=True,
|
|
|
|
)
|
2021-12-10 17:01:56 +08:00
|
|
|
async def test_init(nonebug_init):
|
|
|
|
from nonebot import get_driver
|
2021-12-16 17:28:57 +08:00
|
|
|
from nonebot.drivers.fastapi import FullDriver
|
2021-12-10 17:01:56 +08:00
|
|
|
|
|
|
|
env = get_driver().env
|
|
|
|
assert env == "test"
|
|
|
|
|
2021-12-16 17:28:57 +08:00
|
|
|
assert isinstance(get_driver(), FullDriver)
|
|
|
|
|
2021-12-10 17:01:56 +08:00
|
|
|
config = get_driver().config
|
2021-12-16 17:28:57 +08:00
|
|
|
assert config.config_from_env == {"test": "test"}
|
2021-12-10 17:01:56 +08:00
|
|
|
assert config.config_from_init == "init"
|
|
|
|
assert config.common_config == "common"
|
2021-12-12 18:19:08 +08:00
|
|
|
|
|
|
|
|
2021-12-16 17:28:57 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_get(monkeypatch: pytest.MonkeyPatch, nonebug_clear):
|
|
|
|
import nonebot
|
|
|
|
from nonebot.drivers import ForwardDriver, ReverseDriver
|
|
|
|
from nonebot import get_app, get_bot, get_asgi, get_bots, get_driver
|
|
|
|
|
|
|
|
try:
|
|
|
|
get_driver()
|
|
|
|
assert False, "Driver can only be got after initialization"
|
|
|
|
except ValueError:
|
|
|
|
assert True
|
|
|
|
|
|
|
|
nonebot.init(driver="nonebot.drivers.fastapi")
|
|
|
|
|
|
|
|
driver = get_driver()
|
|
|
|
assert isinstance(driver, ReverseDriver)
|
|
|
|
assert get_asgi() == driver.asgi
|
|
|
|
assert get_app() == driver.server_app
|
|
|
|
|
|
|
|
runned = False
|
|
|
|
|
|
|
|
def mock_run(*args, **kwargs):
|
|
|
|
nonlocal runned
|
|
|
|
runned = True
|
|
|
|
assert args == ("arg",) and kwargs == {"kwarg": "kwarg"}
|
|
|
|
|
|
|
|
monkeypatch.setattr(driver, "run", mock_run)
|
|
|
|
nonebot.run("arg", kwarg="kwarg")
|
|
|
|
assert runned
|
|
|
|
|
|
|
|
try:
|
|
|
|
get_bot()
|
|
|
|
assert False
|
|
|
|
except ValueError:
|
|
|
|
assert True
|
|
|
|
|
|
|
|
monkeypatch.setattr(driver, "_clients", {"test": "test"})
|
|
|
|
assert get_bot() == "test"
|
|
|
|
assert get_bot("test") == "test"
|
|
|
|
assert get_bots() == {"test": "test"}
|
|
|
|
|
|
|
|
|
2021-12-12 18:19:08 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_load_plugin(load_plugin: Set["Plugin"]):
|
|
|
|
import nonebot
|
|
|
|
|
|
|
|
assert nonebot.get_loaded_plugins() == load_plugin
|
|
|
|
plugin = nonebot.get_plugin("depends")
|
|
|
|
assert plugin
|
|
|
|
assert plugin.module_name == "plugins.depends"
|
|
|
|
assert "plugins.depends" in sys.modules
|