import unittest
from pyes.tests import ESTestCase
import pyes.exceptions

class ErrorReportingTestCase(ESTestCase):
    def setUp(self):
        super(ErrorReportingTestCase, self).setUp()

    def testCreateDelete(self):
        """

        Test errors thrown when creating or deleting.

        """
        try:
            self.conn.delete_index("test-index")
        except pyes.exceptions.NotFoundException:
            pass
        self.conn.create_index("test-index")
        self.assertRaises(pyes.exceptions.AlreadyExistsException, self.conn.create_index, "test-index")
        self.conn.delete_index("test-index")
        self.assertRaises(pyes.exceptions.NotFoundException, self.conn.delete_index, "test-index")

if __name__ == "__main__":
    unittest.main()

import unittest
from pyes.tests import ESTestCase
import pyes.exceptions

class ErrorReportingTestCase(ESTestCase):
    def setUp(self):
        super(ErrorReportingTestCase, self).setUp()

    def testCreateDelete(self):
        """Test errors thrown when creating or deleting indexes.

        """
        try:
            self.conn.delete_index("test-index")
        except pyes.exceptions.NotFoundException:
            pass

        self.conn.create_index("test-index")
        err = self.checkRaises(pyes.exceptions.AlreadyExistsException,
                               self.conn.create_index, "test-index")
        self.assertEqual(str(err), "[test-index] Already exists")
        self.assertEqual(err.status, 400)
        self.assertTrue('error' in err.result)
        self.assertTrue('ok' not in err.result)

        self.conn.delete_index("test-index")
        err = self.checkRaises(pyes.exceptions.NotFoundException,
                               self.conn.delete_index, "test-index")
        self.assertEqual(str(err), "[test-index] missing")
        self.assertEqual(err.status, 400)
        self.assertTrue('error' in err.result)
        self.assertTrue('ok' not in err.result)

    def testMissingIndex(self):
        """Test generation of a IndexMissingException.

        """
        try:
            self.conn.delete_index("test-index")
        except pyes.exceptions.NotFoundException:
            pass
        err = self.checkRaises(pyes.exceptions.IndexMissingException,
                               self.conn.flush, 'test-index')
        self.assertEqual(str(err), "[test-index] missing")
        self.assertEqual(err.status, 500)
        self.assertTrue('error' in err.result)
        self.assertTrue('ok' not in err.result)

if __name__ == "__main__":
    unittest.main()

Test that various exceptions are correctly converted
Test that various exceptions are correctly converted

  import unittest
  from pyes.tests import ESTestCase
  import pyes.exceptions
  
  class ErrorReportingTestCase(ESTestCase):
      def setUp(self):
          super(ErrorReportingTestCase, self).setUp()
  
      def testCreateDelete(self):
-         """
- 
-         Test errors thrown when creating or deleting.
+         """Test errors thrown when creating or deleting indexes.
  
          """
          try:
              self.conn.delete_index("test-index")
          except pyes.exceptions.NotFoundException:
              pass
+ 
          self.conn.create_index("test-index")
-         self.assertRaises(pyes.exceptions.AlreadyExistsException, self.conn.create_index, "test-index")
+         err = self.checkRaises(pyes.exceptions.AlreadyExistsException,
+                                self.conn.create_index, "test-index")
+         self.assertEqual(str(err), "[test-index] Already exists")
+         self.assertEqual(err.status, 400)
+         self.assertTrue('error' in err.result)
+         self.assertTrue('ok' not in err.result)
+ 
          self.conn.delete_index("test-index")
-         self.assertRaises(pyes.exceptions.NotFoundException, self.conn.delete_index, "test-index")
+         err = self.checkRaises(pyes.exceptions.NotFoundException,
+                                self.conn.delete_index, "test-index")
+         self.assertEqual(str(err), "[test-index] missing")
+         self.assertEqual(err.status, 400)
+         self.assertTrue('error' in err.result)
+         self.assertTrue('ok' not in err.result)
+ 
+     def testMissingIndex(self):
+         """Test generation of a IndexMissingException.
+ 
+         """
+         try:
+             self.conn.delete_index("test-index")
+         except pyes.exceptions.NotFoundException:
+             pass
+         err = self.checkRaises(pyes.exceptions.IndexMissingException,
+                                self.conn.flush, 'test-index')
+         self.assertEqual(str(err), "[test-index] missing")
+         self.assertEqual(err.status, 500)
+         self.assertTrue('error' in err.result)
+         self.assertTrue('ok' not in err.result)
  
  if __name__ == "__main__":
      unittest.main()
  
Test that various exceptions are correctly converted
## Code Before:
import unittest
from pyes.tests import ESTestCase
import pyes.exceptions

class ErrorReportingTestCase(ESTestCase):
    def setUp(self):
        super(ErrorReportingTestCase, self).setUp()

    def testCreateDelete(self):
        """

        Test errors thrown when creating or deleting.

        """
        try:
            self.conn.delete_index("test-index")
        except pyes.exceptions.NotFoundException:
            pass
        self.conn.create_index("test-index")
        self.assertRaises(pyes.exceptions.AlreadyExistsException, self.conn.create_index, "test-index")
        self.conn.delete_index("test-index")
        self.assertRaises(pyes.exceptions.NotFoundException, self.conn.delete_index, "test-index")

if __name__ == "__main__":
    unittest.main()

## Instruction:
Test that various exceptions are correctly converted
## Code After:
import unittest
from pyes.tests import ESTestCase
import pyes.exceptions

class ErrorReportingTestCase(ESTestCase):
    def setUp(self):
        super(ErrorReportingTestCase, self).setUp()

    def testCreateDelete(self):
        """Test errors thrown when creating or deleting indexes.

        """
        try:
            self.conn.delete_index("test-index")
        except pyes.exceptions.NotFoundException:
            pass

        self.conn.create_index("test-index")
        err = self.checkRaises(pyes.exceptions.AlreadyExistsException,
                               self.conn.create_index, "test-index")
        self.assertEqual(str(err), "[test-index] Already exists")
        self.assertEqual(err.status, 400)
        self.assertTrue('error' in err.result)
        self.assertTrue('ok' not in err.result)

        self.conn.delete_index("test-index")
        err = self.checkRaises(pyes.exceptions.NotFoundException,
                               self.conn.delete_index, "test-index")
        self.assertEqual(str(err), "[test-index] missing")
        self.assertEqual(err.status, 400)
        self.assertTrue('error' in err.result)
        self.assertTrue('ok' not in err.result)

    def testMissingIndex(self):
        """Test generation of a IndexMissingException.

        """
        try:
            self.conn.delete_index("test-index")
        except pyes.exceptions.NotFoundException:
            pass
        err = self.checkRaises(pyes.exceptions.IndexMissingException,
                               self.conn.flush, 'test-index')
        self.assertEqual(str(err), "[test-index] missing")
        self.assertEqual(err.status, 500)
        self.assertTrue('error' in err.result)
        self.assertTrue('ok' not in err.result)

if __name__ == "__main__":
    unittest.main()
