diff options
author | 2020-02-05 13:14:33 -0800 | |
---|---|---|
committer | 2020-02-12 10:07:57 -0800 | |
commit | b3d19d72596052629f56823dfd6c63b42dda6253 (patch) | |
tree | dc82a1ac46588c36c4a94afcd0269de81a6c8f5d /tests | |
parent | Sync tests: fix ID in endpoint for test_sync_cog_on_member_remove (diff) |
Sync tests: test Sync cog's on_user_update
A user should be patched only if the name, discriminator, or avatar
changes.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/bot/cogs/sync/test_cog.py | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/tests/bot/cogs/sync/test_cog.py b/tests/bot/cogs/sync/test_cog.py index 75165a5b2..88c5e00b9 100644 --- a/tests/bot/cogs/sync/test_cog.py +++ b/tests/bot/cogs/sync/test_cog.py @@ -231,7 +231,7 @@ class SyncCogListenerTests(SyncCogTestCase): subtests = ( ("activities", discord.Game("Pong"), discord.Game("Frogger")), ("nick", "old nick", "new nick"), - ("status", discord.Status.online, discord.Status.offline) + ("status", discord.Status.online, discord.Status.offline), ) for attribute, old_value, new_value in subtests: @@ -244,3 +244,44 @@ class SyncCogListenerTests(SyncCogTestCase): asyncio.run(self.cog.on_member_update(before_member, after_member)) self.cog.patch_user.assert_not_called() + + def test_sync_cog_on_user_update(self): + """A user should be patched only if the name, discriminator, or avatar changes.""" + before_data = { + "name": "old name", + "discriminator": "1234", + "avatar": "old avatar", + "bot": False, + } + + subtests = ( + (True, "name", "name", "new name", "new name"), + (True, "discriminator", "discriminator", "8765", 8765), + (True, "avatar", "avatar_hash", "9j2e9", "9j2e9"), + (False, "bot", "bot", True, True), + ) + + for should_patch, attribute, api_field, value, api_value in subtests: + with self.subTest(attribute=attribute): + self.cog.patch_user.reset_mock() + + after_data = before_data.copy() + after_data[attribute] = value + before_user = helpers.MockUser(**before_data) + after_user = helpers.MockUser(**after_data) + + asyncio.run(self.cog.on_user_update(before_user, after_user)) + + if should_patch: + self.cog.patch_user.assert_called_once() + + # Don't care if *all* keys are present; only the changed one is required + call_args = self.cog.patch_user.call_args + self.assertEqual(call_args[0][0], after_user.id) + self.assertIn("updated_information", call_args[1]) + + updated_information = call_args[1]["updated_information"] + self.assertIn(api_field, updated_information) + self.assertEqual(updated_information[api_field], api_value) + else: + self.cog.patch_user.assert_not_called() |