aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/bot/exts/backend/sync/test_cog.py61
-rw-r--r--tests/bot/exts/backend/sync/test_users.py1
2 files changed, 38 insertions, 24 deletions
diff --git a/tests/bot/exts/backend/sync/test_cog.py b/tests/bot/exts/backend/sync/test_cog.py
index 2ce950965..bf117b478 100644
--- a/tests/bot/exts/backend/sync/test_cog.py
+++ b/tests/bot/exts/backend/sync/test_cog.py
@@ -1,4 +1,5 @@
import unittest
+import unittest.mock
from unittest import mock
import discord
@@ -60,40 +61,52 @@ class SyncCogTestCase(unittest.IsolatedAsyncioTestCase):
class SyncCogTests(SyncCogTestCase):
"""Tests for the Sync cog."""
- async def test_sync_cog_sync_on_load(self):
- """Roles and users should be synced on cog load."""
- guild = helpers.MockGuild()
- self.bot.get_guild = mock.MagicMock(return_value=guild)
-
- self.RoleSyncer.reset_mock()
- self.UserSyncer.reset_mock()
-
- await self.cog.cog_load()
-
- self.RoleSyncer.sync.assert_called_once_with(guild)
- self.UserSyncer.sync.assert_called_once_with(guild)
-
- async def test_sync_cog_sync_guild(self):
- """Roles and users should be synced only if a guild is successfully retrieved."""
+ @unittest.mock.patch("bot.exts.backend.sync._cog.create_task", new_callable=unittest.mock.MagicMock)
+ async def test_sync_cog_sync_on_load(self, mock_create_task: unittest.mock.MagicMock):
+ """Sync function should be synced on cog load only if guild is found."""
for guild in (helpers.MockGuild(), None):
with self.subTest(guild=guild):
+ mock_create_task.reset_mock()
self.bot.reset_mock()
self.RoleSyncer.reset_mock()
self.UserSyncer.reset_mock()
self.bot.get_guild = mock.MagicMock(return_value=guild)
-
- await self.cog.cog_load()
-
- self.bot.wait_until_guild_available.assert_called_once()
- self.bot.get_guild.assert_called_once_with(constants.Guild.id)
+ error_raised = False
+ try:
+ await self.cog.cog_load()
+ except ValueError:
+ if guild is None:
+ error_raised = True
+ else:
+ raise
if guild is None:
- self.RoleSyncer.sync.assert_not_called()
- self.UserSyncer.sync.assert_not_called()
+ self.assertTrue(error_raised)
+ mock_create_task.assert_not_called()
else:
- self.RoleSyncer.sync.assert_called_once_with(guild)
- self.UserSyncer.sync.assert_called_once_with(guild)
+ mock_create_task.assert_called_once()
+ self.assertIsInstance(mock_create_task.call_args[0][0], type(self.cog.sync()))
+
+
+ async def test_sync_cog_sync_guild(self):
+ """Roles and users should be synced only if a guild is successfully retrieved."""
+ guild = helpers.MockGuild()
+ self.bot.reset_mock()
+ self.RoleSyncer.reset_mock()
+ self.UserSyncer.reset_mock()
+
+ self.bot.get_guild = mock.MagicMock(return_value=guild)
+ await self.cog.cog_load()
+
+ with mock.patch("asyncio.sleep", new_callable=unittest.mock.AsyncMock):
+ await self.cog.sync()
+
+ self.bot.wait_until_guild_available.assert_called_once()
+ self.bot.get_guild.assert_called_once_with(constants.Guild.id)
+
+ self.RoleSyncer.sync.assert_called_once()
+ self.UserSyncer.sync.assert_called_once()
async def patch_user_helper(self, side_effect: BaseException) -> None:
"""Helper to set a side effect for bot.api_client.patch and then assert it is called."""
diff --git a/tests/bot/exts/backend/sync/test_users.py b/tests/bot/exts/backend/sync/test_users.py
index 2fc97af2d..2fc000446 100644
--- a/tests/bot/exts/backend/sync/test_users.py
+++ b/tests/bot/exts/backend/sync/test_users.py
@@ -11,6 +11,7 @@ def fake_user(**kwargs):
"""Fixture to return a dictionary representing a user with default values set."""
kwargs.setdefault("id", 43)
kwargs.setdefault("name", "bob the test man")
+ kwargs.setdefault("display_name", "bob")
kwargs.setdefault("discriminator", 1337)
kwargs.setdefault("roles", [helpers.MockRole(id=666)])
kwargs.setdefault("in_guild", True)