async def test_sync_cog_on_user_update(self): """A user should be patched only if the name, discriminator, or avatar changes.""" self.assertTrue(self.cog.on_user_update.__cog_listener__) before_data = { "name": "old name", "discriminator": "1234", "bot": False, } subtests = ( (True, "name", "name", "new name", "new name"), (True, "discriminator", "discriminator", "8765", 8765), (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) await 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.args[0], after_user.id) self.assertIn("json", call_args.kwargs) self.assertIn("ignore_404", call_args.kwargs) self.assertTrue(call_args.kwargs["ignore_404"]) json = call_args.kwargs["json"] self.assertIn(api_field, json) self.assertEqual(json[api_field], api_value) else: self.cog.patch_user.assert_not_called()
def test_is_staff_returns_correct_values_based_on_instance_passed(self): """The `is_staff` method should return correct values based on the instance passed.""" test_cases = ( (helpers.MockUser(name="User instance"), False), (helpers.MockMember(name="Member instance without staff role"), False), (helpers.MockMember(name="Member instance with staff role", roles=[self.staff_role]), True) ) for user, expected_return in test_cases: actual_return = self.cog.is_staff(user) with self.subTest(user_type=user.name, expected_return=expected_return, actual_return=actual_return): self.assertEqual(expected_return, actual_return)
def test_user_mock_uses_explicitly_passed_mention_attribute(self): """MockUser should use an explicitly passed value for user.mention.""" user = helpers.MockUser(mention="hello") self.assertEqual(user.mention, "hello")