Esempio n. 1
0
    def setUp(self):
        self.bot = helpers.MockBot(user=helpers.MockMember(bot=True))
        self.syncer = TestSyncer(self.bot)
        self.guild = helpers.MockGuild()

        # Make sure `_get_diff` returns a MagicMock, not an AsyncMock
        self.syncer._get_diff.return_value = mock.MagicMock()
Esempio n. 2
0
    def setUp(self):
        """Sets up fresh objects for each test."""
        self.bot = helpers.MockBot()

        self.cog = information.Information(self.bot)

        self.ctx = helpers.MockContext()
        self.ctx.author.roles.append(self.moderator_role)
Esempio n. 3
0
    def test_duck_pond_correctly_initializes(self):
        """`__init__ should set `bot` and `webhook_id` attributes and schedule `fetch_webhook`."""
        bot = helpers.MockBot()
        cog = MagicMock()

        duck_pond.DuckPond.__init__(cog, bot)

        self.assertEqual(cog.bot, bot)
        self.assertEqual(cog.webhook_id, constants.Webhooks.duck_pond)
        bot.loop.create_loop.called_once_with(cog.fetch_webhook())
Esempio n. 4
0
    def setUp(self):
        self.bot = helpers.MockBot()

        self.role_syncer_patcher = mock.patch(
            "bot.cogs.sync.syncers.RoleSyncer", autospec=Syncer, spec_set=True)
        self.user_syncer_patcher = mock.patch(
            "bot.cogs.sync.syncers.UserSyncer", autospec=Syncer, spec_set=True)
        self.RoleSyncer = self.role_syncer_patcher.start()
        self.UserSyncer = self.user_syncer_patcher.start()

        self.cog = sync.Sync(self.bot)
Esempio n. 5
0
    def test_create_test_on_mock_bot_closes_passed_coroutine(self):
        """`bot.loop.create_task` should close the passed coroutine object to prevent warnings."""
        async def dementati():
            """Dummy coroutine for testing purposes."""

        coroutine_object = dementati()

        bot = helpers.MockBot()
        bot.loop.create_task(coroutine_object)
        with self.assertRaises(RuntimeError, msg="cannot reuse already awaited coroutine"):
            asyncio.run(coroutine_object)
Esempio n. 6
0
    def setUp(self):
        bot_patcher = mock.patch("bot.instance", new=helpers.MockBot())
        self.bot = bot_patcher.start()
        self.addCleanup(bot_patcher.stop)

        chunk_patcher = mock.patch("bot.exts.backend.sync._syncers.CHUNK_SIZE", 2)
        self.chunk_size = chunk_patcher.start()
        self.addCleanup(chunk_patcher.stop)

        self.chunk_count = 2
        self.users = [fake_user(id=i) for i in range(self.chunk_size * self.chunk_count)]
Esempio n. 7
0
    def setUp(self):
        """Set up steps executed before each test is run."""
        self.bot = helpers.MockBot()
        self.cog = information.Information(self.bot)

        self.moderator_role = helpers.MockRole(name="Moderators", id=2, position=10)
        self.flautist_role = helpers.MockRole(name="Flautists", id=3, position=2)
        self.bassist_role = helpers.MockRole(name="Bassists", id=4, position=3)

        self.author = helpers.MockMember(id=1, name="syntaxaire")
        self.moderator = helpers.MockMember(id=2, name="riffautae", roles=[self.moderator_role])
        self.target = helpers.MockMember(id=3, name="__fluzz__")
Esempio n. 8
0
    def setUp(self):
        patcher = mock.patch("bot.instance", new=helpers.MockBot(user=helpers.MockMember(bot=True)))
        self.bot = patcher.start()
        self.addCleanup(patcher.stop)

        self.guild = helpers.MockGuild()

        TestSyncer._get_diff.reset_mock(return_value=True, side_effect=True)
        TestSyncer._sync.reset_mock(return_value=True, side_effect=True)

        # Make sure `_get_diff` returns a MagicMock, not an AsyncMock
        TestSyncer._get_diff.return_value = mock.MagicMock()
