Commit 1709f5ed authored by Jeremy Pallats's avatar Jeremy Pallats 💬
Browse files

Emoji tests and KOS cmd.

parent 1525ac7f
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -1047,7 +1047,7 @@ class KOS(Action):
    """
    Handle the KOS command.
    """
    async def report(self):
    async def report(self):  # pragma: no cover
        """
        Handle the reporting of a new cmdr.
        First ask for approval of addition, then add to kos list.
+2 −4
Original line number Diff line number Diff line
@@ -37,8 +37,6 @@ try:
    import uvloop
    asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
    asyncio.get_event_loop().set_debug(True)
except ImportError:
    print("Falling back to default python loop.")
finally:
    print("Default event loop:", asyncio.get_event_loop())

@@ -79,7 +77,7 @@ class EmojiResolver():
        """
        for guild in guilds:
            emoji_names = [emoji.name for emoji in guild.emojis]
            self.emojis[guild.name] = dict(zip(emoji_names, guild.emojis))
            self.emojis[guild.id] = dict(zip(emoji_names, guild.emojis))

    def fix(self, content, guild):
        """
@@ -88,7 +86,7 @@ class EmojiResolver():
        Embed emojis into the content just like on guild surrounded by ':'. Example:
            Status :Fortifying:
        """
        emojis = self.emojis[guild.name]
        emojis = self.emojis[guild.id]
        for embed in list(set(re.findall(r':\S+:', content))):
            try:
                emoji = emojis[embed[1:-1]]
+43 −1
Original line number Diff line number Diff line
@@ -51,7 +51,7 @@ def patch_scanners():
        'hudson_undermine': scanner,
    }

    yield
    yield scanner

    cog.actions.SCANNERS = old_scanners

@@ -86,6 +86,27 @@ def action_map(fake_message, fake_bot):
    #  print(str(f_bot.send_message.call_args).replace("\\n", "\n"))


class FakeToWrap():
    def __init__(self):
        self.msg = aiomock.Mock(author=None, mentions=["FakeUser"])
        self.log = aiomock.AIOMock()
        self.bot = aiomock.AIOMock()
        self.bot.on_message.async_return_value = True

    @cog.actions.check_mentions
    async def execute(self):
        print("Hello")


@pytest.mark.asyncio
async def test_check_mentions():
    fakeself = FakeToWrap()
    await fakeself.execute()

    assert fakeself.msg.author
    fakeself.bot.on_message.assert_called_with(fakeself.msg)


# General Parse Fails
@pytest.mark.asyncio
async def test_cmd_fail(f_bot):
@@ -1302,6 +1323,27 @@ async def test_cmd_dist_invalid_args(f_bot):
        await action_map(msg, f_bot).execute()


@pytest.mark.asyncio
async def test_cmd_kos_search(f_bot, f_kos):
    msg = fake_msg_gears("!kos search bad")

    await action_map(msg, f_bot).execute()

    expect = "bad_guy   | Hudson  | KILL         | Pretty bad guy"
    assert expect in str(f_bot.send_message.call_args).replace("\\n", "\n")


@pytest.mark.asyncio
async def test_cmd_kos_pull(f_bot, patch_scanners):
    msg = fake_msg_gears("!kos pull")

    await action_map(msg, f_bot).execute()

    # FIXME: Improve
    #  assert patch_scanners.parse_sheet.assert_called()
    f_bot.send_message.assert_called_with(msg.channel, "KOS list refreshed from sheet.")


@pytest.mark.asyncio
async def test_cmd_near_control(f_bot):
    msg = fake_msg_gears("!near control winters rana")
+43 −0
Original line number Diff line number Diff line
"""
Test the bot's main functions.
"""
from cog.bot import EmojiResolver
from tests.conftest import Server, Emoji


def test_emoji__init__():
    guild = Server('myguild')
    guild.emojis = [Emoji('duck'), Emoji('car'), Emoji('sleep')]
    emo = EmojiResolver()
    assert emo.emojis == {}


def test_emoji__str__():
    guild = Server('myguild')
    guild.emojis = [Emoji('car'), Emoji('duck'), Emoji('sleep')]
    emo = EmojiResolver()
    emo.update([guild])

    assert "'duck': Emoji: Emoji-5 duck," in str(emo)


def test_emoji_update():
    guild = Server('myguild')
    guild.emojis = [Emoji('car'), Emoji('duck'), Emoji('sleep')]
    emo = EmojiResolver()
    emo.update([guild])

    expected = {
        guild.id: {
            'car': guild.emojis[0],
            'duck': guild.emojis[1],
            'sleep': guild.emojis[2],
        }
    }
    assert emo.emojis == expected


def test_emoji_fix():
    guild = Server('myguild')
    guild.emojis = [Emoji('car'), Emoji('duck'), Emoji('sleep')]
    emo = EmojiResolver()
    emo.update([guild])

    assert emo.fix(":duck: :sleep:", guild) == "[duck] [sleep]"
+11 −0
Original line number Diff line number Diff line
@@ -380,10 +380,12 @@ class FakeObject():
        return "{}: {}".format(self.__class__.__name__, self.name)


# TODO: Rename Guild.
class Server(FakeObject):
    def __init__(self, name, id=None):
        super().__init__(name, id)
        self.channels = []
        self.emojis = []

    def add(self, channel):
        self.channels.append(channel)
@@ -393,6 +395,15 @@ class Server(FakeObject):
        # return super().__repr__() + channels


class Emoji(FakeObject):
    def __init__(self, name, id=None):
        super().__init__(name, id)

    def __str__(self):
        return "[{}]".format(self.name)



class Channel(FakeObject):
    def __init__(self, name, *, srv=None, id=None):
        super().__init__(name, id)