<commit_before>"""Verify :func:`foremsat.utils.tasks.check_task` functionality."""
from unittest import mock

import pytest

from foremast.exceptions import SpinnakerTaskError
from foremast.utils.tasks import _check_task

FAIL_MESSAGE = 'TERMINAL'
SUCCESS_MESSAGE = 'SUCCEEDED'


@mock.patch('foremast.utils.tasks.requests')
def test_task_success(mock_requests):
    """Successful Task."""
    mock_requests.get.return_value.json.return_value = {'status': SUCCESS_MESSAGE}

    result = _check_task(taskid='')

    assert result == SUCCESS_MESSAGE


@mock.patch('foremast.utils.tasks.requests')
def test_task_failure(mock_requests):
    """Failed Task."""
    mock_requests.get.return_value.json.return_value = {
        'status': FAIL_MESSAGE,
        'execution': {
            'stages': [],
        },
    }

    with pytest.raises(SpinnakerTaskError):
        _check_task(taskid='')


@mock.patch('foremast.utils.tasks.requests')
def test_task_unknown(mock_requests):
    """Unknown Task status raises exception to keep polling."""
    mock_requests.get.return_value.json.return_value = {'status': ''}

    with pytest.raises(ValueError):
        _check_task(taskid='')
<commit_msg>tests: Inconclusive Check Task polling<commit_after>"""Verify :func:`foremsat.utils.tasks.check_task` functionality."""
from unittest import mock

import pytest

from foremast.exceptions import SpinnakerTaskError
from foremast.utils.tasks import _check_task, check_task

FAIL_MESSAGE = 'TERMINAL'
SUCCESS_MESSAGE = 'SUCCEEDED'


@mock.patch('foremast.utils.tasks.requests')
def test_task_success(mock_requests):
    """Successful Task."""
    mock_requests.get.return_value.json.return_value = {'status': SUCCESS_MESSAGE}

    result = _check_task(taskid='')

    assert result == SUCCESS_MESSAGE


@mock.patch('foremast.utils.tasks.requests')
def test_task_failure(mock_requests):
    """Failed Task."""
    mock_requests.get.return_value.json.return_value = {
        'status': FAIL_MESSAGE,
        'execution': {
            'stages': [],
        },
    }

    with pytest.raises(SpinnakerTaskError):
        _check_task(taskid='')


@mock.patch('foremast.utils.tasks.requests')
def test_task_unknown(mock_requests):
    """Unknown Task status raises exception to keep polling."""
    mock_requests.get.return_value.json.return_value = {'status': ''}

    with pytest.raises(ValueError):
        _check_task(taskid='')


@mock.patch('foremast.utils.tasks._check_task')
def test_polling_inconclusive(mock_check):
    """Spinnaker Task with non-terminal state should raise exception."""
    mock_check.side_effect = ValueError

    with pytest.raises(ValueError):
        check_task(taskid='', timeout=2)

{'fraction_non_alphanumeric': 0.1273672484218344, 'fraction_numerical': 0.0003713330857779428, 'mean_word_length': 4.420523138832998, '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': '5433605', 'n_tokens_mistral': 863, 'n_tokens_neox': 850, 'n_words': 148}