Esempio n. 9
0
    def test_setup(self):
        """Setup of the cog should log a message at `INFO` level."""
        bot = helpers.MockBot()
        log = logging.getLogger('bot.cogs.duck_pond')

        with self.assertLogs(logger=log, level=logging.INFO) as log_watcher:
            duck_pond.setup(bot)

        self.assertEqual(len(log_watcher.records), 1)
        record = log_watcher.records[0]
        self.assertEqual(record.levelno, logging.INFO)

        bot.add_cog.assert_called_once()
Esempio n. 10
0
    def test_mocks_rejects_access_to_attributes_not_part_of_spec(self):
        """Accessing attributes that are invalid for the objects they mock should fail."""
        mocks = (
            helpers.MockGuild(),
            helpers.MockRole(),
            helpers.MockMember(),
            helpers.MockBot(),
            helpers.MockContext(),
            helpers.MockTextChannel(),
            helpers.MockMessage(),
        )

        for mock in mocks:
            with self.subTest(mock=mock):
                with self.assertRaises(AttributeError):
                    mock.the_cake_is_a_lie
Esempio n. 11
0
    def setUp(self):
        self.bot = helpers.MockBot()

        # These patch the type. When the type is called, a MockSyncer instanced is returned.
        # MockSyncer is needed so that our custom AsyncMock is used.
        # TODO: Use autospec instead in 3.8, which will automatically use AsyncMock when needed.
        self.role_syncer_patcher = mock.patch(
            "bot.cogs.sync.syncers.RoleSyncer",
            new=mock.MagicMock(return_value=MockSyncer()))
        self.user_syncer_patcher = mock.patch(
            "bot.cogs.sync.syncers.UserSyncer",
            new=mock.MagicMock(return_value=MockSyncer()))
        self.RoleSyncer = self.role_syncer_patcher.start()
        self.UserSyncer = self.user_syncer_patcher.start()

        self.cog = sync.Sync(self.bot)
Esempio n. 12
0
    def setUp(self):
        """Set up steps executed before each test is run."""
        self.bot = helpers.MockBot()
        self.cog = information.Information(self.bot)

        self.moderator_role = helpers.MockRole(name="Moderators", id=2, position=10)
        self.flautist_role = helpers.MockRole(name="Flautists", id=3, position=2)
        self.bassist_role = helpers.MockRole(name="Bassists", id=4, position=3)

        self.author = helpers.MockMember(id=1, name="syntaxaire")
        self.moderator = helpers.MockMember(id=2, name="riffautae", roles=[self.moderator_role])
        self.target = helpers.MockMember(id=3, name="__fluzz__")

        # There's no way to mock the channel constant without deferring imports. The constant is
        # used as a default value for a parameter, which gets defined upon import.
        self.bot_command_channel = helpers.MockTextChannel(id=constants.Channels.bot_commands)
Esempio n. 13
0
    def test_mocks_allows_access_to_attributes_part_of_spec(self):
        """Accessing attributes that are valid for the objects they mock should succeed."""
        mocks = (
            (helpers.MockGuild(), 'name'),
            (helpers.MockRole(), 'hoist'),
            (helpers.MockMember(), 'display_name'),
            (helpers.MockBot(), 'user'),
            (helpers.MockContext(), 'invoked_with'),
            (helpers.MockTextChannel(), 'last_message'),
            (helpers.MockMessage(), 'mention_everyone'),
        )

        for mock, valid_attribute in mocks:
            with self.subTest(mock=mock):
                try:
                    getattr(mock, valid_attribute)
                except AttributeError:
                    msg = f"accessing valid attribute `{valid_attribute}` raised an AttributeError"
                    self.fail(msg)
Esempio n. 14
0
    def setUp(self):
        self.bot = helpers.MockBot()

        role_syncer_patcher = mock.patch(
            "bot.exts.backend.sync._syncers.RoleSyncer",
            autospec=Syncer,
            spec_set=True)
        user_syncer_patcher = mock.patch(
            "bot.exts.backend.sync._syncers.UserSyncer",
            autospec=Syncer,
            spec_set=True)

        self.RoleSyncer = role_syncer_patcher.start()
        self.UserSyncer = user_syncer_patcher.start()

        self.addCleanup(role_syncer_patcher.stop)
        self.addCleanup(user_syncer_patcher.stop)

        self.cog = Sync(self.bot)
