Skip to content

Commit

Permalink
✅ (tests/src/cmds/core/test_user.py): Fix tests patch DB call
Browse files Browse the repository at this point in the history
  • Loading branch information
dimoschi committed Nov 22, 2024
1 parent ab2ce3a commit 3178f52
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/cmds/core/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async def kick(self, ctx: ApplicationContext, user: Member, reason: str, evidenc
)

await ctx.guild.kick(user=member, reason=reason)
infraction_reason = f"{ctx.user.name} was kicked on {datetime.now()} for {reason}"
infraction_reason = f"{ctx.user.name} was kicked on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} for {reason}"
await add_infraction(ctx.guild, member, 0, infraction_reason, ctx.user)
return await ctx.respond(f"{member.name} got the boot!")

Expand Down
12 changes: 9 additions & 3 deletions tests/src/cmds/core/test_user.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from unittest.mock import AsyncMock, patch

import pytest
Expand All @@ -10,9 +11,10 @@ class TestUserCog:
"""Test the `User` cog."""

@pytest.mark.asyncio
async def test_kick_success(self, ctx, bot):
async def test_kick_success(self, ctx, guild, bot, session):
ctx.user = helpers.MockMember(id=1, name="Test Moderator")
user_to_kick = helpers.MockMember(id=2, name="User to Kick", bot=False)
ctx.guild = guild
ctx.guild.kick = AsyncMock()
bot.get_member_or_user = AsyncMock(return_value=user_to_kick)

Expand All @@ -21,19 +23,23 @@ async def test_kick_success(self, ctx, bot):
user_to_kick.name = "User to Kick"

with (
patch('src.cmds.core.user.add_infraction', new_callable=AsyncMock) as add_infraction_mock,
patch('src.cmds.core.user.add_evidence_note', new_callable=AsyncMock) as add_evidence_mock,
patch('src.cmds.core.user.member_is_staff', return_value=False)
):
cog = user.UserCog(bot)
await cog.kick.callback(cog, ctx, user_to_kick, "Violation of rules")

add_evidence_mock.assert_called_once_with(user_to_kick.id, "kick", "Violation of rules", None, ctx.user.id)
reason = "Violation of rules"
add_evidence_mock.assert_called_once_with(user_to_kick.id, "kick", reason, None, ctx.user.id)
add_infraction_mock.assert_called_once_with(
ctx.guild, user_to_kick, 0, f"{ctx.user.name} was kicked on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} for {reason}", ctx.user
)

# Assertions
ctx.guild.kick.assert_called_once_with(user=user_to_kick, reason="Violation of rules")
ctx.respond.assert_called_once_with("User to Kick got the boot!")


def test_setup(self, bot):
"""Test the setup method of the cog."""
# Invoke the command
Expand Down

0 comments on commit 3178f52

Please sign in to comment.