added base tests
This commit is contained in:
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
45
tests/test_bot.py
Normal file
45
tests/test_bot.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import tracemalloc
|
||||
|
||||
from disnake import Intents
|
||||
from disnake.ext import commands
|
||||
|
||||
tracemalloc.start()
|
||||
|
||||
intents = Intents(messages=True,
|
||||
message_content=True,
|
||||
)
|
||||
|
||||
|
||||
class Cog(commands.Cog):
|
||||
@commands.command()
|
||||
async def my_cmd(self, ctx):
|
||||
pass
|
||||
|
||||
|
||||
class CogTwo(commands.Cog):
|
||||
async def bot_check(self, ctx) -> bool:
|
||||
pass
|
||||
|
||||
|
||||
class CogThree(commands.Cog):
|
||||
async def bot_check_once(self, ctx) -> bool:
|
||||
pass
|
||||
|
||||
|
||||
class CogFour(commands.Cog):
|
||||
async def bot_slash_command_check(self, ctx) -> bool:
|
||||
pass
|
||||
|
||||
|
||||
class CogFive(commands.Cog):
|
||||
async def can_run(self, ctx) -> bool:
|
||||
pass
|
||||
|
||||
|
||||
def test_bot():
|
||||
bot = commands.Bot(command_prefix='$', intents=intents)
|
||||
bot.add_cog(Cog())
|
||||
bot.add_cog(CogTwo())
|
||||
bot.add_cog(CogThree())
|
||||
bot.add_cog(CogFour())
|
||||
bot.add_cog(CogFive())
|
||||
70
tests/test_lib_Cog.py
Normal file
70
tests/test_lib_Cog.py
Normal file
@@ -0,0 +1,70 @@
|
||||
import asyncio
|
||||
import inspect
|
||||
import os
|
||||
import tracemalloc
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
from disnake.ext import commands
|
||||
|
||||
from ..lib.Logger import logger
|
||||
|
||||
tracemalloc.start()
|
||||
|
||||
pytest_plugins = ('pytest_asyncio',)
|
||||
|
||||
|
||||
def asyncio_run(async_func):
|
||||
def wrapper(*args, **kwargs):
|
||||
return asyncio.run(async_func(*args, **kwargs))
|
||||
|
||||
wrapper.__signature__ = inspect.signature(async_func) # without this, fixtures are not injected
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
absolute_path = os.path.dirname(__file__)
|
||||
relative_path = "../cogs"
|
||||
full_path = os.path.join(absolute_path, relative_path)
|
||||
return_value = ['cog1', 'cog2']
|
||||
|
||||
bot = commands.InteractionBot()
|
||||
logger = logger
|
||||
|
||||
|
||||
@pytest.fixture(params=['cog1', 'cog2'])
|
||||
def mock_cog_list(mocker):
|
||||
async_mock = AsyncMock()
|
||||
mocker.patch('discord_bot.lib.CogsPrep.cog_list', return_value=async_mock)
|
||||
return async_mock
|
||||
|
||||
|
||||
@pytest.fixture(params=['load',
|
||||
'unload',
|
||||
'reload',
|
||||
'enable',
|
||||
'disable'])
|
||||
def mock_work_with_cogs(mocker, mock_cog_list):
|
||||
mock_cog_list.return_value = return_value
|
||||
result = asyncio.run(mock_cog_list(full_path))
|
||||
|
||||
async_mock = AsyncMock()
|
||||
mocker.patch('discord_bot.lib.CogsPrep.work_with_cogs', params=async_mock)
|
||||
return async_mock
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_cog_list(mock_cog_list):
|
||||
# Assuming there are two .py files in the folder
|
||||
mock_cog_list.return_value = return_value
|
||||
result = await mock_cog_list(full_path)
|
||||
assert result == return_value
|
||||
|
||||
|
||||
@patch('discord_bot.lib.CogsPrep.work_with_cogs', return_value=return_value)
|
||||
@pytest.mark.asyncio
|
||||
async def test_work_with_cogs(mock_work_with_cogs):
|
||||
var = mock_work_with_cogs
|
||||
result = await mock_work_with_cogs()
|
||||
# Check if the logger was called once with a message indicating that cog1 has been loaded
|
||||
assert result
|
||||
Reference in New Issue
Block a user