updated lib_Cog test
This commit is contained in:
2
bot.py
2
bot.py
@@ -36,7 +36,7 @@ bot = Bot(command_prefix=determine_prefix,
|
||||
|
||||
bot.i18n.load("locale/")
|
||||
|
||||
asyncio.run(work_with_cogs('load', bot, asyncio.run(cog_list())))
|
||||
asyncio.run(work_with_cogs('load', bot, cog_list()))
|
||||
|
||||
|
||||
@bot.event
|
||||
|
||||
@@ -8,6 +8,8 @@ work_with_cogs: loads, reloads and unloads cogs files
|
||||
"""
|
||||
from os import listdir, rename
|
||||
|
||||
from disnake.ext.commands.interaction_bot_base import InteractionBotBase
|
||||
|
||||
from .Logger import logger
|
||||
|
||||
|
||||
@@ -20,27 +22,29 @@ def cog_list(fold: str = './cogs') -> list:
|
||||
|
||||
|
||||
async def work_with_cogs(what_do: str,
|
||||
bot,
|
||||
bot: InteractionBotBase,
|
||||
cog: str | list):
|
||||
if isinstance(cog, str):
|
||||
cog = cog.split()
|
||||
for _filename in cog:
|
||||
if _filename.endswith('.py'):
|
||||
_filename = _filename.split('.')[0]
|
||||
if what_do == "load":
|
||||
await bot.load_extension(f'cogs.{_filename}')
|
||||
bot.load_extension(f'cogs.{_filename}')
|
||||
logger.info(f'Cog {_filename} loaded')
|
||||
elif what_do == 'unload':
|
||||
await bot.unload_extension(f'cogs.{_filename}')
|
||||
bot.unload_extension(f'cogs.{_filename}')
|
||||
logger.info(f'Cog {_filename} unloaded')
|
||||
elif what_do == 'reload':
|
||||
await bot.reload_extension(f'cogs.{_filename}')
|
||||
bot.reload_extension(f'cogs.{_filename}')
|
||||
logger.info(f'Cog {_filename} reloaded')
|
||||
elif what_do == 'disable':
|
||||
await bot.unload_extension(f'cogs.{_filename}')
|
||||
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')
|
||||
await bot.load_extension(f'cogs.{_filename}')
|
||||
bot.load_extension(f'cogs.{_filename}')
|
||||
logger.info(f'Cog {_filename} started and enabled')
|
||||
|
||||
@@ -1,29 +1,28 @@
|
||||
import pytest
|
||||
from disnake.ext.commands import Bot
|
||||
from disnake.ext.commands.common_bot_base import CommonBotBase
|
||||
from mock import mock
|
||||
|
||||
from ..lib.CogsPrep import cog_list, work_with_cogs
|
||||
from lib.CogsPrep import cog_list, work_with_cogs
|
||||
|
||||
|
||||
@mock.patch('discord_bot.lib.CogsPrep.cog_list')
|
||||
def test_cog_list(mock_cog_list):
|
||||
with mock.patch('discord_bot.lib.CogsPrep.listdir') as MockClass:
|
||||
def test_cog_list():
|
||||
with mock.patch('lib.CogsPrep.listdir') as MockClass:
|
||||
MockClass.return_value = ['cog1.py', 'cog2.py']
|
||||
result = cog_list()
|
||||
assert result == ['cog1', 'cog2']
|
||||
|
||||
|
||||
# @pytest.mark.skip(reason=None)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@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.AsyncMock()
|
||||
with (mock.patch('discord_bot.lib.CogsPrep.logger.info') as mocked_logger):
|
||||
mock_bot = mock.AsyncMock()
|
||||
await mock_bot(spec=Bot)
|
||||
with mock.patch('discord_bot.lib.CogsPrep.rename') as mock_rename:
|
||||
mock_rename.return_value = True
|
||||
with (mock.patch('lib.CogsPrep.rename') as mock_rename):
|
||||
mock_rename.return_value = None
|
||||
mock_bot = mock.MagicMock(spec=CommonBotBase)
|
||||
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}"
|
||||
if what_do in ['load', 'enable']:
|
||||
assert mock_bot.load_extension.called
|
||||
elif what_do in ['unload', 'disable']:
|
||||
assert mock_bot.unload_extension.called
|
||||
elif what_do == 'reload':
|
||||
assert mock_bot.reload_extension.called
|
||||
|
||||
Reference in New Issue
Block a user