Example #1
0
    def test_get(self):
        with patch('task_dispatcher.decorators.app') as celery_app_mock, \
                patch('task_dispatcher.decorators.register') as register_mock:
            celery_app_mock.task().return_value = self.task_mock

            decorator = BaseDecorator(self.task_mock)

        self.assertTrue(isinstance(decorator.__get__(decorator),
                                   BaseDecorator))
        self.assertEqual(decorator.instance, decorator)
Example #2
0
    def test_getattr_task_method(self):
        with patch('task_dispatcher.decorators.app') as celery_app_mock, \
                patch('task_dispatcher.decorators.register') as register_mock:
            celery_app_mock.task().return_value = self.task_mock

            decorator = BaseDecorator(description='description')(
                self.task_mock)

        name = decorator.foo_method('bar')
        expected_name = 'bar'

        self.assertEqual(expected_name, name)
Example #3
0
    def test_call_not_initialized(self):
        with patch('task_dispatcher.decorators.app') as celery_app_mock, \
                patch('task_dispatcher.decorators.register') as register_mock:
            # Partial initialization to pass description
            celery_app_mock.task().return_value = self.task_mock
            decorator = BaseDecorator(description='description')

            # Try to call before full initialization
            self.assertRaises(ValueError, decorator, 'foo', bar='bar')
Example #4
0
    def test_partial_initialize_with_args(self):
        with patch.object(BaseDecorator, '_decorate') as decorate_mock:
            # Partial initialization to pass description
            decorator = BaseDecorator(description='description')

            self.assertEqual(decorate_mock.call_count, 0)

            # Full initialization through __call__
            decorator(self.task_mock)

            self.assertEqual(decorate_mock.call_count, 1)
Example #5
0
    def test_getattr_decorator_attr(self):
        with patch('task_dispatcher.decorators.app') as celery_app_mock, \
                patch('task_dispatcher.decorators.register') as register_mock:
            celery_app_mock.task().return_value = self.task_mock

            decorator = BaseDecorator(description='description')(
                self.task_mock)

        description = decorator.description
        expected_description = 'description'

        self.assertEqual(expected_description, description)
Example #6
0
    def test_decorate(self):
        expected_task_name = '.'.join(
            [self.task_mock.__module__, self.task_mock.__qualname__])

        with patch('task_dispatcher.decorators.app') as celery_app_mock, \
                patch('task_dispatcher.decorators.register') as register_mock:
            celery_app_mock.task().return_value = self.task_mock

            decorator = BaseDecorator(self.task_mock)

            self.assertEqual(self.task_mock, decorator.task)
            self.assertEqual(register_mock.register.call_count, 1)
            self.assertIn('name', celery_app_mock.task.call_args[1])
            self.assertEqual(celery_app_mock.task.call_args[1]['name'],
                             expected_task_name)
Example #7
0
    def test_call(self):
        expected_call_args = [call('foo', bar='bar')]

        with patch('task_dispatcher.decorators.app') as celery_app_mock, \
                patch('task_dispatcher.decorators.register') as register_mock:
            # Partial initialization to pass description
            celery_app_mock.task().return_value = self.task_mock
            decorator = BaseDecorator(description='description')(
                self.task_mock)

            # Full initialization through __call__
            decorator('foo', bar='bar')

            self.assertEqual(self.task_mock, decorator.task)
            self.assertEqual(self.task_mock.call_count, 1)
            self.assertCountEqual(expected_call_args,
                                  self.task_mock.call_args_list)
Example #8
0
 def test_full_initialize_without_args(self):
     with patch.object(BaseDecorator, '_decorate') as decorate_mock:
         BaseDecorator(self.task_mock)
         self.assertEqual(decorate_mock.call_count, 1)
Example #9
0
    def test_getattr_without_task(self):
        decorator = BaseDecorator()

        with self.assertRaises(AttributeError):
            _ = getattr(decorator, 'foo')