def test_utilities(self): with self.assertRaises(Exception): get_host_from_name('test') alt_settings = { 'queues': [{ 'name': 'test', 'host': 'amqp://*****:*****@localhost:5672/' }] } with override_settings(CARROT=alt_settings): get_host_from_name('test') with self.assertRaises(ImportError): validate_task('some.invalid.task') with self.assertRaises(AttributeError): validate_task('carrot.tests.invalid_function') validate_task(test_task) task = create_scheduled_task(test_task, {'days': 1}) self.assertTrue(isinstance(task, ScheduledTask)) with self.assertRaises(AttributeError): create_scheduled_task(test_task, None) decorate_class_view(MessageList, ['django.contrib.auth.decorators.login_required']) decorate_class_view(MessageList) decorate_function_view( failed_message_log_viewset, ['django.contrib.auth.decorators.login_required'])
def __init__(self, task: str, virtual_host: VirtualHost = None, queue: str = 'default', routing_key: str = None, exchange: str = '', priority: int = 0, task_args: tuple = (), task_kwargs: Union[str, dict] = None) -> None: if not task_kwargs or task_kwargs in ['{}', '"{}"']: task_kwargs = {} if not routing_key: routing_key = queue if not virtual_host: from carrot.utilities import get_host_from_name virtual_host = get_host_from_name(queue) assert isinstance(virtual_host, VirtualHost) self.uuid = str(uuid.uuid4()) self.virtual_host = virtual_host self.exchange = exchange self.queue = queue self.routing_key = routing_key self.priority = priority self.task = task self.task_args = task_args self.task_kwargs = task_kwargs self.formatter = DefaultMessageSerializer(self)
def test_utilities(self): with self.assertRaises(Exception): get_host_from_name('test') alt_settings = { 'queues': [{ 'name': 'test', 'host': 'amqp://*****:*****@localhost:5672/' }] } with override_settings(CARROT=alt_settings): get_host_from_name('test') with self.assertRaises(ImportError): validate_task('some.invalid.task') with self.assertRaises(AttributeError): validate_task('carrot.tests.invalid_function') validate_task(test_task) task = create_scheduled_task(task='carrot.tests.test_task', interval={'days': 1}) self.assertEqual(task.multiplier, 86400) task.interval_type = 'hours' self.assertEqual(task.multiplier, 3600) task.interval_type = 'minutes' self.assertEqual(task.multiplier, 60) task.interval_type = 'seconds' self.assertEqual(task.multiplier, 1) with mock.patch('carrot.utilities.publish_message'): task.publish() self.assertTrue(isinstance(task, ScheduledTask)) with self.assertRaises(AttributeError): create_scheduled_task(task='carrot.tests.test_task', interval=None) decorate_class_view(MessageList, ['django.contrib.auth.decorators.login_required']) decorate_class_view(MessageList) decorate_function_view( failed_message_log_viewset, ['django.contrib.auth.decorators.login_required'])
def virtual_host(self) -> Optional[str]: from carrot.utilities import get_host_from_name try: return str(get_host_from_name(self.queue)) except CarrotConfigException: """ This exception may get raised here when a MessageLog is created with a queue that is later removed from the Django config. This method now returns `None` in these cases, so as not to break the monitor Refer to https://github.com/chris104957/django-carrot/issues/81 """ return None