def test_until_expiration_normal_usage(self): """until_expiration should work for normal usage, across various durations.""" test_cases = ( ('2019-12-12T00:01:00Z', datetime(2019, 12, 11, 12, 0, 5), 2, '12 hours and 55 seconds'), ('2019-12-12T00:01:00Z', datetime(2019, 12, 11, 12, 0, 5), 1, '12 hours'), ('2019-12-12T00:00:00Z', datetime(2019, 12, 11, 23, 59), 2, '1 minute'), ('2019-11-23T20:09:00Z', datetime(2019, 11, 15, 20, 15), 2, '7 days and 23 hours'), ('2019-11-23T20:09:00Z', datetime(2019, 4, 25, 20, 15), 2, '6 months and 28 days'), ('2019-11-23T20:58:00Z', datetime(2019, 11, 23, 20, 53), 2, '5 minutes'), ('2019-11-24T00:00:00Z', datetime(2019, 11, 23, 23, 59, 0), 2, '1 minute'), ('2019-11-23T23:59:00Z', datetime(2017, 7, 21, 23, 0), 2, '2 years and 4 months'), ('2019-11-23T23:59:00Z', datetime(2019, 11, 23, 23, 49, 5), 2, '9 minutes and 55 seconds'), (None, datetime(2019, 11, 23, 23, 49, 5), 2, None), ) for expiry, now, max_units, expected in test_cases: with self.subTest(expiry=expiry, now=now, max_units=max_units, expected=expected): self.assertEqual(time.until_expiration(expiry, now, max_units), expected)
def test_until_expiration_with_duration_custom_units(self): """until_expiration should work for custom max_units.""" test_cases = (('3000-12-12T00:01:00Z', '<t:32533488060:R>'), ('3000-11-23T20:09:00Z', '<t:32531918940:R>')) for expiry, expected in test_cases: with self.subTest(expiry=expiry, expected=expected): self.assertEqual(time.until_expiration(expiry, ), expected)
def test_until_expiration_with_duration_custom_units(self): """until_expiration should work for custom max_units.""" test_cases = ( ('2019-12-12T00:01:00Z', datetime(2019, 12, 11, 12, 5, 5), 6, '11 hours, 55 minutes and 55 seconds'), ('2019-11-23T20:09:00Z', datetime(2019, 4, 25, 20, 15), 20, '6 months, 28 days, 23 hours and 54 minutes') ) for expiry, now, max_units, expected in test_cases: with self.subTest(expiry=expiry, now=now, max_units=max_units, expected=expected): self.assertEqual(time.until_expiration(expiry, now, max_units), expected)
def test_until_expiration_normal_usage(self): """until_expiration should work for normal usage, across various durations.""" test_cases = ( ('3000-12-12T00:01:00Z', '<t:32533488060:R>'), ('3000-12-12T00:01:00Z', '<t:32533488060:R>'), ('3000-12-12T00:00:00Z', '<t:32533488000:R>'), ('3000-11-23T20:09:00Z', '<t:32531918940:R>'), ('3000-11-23T20:09:00Z', '<t:32531918940:R>'), (None, None), ) for expiry, expected in test_cases: with self.subTest(expiry=expiry, expected=expected): self.assertEqual(time.until_expiration(expiry), expected)
def test_until_expiration_with_duration_none_expiry(self): """until_expiration should work for None expiry.""" test_cases = ( (None, None, None, None), # To make sure that now and max_units are not touched (None, 'Why hello there!', None, None), (None, None, float('inf'), None), (None, 'Why hello there!', float('inf'), None), ) for expiry, now, max_units, expected in test_cases: with self.subTest(expiry=expiry, now=now, max_units=max_units, expected=expected): self.assertEqual(time.until_expiration(expiry, now, max_units), expected)
def infraction_to_string(self, infraction: utils.Infraction) -> str: """Convert the infraction object to a string representation.""" actor_id = infraction["actor"] guild = self.bot.get_guild(constants.Guild.id) actor = guild.get_member(actor_id) active = infraction["active"] user_id = infraction["user"] hidden = infraction["hidden"] created = time.format_infraction(infraction["inserted_at"]) if active: remaining = time.until_expiration( infraction["expires_at"]) or "Expired" else: remaining = "Inactive" if infraction["expires_at"] is None: expires = "*Permanent*" else: date_from = datetime.strptime(created, time.INFRACTION_FORMAT) expires = time.format_infraction_with_duration( infraction["expires_at"], date_from) lines = textwrap.dedent(f""" {"**===============**" if active else "==============="} Status: {"__**Active**__" if active else "Inactive"} User: {self.bot.get_user(user_id)} (`{user_id}`) Type: **{infraction["type"]}** Shadow: {hidden} Created: {created} Expires: {expires} Remaining: {remaining} Actor: {actor.mention if actor else actor_id} ID: `{infraction["id"]}` Reason: {infraction["reason"] or "*None*"} {"**===============**" if active else "==============="} """) return lines.strip()
active = infraction["active"] user = infraction["user"] expires_at = infraction["expires_at"] created = time.format_infraction(infraction["inserted_at"]) # Format the user string. if user_obj := self.bot.get_user(user["id"]): # The user is in the cache. user_str = messages.format_user(user_obj) else: # Use the user data retrieved from the DB. name = escape_markdown(user['name']) user_str = f"<@{user['id']}> ({name}#{user['discriminator']:04})" if active: remaining = time.until_expiration(expires_at) or "Expired" else: remaining = "Inactive" if expires_at is None: expires = "*Permanent*" else: date_from = datetime.strptime(created, time.INFRACTION_FORMAT) expires = time.format_infraction_with_duration(expires_at, date_from) lines = textwrap.dedent(f""" {"**===============**" if active else "==============="} Status: {"__**Active**__" if active else "Inactive"} User: {user_str} Type: **{infraction["type"]}** Shadow: {infraction["hidden"]}
def test_until_expiration_with_duration_none_expiry(self): """until_expiration should work for None expiry.""" self.assertEqual(time.until_expiration(None), None)