from curator import curator

from . import CuratorTestCase

class TestCloseIndex(CuratorTestCase):
    def test_positive(self):
        self.create_index('test_index')
        self.client.indices.close('test_index')

        self.assertTrue(curator.index_closed(self.client, 'test_index'))

    def test_negative(self):
        self.create_index('test_index')

        self.assertFalse(curator.index_closed(self.client, 'test_index'))

class TestCloseIndex(CuratorTestCase):
    def test_index_will_be_closed(self):
        self.create_index('test_index')
        curator._close_index(self.client, 'test_index')
        index_metadata = self.client.cluster.state(
            index='test_index',
            metric='metadata',
        )
        self.assertEquals('close', index_metadata['metadata']['indices']['test_index']['state'])

    def test_closed_index_will_be_skipped(self):
        self.create_index('test_index')
        self.client.indices.close(index='test_index')
        self.assertTrue(curator._close_index(self.client, 'test_index'))
        index_metadata = self.client.cluster.state(
            index='test_index',
            metric='metadata',
        )
        self.assertEquals('close', index_metadata['metadata']['indices']['test_index']['state'])

class TestDeleteIndex(CuratorTestCase):
    def test_index_will_be_deleted(self):
        self.create_index('test_index')
        curator._delete_index(self.client, 'test_index')
        self.assertFalse(self.client.indices.exists('test_index'))

class TestBloomIndex(CuratorTestCase):
    def test_bloom_filter_will_be_disabled(self):
        self.create_index('test_index')
        curator._bloom_index(self.client, 'test_index')

        settings = self.client.indices.get_settings(index='test_index')
        self.assertEquals('false', settings['test_index']['settings']['index']['codec']['bloom']['load'])

    def test_closed_index_will_be_skipped(self):
        self.create_index('test_index')
        self.client.indices.close(index='test_index')
        self.assertTrue(curator._bloom_index(self.client, 'test_index'))
        index_metadata = self.client.cluster.state(
            index='test_index',
            metric='metadata',
        )
        self.assertEquals('close', index_metadata['metadata']['indices']['test_index']['state'])

@@ -22,16 +22,46 @@
 urator%0A%0A
+from mock import patch, Mock%0A%0A
 from . i
@@ -2327,28 +2327,1527 @@
 s'%5D%5B'test_index'%5D%5B'state'%5D)%0A
+%0Aclass TestOptimizeIndex(CuratorTestCase):%0A    def test_closed_index_will_be_skipped(self):%0A        self.create_index('test_index')%0A        self.client.indices.close(index='test_index')%0A        self.assertTrue(curator._optimize_index(self.client, 'test_index'))%0A        index_metadata = self.client.cluster.state(%0A            index='test_index',%0A            metric='metadata',%0A        )%0A        self.assertEquals('close', index_metadata%5B'metadata'%5D%5B'indices'%5D%5B'test_index'%5D%5B'state'%5D)%0A%0A    @patch('curator.curator.get_segmentcount')%0A    def test_optimized_index_will_be_skipped(self, get_segmentcount):%0A        get_segmentcount.return_value = 1, 4%0A        self.create_index('test_index')%0A        self.assertTrue(curator._optimize_index(self.client, 'test_index', max_num_segments=4))%0A        get_segmentcount.assert_called_once_with(self.client, 'test_index')%0A%0A    @patch('curator.curator.index_closed')%0A    @patch('curator.curator.get_segmentcount')%0A    def test_unoptimized_index_will_be_optimized(self, get_segmentcount, index_closed):%0A        get_segmentcount.return_value = 1, 40%0A        index_closed.return_value = False%0A        client = Mock()%0A        self.create_index('test_index')%0A        curator._optimize_index(client, 'test_index', max_num_segments=4)%0A        get_segmentcount.assert_called_once_with(client, 'test_index')%0A        index_closed.assert_called_once_with(client, 'test_index')%0A        client.indices.optimize.assert_called_once_with(index='test_index', max_num_segments=4)%0A%0A

Test _optimize_index