2021-12-10 17:01:56 +08:00
import pytest
2023-02-26 14:15:10 +08:00
from nonebug import App
2021-12-16 17:28:57 +08:00
2023-02-22 23:32:48 +08:00
import nonebot
2023-08-26 11:03:24 +08:00
from nonebot . drivers import Driver , ASGIMixin , ReverseDriver
2023-02-26 14:15:10 +08:00
from nonebot import (
get_app ,
get_bot ,
get_asgi ,
get_bots ,
get_driver ,
get_adapter ,
get_adapters ,
)
2021-12-10 17:01:56 +08:00
@pytest.mark.asyncio
2023-02-22 23:32:48 +08:00
async def test_init ( ) :
env = nonebot . get_driver ( ) . env
2021-12-10 17:01:56 +08:00
assert env == " test "
2023-02-22 23:32:48 +08:00
config = nonebot . get_driver ( ) . config
2023-07-04 10:45:55 +08:00
assert config . nickname == { " test " }
assert config . superusers == { " test " , " fake:faketest " }
assert config . api_timeout is None
assert config . simple_none is None
2021-12-16 17:28:57 +08:00
assert config . config_from_env == { " test " : " test " }
2022-06-18 14:47:42 +08:00
assert config . config_override == " new "
2021-12-10 17:01:56 +08:00
assert config . config_from_init == " init "
assert config . common_config == " common "
2022-10-14 09:58:44 +08:00
assert config . common_override == " new "
assert config . nested_dict == { " a " : 1 , " b " : 2 , " c " : { " d " : 3 } }
assert config . nested_missing_dict == { " a " : 1 , " b " : { " c " : 2 } }
assert config . not_nested == " some string "
2021-12-12 18:19:08 +08:00
2021-12-16 17:28:57 +08:00
@pytest.mark.asyncio
2023-06-19 17:48:59 +08:00
async def test_get_driver ( app : App , monkeypatch : pytest . MonkeyPatch ) :
2023-02-22 23:32:48 +08:00
with monkeypatch . context ( ) as m :
m . setattr ( nonebot , " _driver " , None )
2023-06-24 14:47:35 +08:00
with pytest . raises ( ValueError , match = " initialized " ) :
2023-02-22 23:32:48 +08:00
get_driver ( )
2021-12-16 17:28:57 +08:00
2023-06-19 17:48:59 +08:00
@pytest.mark.asyncio
async def test_get_asgi ( app : App , monkeypatch : pytest . MonkeyPatch ) :
2021-12-16 17:28:57 +08:00
driver = get_driver ( )
assert isinstance ( driver , ReverseDriver )
2023-08-26 11:03:24 +08:00
assert isinstance ( driver , ASGIMixin )
2021-12-16 17:28:57 +08:00
assert get_asgi ( ) == driver . asgi
2023-06-19 17:48:59 +08:00
@pytest.mark.asyncio
async def test_get_app ( app : App , monkeypatch : pytest . MonkeyPatch ) :
driver = get_driver ( )
assert isinstance ( driver , ReverseDriver )
2023-08-26 11:03:24 +08:00
assert isinstance ( driver , ASGIMixin )
2021-12-16 17:28:57 +08:00
assert get_app ( ) == driver . server_app
2023-06-19 17:48:59 +08:00
@pytest.mark.asyncio
async def test_get_adapter ( app : App , monkeypatch : pytest . MonkeyPatch ) :
2023-02-26 14:15:10 +08:00
async with app . test_api ( ) as ctx :
adapter = ctx . create_adapter ( )
adapter_name = adapter . get_name ( )
with monkeypatch . context ( ) as m :
m . setattr ( Driver , " _adapters " , { adapter_name : adapter } )
assert get_adapters ( ) == { adapter_name : adapter }
assert get_adapter ( adapter_name ) is adapter
assert get_adapter ( adapter . __class__ ) is adapter
2023-06-24 14:47:35 +08:00
with pytest . raises ( ValueError , match = " registered " ) :
2023-02-26 14:15:10 +08:00
get_adapter ( " not exist " )
2023-06-19 17:48:59 +08:00
@pytest.mark.asyncio
async def test_run ( app : App , monkeypatch : pytest . MonkeyPatch ) :
2021-12-16 17:28:57 +08:00
runned = False
def mock_run ( * args , * * kwargs ) :
nonlocal runned
runned = True
2023-06-24 14:47:35 +08:00
assert args == ( " arg " , )
assert kwargs == { " kwarg " : " kwarg " }
2021-12-16 17:28:57 +08:00
2023-06-19 17:48:59 +08:00
driver = get_driver ( )
with monkeypatch . context ( ) as m :
m . setattr ( driver , " run " , mock_run )
nonebot . run ( " arg " , kwarg = " kwarg " )
2021-12-16 17:28:57 +08:00
assert runned
2023-06-19 17:48:59 +08:00
@pytest.mark.asyncio
async def test_get_bot ( app : App , monkeypatch : pytest . MonkeyPatch ) :
driver = get_driver ( )
2023-06-24 14:47:35 +08:00
with pytest . raises ( ValueError , match = " no bots " ) :
2021-12-16 17:28:57 +08:00
get_bot ( )
2023-06-19 17:48:59 +08:00
with monkeypatch . context ( ) as m :
m . setattr ( driver , " _bots " , { " test " : " test " } )
assert get_bot ( ) == " test "
assert get_bot ( " test " ) == " test "
assert get_bots ( ) == { " test " : " test " }