<commit_before>#
# Copyright 2012 WebFilings, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import base64
import json
import os
import unittest

from nose.plugins.attrib import attr

from mock import call
from mock import Mock
from mock import patch


class TestExecuteTask(unittest.TestCase):
    """Ensure _execute_task runs the tasks."""

    @patch('furious.batcher.Message')
    def test_execute_task(self, Message):
        """When a task is passed to _execute_task, make sure it is executed,
        and the task's environment is cleaned up."""

        from furious.context import _local
        from furious.test_stubs.appengine.queues import _execute_task

        # Create the async_options to call the dir function
        async_options = {'job': ('furious.batcher.Message', None, None)}

        body = base64.b64encode(json.dumps(async_options))

        task = {'body': body, 'headers': ''}

        _execute_task(task)

        # Make sure our function was called
        self.assertTrue(Message.called)

        # Make sure cleanup worked
        self.assertFalse('REQUEST_ID_HASH' in os.environ)
        self.assertFalse(hasattr(_local._local_context, 'registry'))


class TestExecuteQueue(unittest.TestCase):
    """Ensure tasks from queues are executed."""

    @patch('furious.test_stubs.appengine.queues._execute_task')
    def test_execute_queue(self, _execute_task):
        """When execute_queues is called, ensure tasks are executed, and
        the queue is flushed to remove executed tasks.  Also, ensure True
        is returned since messages were processed.
        """

        from furious.test_stubs.appengine.queues import _execute_queue

        queue_service = Mock()
        queue_service.GetTasks.return_value = ['task1', 'task2', 'task3']

        any_processed = _execute_queue('default', queue_service)

        # Expect _execute_task() to be called for each task
        expected_call_args_list = [call('task1'), call('task2'), call('task3')]

        self.assertEquals(_execute_task.call_args_list,
                          expected_call_args_list)
<commit_msg>Add test for _execute_task with args and kwargs<commit_after>#
# Copyright 2012 WebFilings, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import base64
import json
import os
import unittest

from nose.plugins.attrib import attr

from mock import call
from mock import Mock
from mock import patch


class TestExecuteTask(unittest.TestCase):
    """Ensure _execute_task runs the tasks."""

    @patch('furious.batcher.Message')
    def test_execute_task(self, Message):
        """When a task is passed to _execute_task, make sure it is executed.
        Ensure the task's environment is cleaned up.
        """

        from furious.context import _local
        from furious.test_stubs.appengine.queues import _execute_task

        # Create the async_options to call the target, Message()
        async_options = {'job': ('furious.batcher.Message', None, None)}

        body = base64.b64encode(json.dumps(async_options))

        task = {'body': body, 'headers': ''}

        _execute_task(task)

        # Make sure our function was called
        self.assertTrue(Message.called)

        # Make sure context cleanup worked
        self.assertFalse('REQUEST_ID_HASH' in os.environ)
        self.assertFalse(hasattr(_local._local_context, 'registry'))

    @patch('furious.batcher.MessageProcessor', autospec=True)
    def test_execute_task_with_args_kwargs(self, MessageProcessor):
        """When a task with args and kwargs is passed to _execute_task, make
        sure it is executed with those parameters.
        Ensure the task's environment is cleaned up.
        """

        from furious.context import _local
        from furious.test_stubs.appengine.queues import _execute_task

        # Create the async_options to call the target, MessageProcessor()
        args = [1, 2]
        kwargs = {'my_kwarg': 'my_value'}
        async_options = {'job': ('furious.batcher.MessageProcessor',
                                 args, kwargs)}

        body = base64.b64encode(json.dumps(async_options))

        task = {'body': body, 'headers': ''}

        _execute_task(task)

        # Make sure our function was called with the right arguments
        MessageProcessor.assert_called_once_with(*args, **kwargs)

        # Make sure context cleanup worked
        self.assertFalse('REQUEST_ID_HASH' in os.environ)
        self.assertFalse(hasattr(_local._local_context, 'registry'))


class TestExecuteQueue(unittest.TestCase):
    """Ensure tasks from queues are executed."""

    @patch('furious.test_stubs.appengine.queues._execute_task')
    def test_execute_queue(self, _execute_task):
        """When execute_queues is called, ensure tasks are executed, and
        the queue is flushed to remove executed tasks.  Also, ensure True
        is returned since messages were processed.
        """

        from furious.test_stubs.appengine.queues import _execute_queue

        queue_service = Mock()
        queue_service.GetTasks.return_value = ['task1', 'task2', 'task3']

        any_processed = _execute_queue('default', queue_service)

        # Expect _execute_task() to be called for each task
        expected_call_args_list = [call('task1'), call('task2'), call('task3')]

        self.assertEquals(_execute_task.call_args_list,
                          expected_call_args_list)

{'fraction_non_alphanumeric': 0.0859192265710276, 'fraction_numerical': 0.0071729299859660065, 'mean_word_length': 3.242063492063492, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3394556', 'n_tokens_mistral': 1775, 'n_tokens_neox': 1716, 'n_words': 587}