Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher committed Nov 21, 2024
1 parent 3991e77 commit 715d5ab
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 20 deletions.
51 changes: 34 additions & 17 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
31 changes: 28 additions & 3 deletions tests/unit/discord/test_discord_bot.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# standard imports
import asyncio
import os

# lib imports
import pytest

# local imports
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"
Expand All @@ -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

0 comments on commit 715d5ab

Please sign in to comment.