Exemple #1
0
    def test_duration_converter_for_invalid(self):
        """Duration raises the right exception for invalid duration strings."""
        test_values = (
            # Units in wrong order
            ('1d1w'),
            ('1s1y'),

            # Duplicated units
            ('1 year 2 years'),
            ('1 M 10 minutes'),

            # Unknown substrings
            ('1MVes'),
            ('1y3breads'),

            # Missing amount
            ('ym'),

            # Incorrect whitespace
            (" 1y"),
            ("1S "),
            ("1y  1m"),

            # Garbage
            ('Guido van Rossum'),
            ('lemon lemon lemon lemon lemon lemon lemon'),
        )

        converter = Duration()

        for invalid_duration in test_values:
            with self.subTest(invalid_duration=invalid_duration):
                exception_message = f'`{invalid_duration}` is not a valid duration string.'
                with self.assertRaises(BadArgument, msg=exception_message):
                    asyncio.run(converter.convert(self.context, invalid_duration))
Exemple #2
0
    def __init__(self, bot: Bot, validation_errors: bool) -> None:
        self.bot = bot
        self.validation_errors = validation_errors
        role_id = AntiSpamConfig.punishment['role_id']
        self.muted_role = Object(role_id)
        self.expiration_date_converter = Duration()

        self.message_deletion_queue = dict()
        self.queue_consumption_tasks = dict()
Exemple #3
0
    def __init__(self, bot: Bot, validation_errors: Dict[str, str]) -> None:
        self.bot = bot
        self.validation_errors = validation_errors
        role_id = AntiSpamConfig.punishment['role_id']
        self.muted_role = Object(role_id)
        self.expiration_date_converter = Duration()

        self.message_deletion_queue = dict()

        self.bot.loop.create_task(self.alert_on_validation_error())
Exemple #4
0
    def __init__(self, bot: Bot, validation_errors: Dict[str, str]) -> None:
        self.bot = bot
        self.validation_errors = validation_errors
        role_id = AntiSpamConfig.punishment['role_id']
        self.muted_role = Object(role_id)
        self.expiration_date_converter = Duration()

        self.message_deletion_queue = dict()

        # Fetch the rule configuration with the highest rule interval.
        max_interval_config = max(
            AntiSpamConfig.rules.values(),
            key=itemgetter('interval')
        )
        self.max_interval = max_interval_config['interval']
        self.cache = MessageCache(AntiSpamConfig.cache_size, newest_first=True)

        self.bot.loop.create_task(self.alert_on_validation_error(), name="AntiSpam.alert_on_validation_error")
Exemple #5
0
    async def test_duration_converter_for_invalid(self):
        """Duration raises the right exception for invalid duration strings."""
        test_values = (
            # Units in wrong order
            '1d1w',
            '1s1y',

            # Duplicated units
            '1 year 2 years',
            '1 M 10 minutes',

            # Unknown substrings
            '1MVes',
            '1y3breads',

            # Missing amount
            'ym',

            # Incorrect whitespace
            " 1y",
            "1S ",
            "1y  1m",

            # Garbage
            'Guido van Rossum',
            'lemon lemon lemon lemon lemon lemon lemon',
        )

        converter = Duration()

        for invalid_duration in test_values:
            with self.subTest(invalid_duration=invalid_duration):
                exception_message = f'`{invalid_duration}` is not a valid duration string.'
                with self.assertRaisesRegex(BadArgument,
                                            re.escape(exception_message)):
                    await converter.convert(self.context, invalid_duration)
Exemple #6
0
    def test_duration_converter_for_valid(self):
        """Duration returns the correct `datetime` for valid duration strings."""
        test_values = (
            # Simple duration strings
            ('1Y', {"years": 1}),
            ('1y', {"years": 1}),
            ('1year', {"years": 1}),
            ('1years', {"years": 1}),
            ('1m', {"months": 1}),
            ('1month', {"months": 1}),
            ('1months', {"months": 1}),
            ('1w', {"weeks": 1}),
            ('1W', {"weeks": 1}),
            ('1week', {"weeks": 1}),
            ('1weeks', {"weeks": 1}),
            ('1d', {"days": 1}),
            ('1D', {"days": 1}),
            ('1day', {"days": 1}),
            ('1days', {"days": 1}),
            ('1h', {"hours": 1}),
            ('1H', {"hours": 1}),
            ('1hour', {"hours": 1}),
            ('1hours', {"hours": 1}),
            ('1M', {"minutes": 1}),
            ('1minute', {"minutes": 1}),
            ('1minutes', {"minutes": 1}),
            ('1s', {"seconds": 1}),
            ('1S', {"seconds": 1}),
            ('1second', {"seconds": 1}),
            ('1seconds', {"seconds": 1}),

            # Complex duration strings
            (
                '1y1m1w1d1H1M1S',
                {
                    "years": 1,
                    "months": 1,
                    "weeks": 1,
                    "days": 1,
                    "hours": 1,
                    "minutes": 1,
                    "seconds": 1
                }
            ),
            ('5y100S', {"years": 5, "seconds": 100}),
            ('2w28H', {"weeks": 2, "hours": 28}),

            # Duration strings with spaces
            ('1 year 2 months', {"years": 1, "months": 2}),
            ('1d 2H', {"days": 1, "hours": 2}),
            ('1 week2 days', {"weeks": 1, "days": 2}),
        )

        converter = Duration()

        for duration, duration_dict in test_values:
            expected_datetime = self.fixed_utc_now + relativedelta(**duration_dict)

            with patch('bot.converters.datetime') as mock_datetime:
                mock_datetime.utcnow.return_value = self.fixed_utc_now

                with self.subTest(duration=duration, duration_dict=duration_dict):
                    converted_datetime = asyncio.run(converter.convert(self.context, duration))
                    self.assertEqual(converted_datetime, expected_datetime)
Exemple #7
0
def test_duration_converter_for_invalid(duration: str):
    converter = Duration()
    with pytest.raises(BadArgument,
                       match=f'`{duration}` is not a valid duration string.'):
        asyncio.run(converter.convert(None, duration))
Exemple #8
0
def test_duration_converter_for_valid(create_future_datetime: tuple):
    converter = Duration()
    duration, expected = create_future_datetime
    with patch('bot.converters.datetime') as mock_datetime:
        mock_datetime.utcnow.return_value = FIXED_UTC_NOW
        assert asyncio.run(converter.convert(None, duration)) == expected