Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions bot/extensions/translator_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ async def language_autocomplete(_: Interaction, current: str) -> list[Choice[str

return [
Choice(name=language.capitalize(), value=language)
for language in languages[:25]
for language in languages
if current.lower() in language.lower()
]
][:25]


class TranslatorCog(
Expand All @@ -36,21 +36,24 @@ class TranslatorCog(
usage="sentence={sentence}",
)
@autocomplete(translate_into=language_autocomplete)
async def translator(self, ctx: Context, *, sentence: str, translate_into: str):
@autocomplete(translate_from=language_autocomplete)
async def translator(self, ctx: Context, *, sentence: str, translate_from: str | None = "auto", translate_into: str):
"""Translate a sentence or word from any language into any languages.
:param ctx: The context object.
:type ctx: Context
:param sentence: The sentence or word to be translated.
:type sentence: str
:param translate_from: The language code for the source language.
:type translate_from: str | None
:param translate_into: The language code for the target language.
:type translate_into: str
:return: Embed with original input and its translation
"""
await ctx.defer()

text_translator = Translator()
translated_text = text_translator.translate(sentence, dest=translate_into)
translated_text = text_translator.translate(sentence, src=translate_from, dest=translate_into)

embed = Embed(color=self.bot.default_color)

Expand All @@ -60,8 +63,8 @@ async def translator(self, ctx: Context, *, sentence: str, translate_into: str):
inline=False,
)
embed.add_field(
name=f"{translate_into} Translation",
value=translated_text.text,
name=f"{translate_into.capitalize()} Translation",
value=translated_text.text.capitalize(),
inline=False,
)

Expand Down
13 changes: 13 additions & 0 deletions tests/extensions/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from unittest.mock import MagicMock

import pytest


@pytest.fixture
def mock_bot():
"""Create a mock Discord bot instance."""
bot = MagicMock()
bot.default_color = 0xFFFFFF
bot.app.config.get = MagicMock(return_value=None)
bot.scheduler = MagicMock()
return bot
12 changes: 1 addition & 11 deletions tests/extensions/test_reminder_cog.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re
from datetime import UTC, datetime, timedelta
from unittest.mock import AsyncMock, MagicMock
from unittest.mock import AsyncMock

import pytest
from dateutil.tz import tzlocal
Expand All @@ -10,16 +10,6 @@
from bot.extensions.reminder_cog import ReminderCog


@pytest.fixture
def mock_bot():
"""Create a mock Discord bot instance."""
bot = MagicMock()
bot.default_color = 0xFFFFFF
bot.app.config.get = MagicMock(return_value=None)
bot.scheduler = MagicMock()
return bot


@pytest.fixture
def reminder_cog(mock_bot):
"""Instantiate the ReminderCog with a mock bot."""
Expand Down
10 changes: 0 additions & 10 deletions tests/extensions/test_threads_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@
from bot.models.extensions.thread import Thread


@pytest.fixture
def mock_bot():
"""Create a mock Discord bot instance."""
bot = MagicMock()
bot.default_color = 0xFFFFFF
bot.app.config.get = MagicMock(return_value=None)
bot.scheduler = MagicMock()
return bot


@pytest.fixture
def threads_cog(mock_bot):
"""Instantiate the ThreadsCog with a mock bot."""
Expand Down
6 changes: 0 additions & 6 deletions tests/extensions/test_time_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@
from bot.extensions.time_cog import TimeCog


@pytest.fixture
def mock_bot():
"""Create a mock Discord bot instance."""
return MagicMock()


@pytest.fixture
def time_cog(mock_bot):
"""Instantiate the TimeCog with a mock bot."""
Expand Down
73 changes: 73 additions & 0 deletions tests/extensions/test_translator_cog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from unittest.mock import AsyncMock, MagicMock

import pytest
from discord import Embed
from discord.app_commands import Choice

from bot.extensions.translator_cog import TranslatorCog, language_autocomplete


@pytest.fixture
def translator_cog(mock_bot):
"""Instantiate the TranslatorCog with a mock bot."""

return TranslatorCog(mock_bot)

@pytest.mark.asyncio
async def test_translator(translator_cog, mock_bot):
ctx = MagicMock()
ctx.defer = AsyncMock()
ctx.send = AsyncMock()

translate_from = "English"
translate_into = "Arabic"
sentence = "Funny"
translated_text = "مضحك"

await translator_cog.translator(
translator_cog,
ctx,
sentence="Funny",
translate_from="English",
translate_into="Arabic"
)

result = Embed(color=mock_bot.default_color)

result.add_field(
name=f"{translate_from} Original",
value=sentence,
inline=False,
)
result.add_field(
name=f"{translate_into} Translation",
value=translated_text,
inline=False,
)

ctx.send.assert_awaited_once_with(embed=result)


@pytest.mark.asyncio
async def test_language_autocomplete_empty_input():
empty = await language_autocomplete(None, "")
assert len(empty) == 25

@pytest.mark.asyncio
async def test_language_autocomplete_invalid_input():
invalid = await language_autocomplete(None, "1232")
assert invalid == []

@pytest.mark.asyncio
async def test_language_autocomplete_ara_input():
ara = await language_autocomplete(None, "ara")
assert ara == [
Choice(name='Arabic', value='arabic'),
Choice(name='Gujarati', value='gujarati'),
Choice(name='Marathi', value='marathi')
]

@pytest.mark.asyncio
async def test_language_autocomplete_zu_input():
zu = await language_autocomplete(None, "zu")
assert zu == [Choice(name='Zulu', value='zulu')]
Loading