<commit_before># -*- coding: utf-8 -*-
from django.test import TestCase

from elasticsearch.exceptions import TransportError

from ..compat import mock
from ..management.commands import (
    BaseSearchCommand,
    create_search_index,
    delete_search_index,
    prune_search_index,
    update_search_index,
    rebuild_search_index
)


class BaseSearchCommandTests(TestCase):

    """Tests for the elasticsearch_django management commands base command."""

    @mock.patch('elasticsearch_django.management.commands.logger')
    @mock.patch.object(BaseSearchCommand, 'do_index_command')
    def test_handle(self, mock_do, mock_log):
        """Test the main handle method calls do_index_command."""
        obj = BaseSearchCommand()
        obj.handle(indexes=['foo', 'bar'])
        # this should have called the do_index_command twice
        mock_do.assert_has_calls([mock.call('foo'), mock.call('bar')])
        mock_do.reset_mock()
        mock_do.side_effect = TransportError(123, "oops", {'error': {'reason': 'no idea'}})
        obj.handle(indexes=['baz'])
        mock_do.assert_called_once_with('baz')
        mock_log.warn.assert_called_once()


class NamedCommandTests(TestCase):

    """Test each named command."""

    @mock.patch('elasticsearch_django.management.commands.create_search_index.create_index')
    def test_create_search_index(self, mock_create):
        """Test the create_search_index command."""
        cmd = create_search_index.Command()
        cmd.do_index_command('foo')
        mock_create.assert_called_once_with('foo')

    @mock.patch('elasticsearch_django.management.commands.delete_search_index.delete_index')
    def test_delete_search_index(self, mock_delete):
        """Test the delete_search_index command."""
        cmd = delete_search_index.Command()
        retval = cmd.do_index_command('foo', interactive=False)  # True would hang the tests
        self.assertEqual(retval, mock_delete.return_value)
        mock_delete.assert_called_once_with('foo')
        mock_delete.reset_mock()

        # mock out thw raw_input so the test doesn't hang
        with mock.patch.object(delete_search_index.Command, '_confirm_action') as mock_confirm:
            mock_confirm.return_value = False
            retval = cmd.do_index_command('foo', interactive=True)
            mock_delete.assert_not_called()
            self.assertIsNone(retval)

    @mock.patch('elasticsearch_django.management.commands.prune_search_index.prune_index')<commit_msg>Fix up test failure<commit_after># -*- coding: utf-8 -*-
from django.test import TestCase

from elasticsearch.exceptions import TransportError

from ..compat import mock
from ..management.commands import (
    BaseSearchCommand,
    create_search_index,
    delete_search_index,
    prune_search_index,
    update_search_index,
    rebuild_search_index
)


class BaseSearchCommandTests(TestCase):

    """Tests for the elasticsearch_django management commands base command."""

    @mock.patch('elasticsearch_django.management.commands.logger')
    @mock.patch.object(BaseSearchCommand, 'do_index_command')
    def test_handle(self, mock_do, mock_log):
        """Test the main handle method calls do_index_command."""
        obj = BaseSearchCommand()
        obj.handle(indexes=['foo', 'bar'])
        # this should have called the do_index_command twice
        mock_do.assert_has_calls([mock.call('foo'), mock.call('bar')])
        mock_do.reset_mock()
        mock_do.side_effect = TransportError(123, "oops", {'error': {'reason': 'no idea'}})
        obj.handle(indexes=['baz'])
        mock_do.assert_called_once_with('baz')
        mock_log.warning.assert_called_once()


class NamedCommandTests(TestCase):

    """Test each named command."""

    @mock.patch('elasticsearch_django.management.commands.create_search_index.create_index')
    def test_create_search_index(self, mock_create):
        """Test the create_search_index command."""
        cmd = create_search_index.Command()
        cmd.do_index_command('foo')
        mock_create.assert_called_once_with('foo')

    @mock.patch('elasticsearch_django.management.commands.delete_search_index.delete_index')
    def test_delete_search_index(self, mock_delete):
        """Test the delete_search_index command."""
        cmd = delete_search_index.Command()
        retval = cmd.do_index_command('foo', interactive=False)  # True would hang the tests
        self.assertEqual(retval, mock_delete.return_value)
        mock_delete.assert_called_once_with('foo')
        mock_delete.reset_mock()

        # mock out thw raw_input so the test doesn't hang
        with mock.patch.object(delete_search_index.Command, '_confirm_action') as mock_confirm:
            mock_confirm.return_value = False
            retval = cmd.do_index_command('foo', interactive=True)
            mock_delete.assert_not_called()
            self.assertIsNone(retval)

    @mock.patch('elasticsearch_django.management.commands.prune_search_index.prune_index')
{'fraction_non_alphanumeric': 0.10614860805127178, 'fraction_numerical': 0.0016022431403965552, 'mean_word_length': 3.848543689320388, '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': '5001271', 'n_tokens_mistral': 1438, 'n_tokens_neox': 1419, 'n_words': 270}