# Path: tests/utils.py
# def skipIf(condition, message):
#     try:
#         from unittest import skipIf
#         return skipIf(condition, message)
#     except ImportError:  # skipIf/skip not implemented
#         if condition:
#             return lambda x: None
#         else:
#             return lambda x: x
# 
# Path: emit/router/celery.py
# class CeleryRouter(Router):
#     'Router specifically for Celery routing'
#     def __init__(self, celery_task, *args, **kwargs):
#         '''\
#         Specifically route when celery is needed
# 
#         :param celery_task: celery task to apply to all nodes (can be
#                             overridden in :py:meth:`Router.node`.)
#         :type celery_task: A celery task decorator, in any form
#         '''
#         super(CeleryRouter, self).__init__(*args, **kwargs)
#         self.celery_task = celery_task
#         self.logger.debug('Initialized Celery Router')
# 
#     def dispatch(self, origin, destination, message):
#         '''\
#         enqueue a message with Celery
# 
#         :param destination: destination to dispatch to
#         :type destination: :py:class:`str`
#         :param message: message to dispatch
#         :type message: :py:class:`emit.message.Message` or subclass
#         '''
#         func = self.functions[destination]
#         self.logger.debug('delaying %r', func)
#         return func.delay(_origin=origin, **message)
# 
#     def wrap_node(self, node, options):
#         '''\
#         celery registers tasks by decorating them, and so do we, so the user
#         can pass a celery task and we'll wrap our code with theirs in a nice
#         package celery can execute.
#         '''
#         if 'celery_task' in options:
#             return options['celery_task'](node)
# 
#         return self.celery_task(node)


import mock
from unittest import TestCase
from .utils import skipIf
from emit.router.celery import CeleryRouter
    from celery import Celery, Task
def prefix(name):
    return '%s.%s' % (__name__, name)


@skipIf(Celery is None, 'Celery did not import correctly')
class CeleryRouterTests(TestCase):
    'tests for using celery to route nodes'
    def setUp(self):
        self.celery = self.get_test_celery()
        self.router = CeleryRouter(self.celery.task)

    def get_test_celery(self):
        celery = Celery()
        celery.conf.update(
            CELERY_ALWAYS_EAGER=True
        )
        return celery

    def test_registers_as_task(self):
        'registers the function as a task'
        l = lambda x: x
        l.__name__ = 'test'
        self.router.node(['test'], celery_task=self.celery.task)(l)

        self.assertTrue(
            isinstance(self.router.functions[prefix('test')], Task)
        )

    def test_adds_when_initialized(self):
        'if router is passed a celery task when initialized it wraps with it'

# Path: tests/utils.py
# def skipIf(condition, message):
#     try:
#         from unittest import skipIf
#         return skipIf(condition, message)
#     except ImportError:  # skipIf/skip not implemented
#         if condition:
#             return lambda x: None
#         else:
#             return lambda x: x
# 
# Path: emit/router/celery.py
# class CeleryRouter(Router):
#     'Router specifically for Celery routing'
#     def __init__(self, celery_task, *args, **kwargs):
#         '''\
#         Specifically route when celery is needed
# 
#         :param celery_task: celery task to apply to all nodes (can be
#                             overridden in :py:meth:`Router.node`.)
#         :type celery_task: A celery task decorator, in any form
#         '''
#         super(CeleryRouter, self).__init__(*args, **kwargs)
#         self.celery_task = celery_task
#         self.logger.debug('Initialized Celery Router')
# 
#     def dispatch(self, origin, destination, message):
#         '''\
#         enqueue a message with Celery
# 
#         :param destination: destination to dispatch to
#         :type destination: :py:class:`str`
#         :param message: message to dispatch
#         :type message: :py:class:`emit.message.Message` or subclass
#         '''
#         func = self.functions[destination]
#         self.logger.debug('delaying %r', func)
#         return func.delay(_origin=origin, **message)
# 
#     def wrap_node(self, node, options):
#         '''\
#         celery registers tasks by decorating them, and so do we, so the user
#         can pass a celery task and we'll wrap our code with theirs in a nice
#         package celery can execute.
#         '''
#         if 'celery_task' in options:
#             return options['celery_task'](node)
# 
#         return self.celery_task(node)


# Path: tests/router_celery_tests.py
import mock
from unittest import TestCase
from .utils import skipIf
from emit.router.celery import CeleryRouter
    from celery import Celery, Task

def prefix(name):
    return '%s.%s' % (__name__, name)


@skipIf(Celery is None, 'Celery did not import correctly')
class CeleryRouterTests(TestCase):
    'tests for using celery to route nodes'
    def setUp(self):
        self.celery = self.get_test_celery()
        self.router = CeleryRouter(self.celery.task)

    def get_test_celery(self):
        celery = Celery()
        celery.conf.update(
            CELERY_ALWAYS_EAGER=True
        )
        return celery

    def test_registers_as_task(self):
        'registers the function as a task'
        l = lambda x: x
        l.__name__ = 'test'
        self.router.node(['test'], celery_task=self.celery.task)(l)

        self.assertTrue(
            isinstance(self.router.functions[prefix('test')], Task)
        )

    def test_adds_when_initialized(self):
        'if router is passed a celery task when initialized it wraps with it'

        r = CeleryRouter(celery_task=self.celery.task)