def test_mock_role_uses_position_for_less_than_greater_than(self): """Test if `<` and `>` comparisons for MockRole are based on its position attribute.""" role_one = mocks.MockRole(position=1) role_two = mocks.MockRole(position=2) role_three = mocks.MockRole(position=3) self.assertLess(role_one, role_two) self.assertLess(role_one, role_three) self.assertLess(role_two, role_three) self.assertGreater(role_three, role_two) self.assertGreater(role_three, role_one) self.assertGreater(role_two, role_one)
def test_mock_guild_alternative_arguments(self): """Test if MockGuild initializes with the arguments provided.""" core_developer = mocks.MockRole(name="Core Developer", position=2) guild = mocks.MockGuild( roles=[core_developer], members=[mocks.MockMember(id=54321)], ) self.assertListEqual(guild.roles, [ mocks.MockRole(name="@everyone", position=1, id=0), core_developer ]) self.assertListEqual(guild.members, [mocks.MockMember(id=54321)])
def test_mock_member_alternative_arguments(self): """Test if MockMember initializes with the arguments provided.""" core_developer = mocks.MockRole(name="Core Developer", position=2) member = mocks.MockMember(name="Mark", id=12345, roles=[core_developer]) self.assertEqual(member.name, "Mark") self.assertEqual(member.id, 12345) self.assertListEqual(member.roles, [ mocks.MockRole(name="@everyone", position=1, id=0), core_developer ]) self.assertEqual(member.mention, "<@!12345>")
async def test_flair(self): bot = mocks.MockSSB(cache=mocks.cache()) bob = mocks.MockMember(name='bob', id=1) with self.subTest('True Locked'): bob.roles = [mocks.MockRole(name=TRUE_LOCKED)] with self.assertRaises(ValueError) as ve: await flair(bob, mocks.HK, bot) self.assertEqual( str(ve.exception), f'{bob.display_name} cannot be flaired because they are {TRUE_LOCKED}.' ) with self.subTest('Join CD'): bob.roles = [mocks.MockRole(name=JOIN_CD)] with self.assertRaises(ValueError) as ve: await flair(bob, mocks.HK, bot) self.assertEqual( str(ve.exception), f'{bob.display_name} cannot be flaired because they have {JOIN_CD}.' ) with self.subTest('Free Agent non overflow.'): bob.roles = [bot.cache.roles.free_agent] await flair(bob, mocks.HK, bot) after = set(bob.roles) expected = {mocks.hk_role, bot.cache.roles.join_cd} self.assertEqual(expected, after) with self.subTest('Track 2 non overflow.'): bob.roles = [bot.cache.roles.track3] await flair(bob, mocks.HK, bot) after = set(bob.roles) expected = { mocks.hk_role, bot.cache.roles.join_cd, bot.cache.roles.true_locked } self.assertEqual(expected, after) with self.subTest('Overflow.'): overflow_bob = mocks.MockMember(name='bob', id=1) bob.roles = [bot.cache.roles.overflow] bot.cache.overflow_server.members = [overflow_bob] await flair(bob, mocks.Ballers, bot) after_main = set(bob.roles) expected_main = {bot.cache.roles.join_cd, bot.cache.roles.overflow} self.assertEqual(expected_main, after_main) after_overflow = set(overflow_bob.roles) self.assertIn(mocks.ballers_role, after_overflow)
def test_mock_role_accepts_dynamic_arguments(self): """Test if MockRole accepts and sets abitrary keyword arguments.""" role = mocks.MockRole( guild="Dino Man", hoist=True, ) self.assertEqual(role.guild, "Dino Man") self.assertTrue(role.hoist)
def test_mock_guild_default_initialization(self): """Test if the default initialization of Mockguild results in the correct object.""" guild = mocks.MockGuild() # The `spec` argument makes sure `isistance` checks with `discord.Guild` pass self.assertIsInstance(guild, discord.Guild) self.assertListEqual( guild.roles, [mocks.MockRole(name="@everyone", position=1, id=0)]) self.assertListEqual(guild.members, [])
def test_mock_role_default_initialization(self): """Test if the default initialization of MockRole results in the correct object.""" role = mocks.MockRole() # The `spec` argument makes sure `isistance` checks with `discord.Role` pass self.assertIsInstance(role, discord.Role) self.assertEqual(role.name, "role") self.assertEqual(role.position, 1) self.assertEqual(role.mention, "&role")
def test_mock_member_default_initialization(self): """Test if the default initialization of Mockmember results in the correct object.""" member = mocks.MockMember() # The `spec` argument makes sure `isistance` checks with `discord.Member` pass self.assertIsInstance(member, discord.Member) self.assertEqual(member.name, "member") self.assertListEqual( member.roles, [mocks.MockRole(name="@everyone", position=1, id=0)]) self.assertEqual(member.mention, f"<@!{member.id}>")
def test_mock_role_alternative_arguments(self): """Test if MockRole initializes with the arguments provided.""" role = mocks.MockRole( name="Admins", id=90210, position=10, ) self.assertEqual(role.name, "Admins") self.assertEqual(role.id, 90210) self.assertEqual(role.position, 10) self.assertEqual(role.mention, "&Admins")
def test_mocks_rejects_access_to_attributes_not_part_of_spec(self): """Accessing attributes that are invalid for the objects they mock should fail.""" mock_tuple = ( mocks.MockGuild(), mocks.MockRole(), mocks.MockMember(), mocks.MockBot(), mocks.MockContext(), mocks.MockTextChannel(), mocks.MockMessage(), ) for mock in mock_tuple: with self.subTest(mock=mock): with self.assertRaises(AttributeError): mock.the_cake_is_a_lie
def test_mocks_allows_access_to_attributes_part_of_spec(self): """Accessing attributes that are valid for the objects they mock should succeed.""" mock_tuple = ( (mocks.MockGuild(), 'name'), (mocks.MockRole(), 'hoist'), (mocks.MockMember(), 'display_name'), (mocks.MockBot(), 'user'), (mocks.MockContext(), 'invoked_with'), (mocks.MockTextChannel(), 'last_message'), (mocks.MockMessage(), 'mention_everyone'), ) for mock, valid_attribute in mock_tuple: 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)
def test_crew(self): member = mocks.MockMember(name='Steve', id=int('4' * 17)) hk = mocks.HK role = mocks.MockRole(name=hk.name) bot = mocks.MockSSB(cache=mocks.cache()) with self.subTest('Not on a crew.'): member.roles = [] with self.assertRaises(Exception): crew(member, bot) with self.subTest('On a crew.'): member.roles = [role] self.assertEqual(hk.name, crew(member, bot)) with self.subTest('On overflow crew.'): member.roles = [bot.cache.roles.overflow] of_member = mocks.MockMember(name='Steve', id=int('4' * 17), roles=[mocks.ballers_role]) bot.cache.overflow_server.members.append(of_member) self.assertEqual(mocks.Ballers.name, crew(member, bot))
def test_mock_class_with_hashable_mixin_uses_id_for_hashing(self): """Test if the MagicMock subclasses that implement the HashableMixin use id for hash.""" for mock in self.hashable_mocks: with self.subTest(mock_class=mock): instance = mocks.MockRole(id=100) self.assertEqual(hash(instance), instance.id)