<commit_before>import six
import mock

from celery.result import BaseAsyncResult
from celery.states import SUCCESS
from django.test import TestCase

from jobtastic import JobtasticTask
from jobtastic.states import PROGRESS
from jobtastic.tests import allow_checking_status_for_eager


class ProgressTask(JobtasticTask):
    """
    Just count up to the given number, with hooks for testing.
    """
    significant_kwargs = [
        ('count_to', str),
    ]
    herd_avoidance_timeout = 0

    def calculate_result(self, count_to, **kwargs):
        update_frequency = 2
        for counter in six.moves.range(count_to):
            self.update_progress(
                counter,
                count_to,
                update_frequency=update_frequency,
            )

        return count_to


def task_status_is_progress(self, **kwargs):
    task_id = self.request.id
    meta = BaseAsyncResult(task_id)

    with allow_checking_status_for_eager():
        assert meta.status == PROGRESS


class ProgressTestCase(TestCase):
    def setUp(self):
        self.task = ProgressTask

    def test_sanity(self):
        # The task actually runs
        with self.settings(CELERY_ALWAYS_EAGER=True):
            async_task = self.task.delay(count_to=2)
        self.assertEqual(async_task.status, SUCCESS)
        self.assertEqual(async_task.result, 2)

    def test_starts_with_progress_state(self):
        # The state has already been set to PROGRESS before `calculate_result`
        # starts
        with self.settings(CELERY_ALWAYS_EAGER=True):
            with mock.patch.object(
                self.task,
                'calculate_result',<commit_msg>Updated import path.<commit_after>import six
import mock

from celery.result import AsyncResult
from celery.states import SUCCESS
from django.test import TestCase

from jobtastic import JobtasticTask
from jobtastic.states import PROGRESS
from jobtastic.tests import allow_checking_status_for_eager


class ProgressTask(JobtasticTask):
    """
    Just count up to the given number, with hooks for testing.
    """
    significant_kwargs = [
        ('count_to', str),
    ]
    herd_avoidance_timeout = 0

    def calculate_result(self, count_to, **kwargs):
        update_frequency = 2
        for counter in six.moves.range(count_to):
            self.update_progress(
                counter,
                count_to,
                update_frequency=update_frequency,
            )

        return count_to


def task_status_is_progress(self, **kwargs):
    task_id = self.request.id
    meta = AsyncResult(task_id)

    with allow_checking_status_for_eager():
        assert meta.status == PROGRESS


class ProgressTestCase(TestCase):
    def setUp(self):
        self.task = ProgressTask

    def test_sanity(self):
        # The task actually runs
        with self.settings(CELERY_ALWAYS_EAGER=True):
            async_task = self.task.delay(count_to=2)
        self.assertEqual(async_task.status, SUCCESS)
        self.assertEqual(async_task.result, 2)

    def test_starts_with_progress_state(self):
        # The state has already been set to PROGRESS before `calculate_result`
        # starts
        with self.settings(CELERY_ALWAYS_EAGER=True):
            with mock.patch.object(
                self.task,
                'calculate_result',
{'fraction_non_alphanumeric': 0.07261223260018078, 'fraction_numerical': 0.0024103645676408557, 'mean_word_length': 2.680709534368071, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4604092', 'n_tokens_mistral': 989, 'n_tokens_neox': 942, 'n_words': 227}