diff --git a/tests/conftest.py b/tests/conftest.py index 451baa0..a40104a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,8 +1,10 @@ # standard imports import asyncio +import time # lib imports import dotenv +import pytest import pytest_asyncio dotenv.load_dotenv(override=False) # environment secrets take priority over .env file @@ -12,25 +14,40 @@ from src.discord import bot as d_bot # noqa: E402 -@pytest_asyncio.fixture(loop_scope='function') -async def discord_bot(): - # event_loop fixture is deprecated - _loop = asyncio.get_event_loop() - - bot = d_bot.Bot(loop=_loop) - future = asyncio.run_coroutine_threadsafe(bot.start(token=bot.token), _loop) - await bot.wait_until_ready() # Wait until the bot is ready - +# @pytest_asyncio.fixture(loop_scope='function') +@pytest.fixture(scope='session') +def discord_bot(): + # # event_loop fixture is deprecated + # _loop = asyncio.get_event_loop() + # + # bot = d_bot.Bot(loop=_loop) + # future = asyncio.run_coroutine_threadsafe(bot.start(token=bot.token), _loop) + # await bot.wait_until_ready() # Wait until the bot is ready + # + # globals.DISCORD_BOT = bot + # + # yield bot + # bot.stop(future=future) + # + # # wait for the bot to finish + # counter = 0 + # while not future.done() and counter < 30: + # await asyncio.sleep(1) + # counter += 1 + # future.cancel() # Cancel the bot when the tests are done + # + # globals.DISCORD_BOT = None + + # _loop = asyncio.get_event_loop() + # print(f'Using loop: {_loop}') + bot = d_bot.Bot() + bot.start_threaded() globals.DISCORD_BOT = bot - yield bot - bot.stop(future=future) + while not bot.is_ready(): # Wait until the bot is ready + time.sleep(1) - # wait for the bot to finish - counter = 0 - while not future.done() and counter < 30: - await asyncio.sleep(1) - counter += 1 - future.cancel() # Cancel the bot when the tests are done + yield bot + bot.stop() globals.DISCORD_BOT = None diff --git a/tests/unit/discord/test_discord_bot.py b/tests/unit/discord/test_discord_bot.py index f51313e..61e8d08 100644 --- a/tests/unit/discord/test_discord_bot.py +++ b/tests/unit/discord/test_discord_bot.py @@ -1,3 +1,7 @@ +# standard imports +import asyncio +import os + # lib imports import pytest @@ -5,8 +9,8 @@ from src.common import common -@pytest.mark.asyncio -async def test_bot_on_ready(discord_bot): +# @pytest.mark.asyncio +def test_bot_on_ready(discord_bot): assert discord_bot is not None assert discord_bot.guilds assert discord_bot.guilds[0].name == "ReenigneArcher's test server" @@ -15,4 +19,25 @@ async def test_bot_on_ready(discord_bot): assert discord_bot.user.avatar # compare the bot avatar to our intended avatar - assert await discord_bot.user.avatar.read() == common.get_avatar_bytes() + # assert await discord_bot.user.avatar.read() == common.get_avatar_bytes() + future = asyncio.run_coroutine_threadsafe(discord_bot.user.avatar.read(), discord_bot.loop) + assert future.result() == common.get_avatar_bytes() + + +def test_send_message(discord_bot): + channel_id = int(os.environ['DISCORD_REDDIT_CHANNEL_ID']) + message = "This is a test message." + embeds = None + message = discord_bot.send_message(channel_id=channel_id, message=message, embeds=embeds) + assert message.content == "This is a test message." + assert message.channel.id == channel_id + assert message.author.id == 939171917578002502 + assert message.author.name == common.bot_name + + avatar_future = asyncio.run_coroutine_threadsafe(message.author.avatar.read(), discord_bot.loop) + assert avatar_future.result() == common.get_avatar_bytes() + + assert message.author.display_name == common.bot_name + assert message.author.discriminator == "7085" + assert message.author.bot is True + assert message.author.system is False