def setUp(self): """ Simply setup our Guild obj before usage """ self.guild = Guild(None, 15, Static.DEFAULTS) self.guild.users = User(None, 20, 15, Static.DEFAULTS) self.guild.users = User(None, 21, 15, Static.DEFAULTS)
async def propagate(self, message: discord.Message): """ This method handles a message object and then adds it to the relevant member Parameters ========== message : discord.Message The message that needs to be propagated out Returns ======= dict A dictionary of useful information about the user in question """ if not isinstance(message, discord.Message) and not isinstance( message, MagicMock ): raise ValueError("Expected message of ignore_type: discord.Message") user = User(self._bot, message.author.id, message.guild.id, self.options,) try: user = next(iter(u for u in self._users if u == user)) except StopIteration: self.users = user logging.info(f"Created User: {user.id}") return await user.propagate(message)
async def asyncSetUp(self): """ Simply setup our User obj before usage """ self.user = User(None, 0, 3, Static.DEFAULTS) self.user.messages = Message(0, "Hello world", 0, 2, 3) self.user.messages = Message(1, "Foo Bar", 0, 2, 3)
def test_userRaisesDuplicate(self): with self.assertRaises(DuplicateObject): self.guild.users = User(None, 21, 15, Static.DEFAULTS, logger=logging.getLogger(__name__))
def test_messageRaisesMismatch(self): with self.assertRaises(ObjectMismatch): self.guild.users = User(None, 22, 16, Static.DEFAULTS, logger=logging.getLogger(__name__))
def test_userAssignment(self): self.assertEqual(len(self.guild.users), 2) self.guild.users = User(None, 22, 15, Static.DEFAULTS, logger=logging.getLogger(__name__)) self.assertEqual(len(self.guild.users), 3)
def setUp(self): """ Simply setup our Guild obj before usage """ self.guild = Guild(None, 15, Static.DEFAULTS, logger=logging.getLogger(__name__)) self.guild.users = User(None, 20, 15, Static.DEFAULTS, logger=logging.getLogger(__name__)) self.guild.users = User(None, 21, 15, Static.DEFAULTS, logger=logging.getLogger(__name__))
def setUp(self): """ Simply setup our User obj before usage """ self.user = User( None, 0, 3, Static.DEFAULTS, logger=logging.getLogger(__name__) ) self.user.messages = Message(0, "Hello world", 0, 2, 3) self.user.messages = Message(1, "Foo Bar", 0, 2, 3)
def reset_user_count(self, user_id: int, guild_id: int, counter: str) -> None: """ Reset an internal counter attached to a User object Parameters ---------- user_id : int The user to reset guild_id : int The guild they are attached to counter : str A str denoting which count to reset, Options are:\n ``warn_counter`` -> Reset the warn count\n ``kick_counter`` -> Reset the kick count Raises ====== LogicError Invalid count to reset Notes ===== Silently ignores if the User or Guild does not exist. This is because in the packages mind, the counts are 'reset' since the default value is the reset value. """ guild = Guild(self.bot, guild_id, self.options) try: guild = next(iter(g for g in self.guilds if g == guild)) except StopIteration: return user = User(self.bot, user_id, guild_id=guild_id, options=guild.options) try: user = next(iter(u for u in guild.users if u == user)) except StopIteration: return if counter.lower() == Static.WARNCOUNTER: user.warn_count = 0 log.debug(f"Reset the warn count for user: {user_id}") elif counter.lower() == Static.KICKCOUNTER: user.kick_count = 0 log.debug(f"Reset the kick count for user: {user_id}") else: raise LogicError( "Invalid counter argument, please select a valid counter.")
async def test_resetKickCounter(self): # GIVEN / SETUP user = User(get_mocked_bot(), 123454321, 12, Static.DEFAULTS) self.ash.guilds[0].users = user self.assertEqual(1, len(self.ash.guilds[0].users)) self.assertEqual(0, self.ash.guilds[0].users[0].kick_count) self.ash.guilds[0].users[0].kick_count += 1 self.assertEqual(1, self.ash.guilds[0].users[0].kick_count) # WHEN / TESTING self.ash.reset_user_count(123454321, 12, Static.KICKCOUNTER) # THEN / ASSERTIONS self.assertEqual(0, self.ash.guilds[0].users[0].kick_count)
async def test_resetWarnCounter(self): # GIVEN / SETUP user = User( MockedMember(mock_type="bot").to_mock(), 123454321, 12, Static.DEFAULTS) self.ash.guilds[0].users = user self.assertEqual(1, len(self.ash.guilds[0].users)) self.assertEqual(0, self.ash.guilds[0].users[0].warn_count) self.ash.guilds[0].users[0].warn_count += 1 self.assertEqual(1, self.ash.guilds[0].users[0].warn_count) # WHEN / TESTING self.ash.reset_user_count(123454321, 12, Static.WARNCOUNTER) # THEN / ASSERTIONS self.assertEqual(0, self.ash.guilds[0].users[0].warn_count)
async def test_resetCountersRaises(self): # SETUP user = User(get_mocked_bot(), 123454321, 12, Static.DEFAULTS) self.ash.guilds[0].users = user # ASSERTIONS / TESTING with self.assertRaises(LogicError): self.ash.reset_user_count(123454321, 12, "Who knows") # Invalid guild, should work silently self.ash.reset_user_count(123454321, 15, "Who knows") # Invalid user, should work silently self.ash.reset_user_count(1234, 12, "Who knows") # Invalid both, should work silently self.ash.reset_user_count(1234, 15, "Who knows")
async def test_multiChannelSpam(self): options = deepcopy(Static.DEFAULTS) options["per_channel_spam"] = True user = User(None, 0, 3, options) for i in range(3): m = MockedMessage(message_id=i, author_id=0, guild_id=3).to_mock() m.channel = MockedChannel(channel_id=1).to_mock() await user.propagate(m) self.assertEqual(user.get_correct_duplicate_count(1), 3) message = MockedMessage(author_id=0, guild_id=3).to_mock() message.channel = MockedChannel(channel_id=2).to_mock() self.assertEqual(user.get_correct_duplicate_count(2), 0) for i in range(5): m = MockedMessage(message_id=i, author_id=0, guild_id=3).to_mock() m.channel = MockedChannel(channel_id=2).to_mock() await user.propagate(m) self.assertEqual(user.get_correct_duplicate_count(1), 3) self.assertEqual(user.get_correct_duplicate_count(2), 5)
def test_eqEqual(self): obj = User(None, 0, 3, Static.DEFAULTS, logger=logging.getLogger(__name__)) self.assertTrue(self.user == obj)
def test_eqNotEqual(self): obj = User(None, 2, 2, Static.DEFAULTS, logger=logging.getLogger(__name__)) self.assertFalse(self.user == obj)
def test_userAssignment(self): self.assertEqual(len(self.guild.users), 2) self.guild.users = User(None, 22, 15, Static.DEFAULTS) self.assertEqual(len(self.guild.users), 3)
def test_userRaisesDuplicate(self): with self.assertRaises(DuplicateObject): self.guild.users = User(None, 21, 15, Static.DEFAULTS)
async def test_eqNotEqual(self): obj = User(None, 2, 2, Static.DEFAULTS) self.assertFalse(self.user == obj)
async def test_eqEqual(self): obj = User(None, 0, 3, Static.DEFAULTS) self.assertTrue(self.user == obj)
def test_messageRaisesMismatch(self): with self.assertRaises(ObjectMismatch): self.guild.users = User(None, 22, 16, Static.DEFAULTS)