Esempio n. 15
0
    async def asyncSetUp(self):  # noqa: N802
        """Sets up the objects that only have to be initialized once."""
        self.bot = helpers.MockBot()
        self.bot.redis_session = await fakeredis.aioredis.create_redis_pool()

        # Okay, so this is necessary so that we can create a clean new
        # class for every test method, and we want that because it will
        # ensure we get a fresh loop, which is necessary for test_increment_lock
        # to be able to pass.
        class DummyCog:
            """A dummy cog, for dummies."""

            redis = RedisCache()

            def __init__(self, bot: helpers.MockBot):
                self.bot = bot

        self.cog = DummyCog(self.bot)

        await self.cog.redis.clear()
Esempio n. 16
0
 def setUp(self):
     self.bot = helpers.MockBot()
     self.syncer = TestSyncer(self.bot)
     self.core_dev_role = helpers.MockRole(
         id=constants.Roles.core_developers)
Esempio n. 17
0
    def test_mock_bot_default_initialization(self):
        """Tests if MockBot initializes with the correct values."""
        bot = helpers.MockBot()

        # The `spec` argument makes sure `isistance` checks with `discord.ext.commands.Bot` pass
        self.assertIsInstance(bot, discord.ext.commands.Bot)
Esempio n. 18
0
 def test_extension_setup():
     """The Sync cog should be added."""
     bot = helpers.MockBot()
     sync.setup(bot)
     bot.add_cog.assert_called_once()
Esempio n. 19
0
 def setUp(self):
     self.bot = helpers.MockBot(user=helpers.MockMember(bot=True))
     self.syncer = TestSyncer(self.bot)
Esempio n. 20
0
import asyncio
import re

from aiohttp import ClientSession

from bot import Bot
import tests.helpers as helpers
from cogs.animals import animals
from cogs.misc import misc
from cogs.useful import useful
from cogs.stocks import stocks
from cogs.crypto import crypto
from cogs.apis import apis


bot = Bot(helpers.MockBot())
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())


url_regex = re.compile(
    r"^(?:http)s?://(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}"
    r"[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?))",
    re.IGNORECASE,
)


class AdminCogTests(unittest.IsolatedAsyncioTestCase):
    pass


class AnimalsCogTests(unittest.IsolatedAsyncioTestCase):
Esempio n. 21
0
 def setUp(self):
     """Common set-up steps done before for each test."""
     self.bot = helpers.MockBot()
     self.bot.api_client.get = helpers.AsyncMock()
     self.cog = information.Information(self.bot)
     self.member = helpers.MockMember(user_id=1234)
Esempio n. 22
0
 def setUp(self):
     """Sets up the objects that need to be refreshed before each test."""
     self.bot = helpers.MockBot(user=helpers.MockMember(id=46692))
     self.cog = duck_pond.DuckPond(bot=self.bot)
Esempio n. 23
0
 def setUp(self):
     patcher = mock.patch("bot.instance", new=helpers.MockBot())
     self.bot = patcher.start()
     self.addCleanup(patcher.stop)
Esempio n. 24
0
 def setUp(self):
     """Common set-up steps done before for each test."""
     self.bot = helpers.MockBot()
     self.bot.api_client.get = unittest.mock.AsyncMock()
     self.cog = information.Information(self.bot)
Esempio n. 25
0
 def setUp(self):
     self.bot = helpers.MockBot()
Esempio n. 26
0
 def setUp(self):
     self.bot = helpers.MockBot()
     self.syncer = UserSyncer(self.bot)
Esempio n. 27
0
 def test_setup(self):
     """Setup of the extension should call add_cog."""
     bot = helpers.MockBot()
     duck_pond.setup(bot)
     bot.add_cog.assert_called_once()