ended test_lib_Cog.py
This commit is contained in:
2
bot.py
2
bot.py
@@ -7,7 +7,6 @@ from disnake import OptionType, Option, Localized, ApplicationCommandInteraction
|
||||
from disnake.ext.commands import Bot, is_owner
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from __init__ import __version__ as ver
|
||||
from lib.CogsPrep import work_with_cogs, cog_list
|
||||
from lib.Comands import determine_prefix
|
||||
from lib.Logger import logger
|
||||
@@ -45,7 +44,6 @@ async def on_ready():
|
||||
logger.info('Bot started')
|
||||
logger.info(f'Disnake version {__version__}')
|
||||
logger.info(f'We have logged in as {bot.user}')
|
||||
logger.info(f'Version of bot is - v{ver}')
|
||||
|
||||
|
||||
@bot.slash_command(
|
||||
|
||||
@@ -8,6 +8,8 @@ work_with_cogs: loads, reloads and unloads cogs files
|
||||
"""
|
||||
from os import listdir, rename
|
||||
|
||||
from disnake.ext.commands import Bot
|
||||
|
||||
from .Logger import logger
|
||||
|
||||
|
||||
@@ -19,13 +21,15 @@ async def cog_list(fold: str = './cogs') -> list:
|
||||
return cogs_list
|
||||
|
||||
|
||||
async def work_with_cogs(what_do, bot, cog):
|
||||
async def work_with_cogs(what_do: str,
|
||||
bot: Bot,
|
||||
cog: str | list):
|
||||
if isinstance(cog, str):
|
||||
cog = cog.split()
|
||||
for _filename in cog:
|
||||
if what_do == "lad":
|
||||
if what_do == "load":
|
||||
bot.load_extension(f'cogs.{_filename}')
|
||||
logger.info(f'Loaded cog {_filename}')
|
||||
logger.info(f'Cog {_filename} loaded')
|
||||
elif what_do == 'unload':
|
||||
bot.unload_extension(f'cogs.{_filename}')
|
||||
logger.info(f'Cog {_filename} unloaded')
|
||||
@@ -34,8 +38,11 @@ async def work_with_cogs(what_do, bot, cog):
|
||||
logger.info(f'Cog {_filename} reloaded')
|
||||
elif what_do == 'disable':
|
||||
bot.unload_extension(f'cogs.{_filename}')
|
||||
rename(f'cogs/{_filename}.py',
|
||||
f'cogs/disabled/{_filename}.py')
|
||||
logger.info(f'Cog {_filename} stopped and disabled')
|
||||
elif what_do == 'enable':
|
||||
rename(f'cogs/disabled/{_filename}.py', f'cogs/{_filename}.py')
|
||||
rename(f'cogs/disabled/{_filename}.py',
|
||||
f'cogs/{_filename}.py')
|
||||
bot.load_extension(f'cogs.{_filename}')
|
||||
logger.info(f'Cog {_filename} started and enabled')
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
import tracemalloc
|
||||
|
||||
from disnake import Intents
|
||||
from disnake.ext import commands
|
||||
|
||||
tracemalloc.start()
|
||||
|
||||
intents = Intents(messages=True,
|
||||
message_content=True,
|
||||
|
||||
@@ -1,70 +1,29 @@
|
||||
import asyncio
|
||||
import inspect
|
||||
import os
|
||||
import tracemalloc
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
import pytest
|
||||
from disnake.ext import commands
|
||||
from disnake.ext.commands import Bot
|
||||
from mock import mock
|
||||
|
||||
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
|
||||
from ..lib.CogsPrep import cog_list, work_with_cogs
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@mock.patch('discord_bot.lib.CogsPrep.cog_list')
|
||||
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
|
||||
with mock.patch('discord_bot.lib.CogsPrep.listdir') as MockClass:
|
||||
MockClass.return_value = ['cog1.py', 'cog2.py']
|
||||
result = await cog_list()
|
||||
assert result == ['cog1', 'cog2']
|
||||
|
||||
|
||||
@patch('discord_bot.lib.CogsPrep.work_with_cogs', return_value=return_value)
|
||||
# @pytest.mark.skip(reason=None)
|
||||
|
||||
@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
|
||||
@pytest.mark.parametrize("cog", ["cog1.py", "cog2"])
|
||||
@pytest.mark.parametrize("what_do", ['load', 'unload', 'reload', 'disable', 'enable'])
|
||||
async def test_work_with_cogs(what_do, cog):
|
||||
mock_logger = mock.MagicMock()
|
||||
with mock.patch('discord_bot.lib.CogsPrep.logger.info') as mocked_logger:
|
||||
mock_bot = mock.MagicMock(spec=Bot)
|
||||
with mock.patch('discord_bot.lib.CogsPrep.rename') as mock_rename:
|
||||
mock_rename.return_value = True
|
||||
result = await work_with_cogs(what_do, mock_bot, cog)
|
||||
assert len(mocked_logger.call_args_list) == 1, f"Not listed what_do {what_do}"
|
||||
|
||||
Reference in New Issue
Block a user