# Path: caravan/tests/fixtures.py
# class TestWorkflow1(Workflow):
# class TestWorkflow2(Workflow):
# 
# Path: caravan/tests/util.py
# def mock_args(args):
#     return mock.patch('sys.argv', ['PROG'] + args)
# 
# Path: caravan/commands/decider.py
# class Command(BaseCommand):
# 
#     description = 'Decider worker'
# 
#     def setup_arguments(self, parser):
#         parser.add_argument('-m', '--modules',
#                             type=ClassesLoaderFromModule(Workflow),
#                             nargs='+',
#                             required=True)
#         parser.add_argument('-d', '--domain',
#                             required=True)
#         parser.add_argument('-t', '--task-list',
#                             required=True)
#         parser.add_argument('--register-workflows', action='store_true')
# 
#     def run(self):
#         connection = get_connection()
#         workflows = [w for module in self.args.modules for w in module]
# 
#         if self.args.register_workflows:
#             log.info("Registering workflow types")
#             for workflow in workflows:
#                 created = register_workflow(connection=connection,
#                                             domain=self.args.domain,
#                                             workflow=workflow)
#                 if created:
#                     log.info("Workflow type %s(%s): registered.",
#                              workflow.name, workflow.version)
#                 else:
#                     log.info("Workflow type %s(%s): already registered.",
#                              workflow.name, workflow.version)
# 
#         log.info("Start decider worker...")
#         worker = Worker(connection=connection,
#                         domain=self.args.domain,
#                         task_list=self.args.task_list,
#                         workflows=workflows)
# 
#         while True:
#             try:
#                 worker.run()
#             except Exception:  # Doesn't catch KeyboardInterrupt
#                 log.exception("Decider crashed!")
# 
# Path: caravan/commands/decider.py
# class Command(BaseCommand):
#     def setup_arguments(self, parser):
#     def run(self):


import unittest
import mock
import caravan
from caravan.tests import fixtures
from caravan.tests.util import mock_args
from caravan.commands.decider import Command
from caravan.commands.decider import ClassesLoaderFromModule
                         [fixtures.TestWorkflow1, fixtures.TestWorkflow2])

        # Boto3 client object are dynamically forged...
        swf_connection = kwargs['connection']
        self.assertEqual(swf_connection.__class__.__name__, 'SWF')
        self.assertEqual(swf_connection.__class__.__module__, 'botocore.client')

    @mock.patch('caravan.commands.decider.register_workflow', autospec=True)
    @mock.patch('caravan.commands.decider.get_connection', autospec=True)
    def test_register_workflows(self, m_get_conn, m_register, m_worker_cls):
        m_worker = m_worker_cls.return_value
        m_worker.run.side_effect = KeyboardInterrupt('KILLTEST')

        m_conn = m_get_conn.return_value

        args = [
            '-d', 'DOMAIN', '-m', 'caravan.tests.fixtures', '-t', 'TASKLIST',
            '--register-workflows',
            ]

        with mock_args(args):
            with self.assertRaises(SystemExit):
                Command.main()

        expected = [
            mock.call(connection=m_conn,
                      domain='DOMAIN',
                      workflow=fixtures.TestWorkflow1),
            mock.call(connection=m_conn,
                      domain='DOMAIN',

# Path: caravan/tests/fixtures.py
# class TestWorkflow1(Workflow):
# class TestWorkflow2(Workflow):
# 
# Path: caravan/tests/util.py
# def mock_args(args):
#     return mock.patch('sys.argv', ['PROG'] + args)
# 
# Path: caravan/commands/decider.py
# class Command(BaseCommand):
# 
#     description = 'Decider worker'
# 
#     def setup_arguments(self, parser):
#         parser.add_argument('-m', '--modules',
#                             type=ClassesLoaderFromModule(Workflow),
#                             nargs='+',
#                             required=True)
#         parser.add_argument('-d', '--domain',
#                             required=True)
#         parser.add_argument('-t', '--task-list',
#                             required=True)
#         parser.add_argument('--register-workflows', action='store_true')
# 
#     def run(self):
#         connection = get_connection()
#         workflows = [w for module in self.args.modules for w in module]
# 
#         if self.args.register_workflows:
#             log.info("Registering workflow types")
#             for workflow in workflows:
#                 created = register_workflow(connection=connection,
#                                             domain=self.args.domain,
#                                             workflow=workflow)
#                 if created:
#                     log.info("Workflow type %s(%s): registered.",
#                              workflow.name, workflow.version)
#                 else:
#                     log.info("Workflow type %s(%s): already registered.",
#                              workflow.name, workflow.version)
# 
#         log.info("Start decider worker...")
#         worker = Worker(connection=connection,
#                         domain=self.args.domain,
#                         task_list=self.args.task_list,
#                         workflows=workflows)
# 
#         while True:
#             try:
#                 worker.run()
#             except Exception:  # Doesn't catch KeyboardInterrupt
#                 log.exception("Decider crashed!")
# 
# Path: caravan/commands/decider.py
# class Command(BaseCommand):
#     def setup_arguments(self, parser):
#     def run(self):


# Path: caravan/tests/commands/test_decider.py
import unittest
import mock
import caravan
from caravan.tests import fixtures
from caravan.tests.util import mock_args
from caravan.commands.decider import Command
from caravan.commands.decider import ClassesLoaderFromModule

                         [fixtures.TestWorkflow1, fixtures.TestWorkflow2])

        # Boto3 client object are dynamically forged...
        swf_connection = kwargs['connection']
        self.assertEqual(swf_connection.__class__.__name__, 'SWF')
        self.assertEqual(swf_connection.__class__.__module__, 'botocore.client')

    @mock.patch('caravan.commands.decider.register_workflow', autospec=True)
    @mock.patch('caravan.commands.decider.get_connection', autospec=True)
    def test_register_workflows(self, m_get_conn, m_register, m_worker_cls):
        m_worker = m_worker_cls.return_value
        m_worker.run.side_effect = KeyboardInterrupt('KILLTEST')

        m_conn = m_get_conn.return_value

        args = [
            '-d', 'DOMAIN', '-m', 'caravan.tests.fixtures', '-t', 'TASKLIST',
            '--register-workflows',
            ]

        with mock_args(args):
            with self.assertRaises(SystemExit):
                Command.main()

        expected = [
            mock.call(connection=m_conn,
                      domain='DOMAIN',
                      workflow=fixtures.TestWorkflow1),
            mock.call(connection=m_conn,
                      domain='DOMAIN',

                      workflow=fixtures.TestWorkflow2),