"""Test for go.base.decorators."""

from go.vumitools.tests.helpers import djangotest_imports

with djangotest_imports(globals()):
    from go.base.tests.helpers import GoDjangoTestCase
    from go.base.decorators import render_exception
    from django.template.response import TemplateResponse


class CatchableDummyError(Exception):
    """Error that will be caught by DummyView.post."""


class UncatchableDummyError(Exception):
    """Error that will not be caught by DummyView.post."""


class DummyView(object):
    @render_exception(CatchableDummyError, 400, "Meep.")
    def post(self, request, err=None):
        if err is None:
            return "Success"
        raise err


class TestRenderException(GoDjangoTestCase):

    def test_no_exception(self):
        d = DummyView()
        self.assertEqual(d.post("request"), "Success")

    def test_expected_exception(self):
        d = DummyView()
        self.assertRaises(
            UncatchableDummyError, d.post, "request", UncatchableDummyError())

    def test_other_exception(self):
        d = DummyView()
        response = d.post("request", CatchableDummyError("foo"))
        self.assertTrue(isinstance(response, TemplateResponse))
        self.assertEqual(response.template_name, 'error.html')
        self.assertEqual(response.status_code, 400)

"""Test for go.base.decorators."""

from go.vumitools.tests.helpers import djangotest_imports

with djangotest_imports(globals()):
    from go.base.tests.helpers import GoDjangoTestCase
    from go.base.decorators import render_exception
    from django.template.response import TemplateResponse

    class CatchableDummyError(Exception):
        """Error that will be caught by DummyView.post."""

    class UncatchableDummyError(Exception):
        """Error that will not be caught by DummyView.post."""

    class DummyView(object):
        @render_exception(CatchableDummyError, 400, "Meep.")
        def post(self, request, err=None):
            if err is None:
                return "Success"
            raise err


class TestRenderException(GoDjangoTestCase):

    def test_no_exception(self):
        d = DummyView()
        self.assertEqual(d.post("request"), "Success")

    def test_expected_exception(self):
        d = DummyView()
        self.assertRaises(
            UncatchableDummyError, d.post, "request", UncatchableDummyError())

    def test_other_exception(self):
        d = DummyView()
        response = d.post("request", CatchableDummyError("foo"))
        self.assertTrue(isinstance(response, TemplateResponse))
        self.assertEqual(response.template_name, 'error.html')
        self.assertEqual(response.status_code, 400)

Move Django-specific pieces into the django_imports block.
Move Django-specific pieces into the django_imports block.

  """Test for go.base.decorators."""
  
  from go.vumitools.tests.helpers import djangotest_imports
  
  with djangotest_imports(globals()):
      from go.base.tests.helpers import GoDjangoTestCase
      from go.base.decorators import render_exception
      from django.template.response import TemplateResponse
  
+     class CatchableDummyError(Exception):
+         """Error that will be caught by DummyView.post."""
  
- class CatchableDummyError(Exception):
+     class UncatchableDummyError(Exception):
-     """Error that will be caught by DummyView.post."""
+         """Error that will not be caught by DummyView.post."""
  
- 
- class UncatchableDummyError(Exception):
-     """Error that will not be caught by DummyView.post."""
- 
- 
- class DummyView(object):
+     class DummyView(object):
-     @render_exception(CatchableDummyError, 400, "Meep.")
+         @render_exception(CatchableDummyError, 400, "Meep.")
-     def post(self, request, err=None):
+         def post(self, request, err=None):
-         if err is None:
+             if err is None:
-             return "Success"
+                 return "Success"
-         raise err
+             raise err
  
  
  class TestRenderException(GoDjangoTestCase):
  
      def test_no_exception(self):
          d = DummyView()
          self.assertEqual(d.post("request"), "Success")
  
      def test_expected_exception(self):
          d = DummyView()
          self.assertRaises(
              UncatchableDummyError, d.post, "request", UncatchableDummyError())
  
      def test_other_exception(self):
          d = DummyView()
          response = d.post("request", CatchableDummyError("foo"))
          self.assertTrue(isinstance(response, TemplateResponse))
          self.assertEqual(response.template_name, 'error.html')
          self.assertEqual(response.status_code, 400)
  
Move Django-specific pieces into the django_imports block.
## Code Before:
"""Test for go.base.decorators."""

from go.vumitools.tests.helpers import djangotest_imports

with djangotest_imports(globals()):
    from go.base.tests.helpers import GoDjangoTestCase
    from go.base.decorators import render_exception
    from django.template.response import TemplateResponse


class CatchableDummyError(Exception):
    """Error that will be caught by DummyView.post."""


class UncatchableDummyError(Exception):
    """Error that will not be caught by DummyView.post."""


class DummyView(object):
    @render_exception(CatchableDummyError, 400, "Meep.")
    def post(self, request, err=None):
        if err is None:
            return "Success"
        raise err


class TestRenderException(GoDjangoTestCase):

    def test_no_exception(self):
        d = DummyView()
        self.assertEqual(d.post("request"), "Success")

    def test_expected_exception(self):
        d = DummyView()
        self.assertRaises(
            UncatchableDummyError, d.post, "request", UncatchableDummyError())

    def test_other_exception(self):
        d = DummyView()
        response = d.post("request", CatchableDummyError("foo"))
        self.assertTrue(isinstance(response, TemplateResponse))
        self.assertEqual(response.template_name, 'error.html')
        self.assertEqual(response.status_code, 400)

## Instruction:
Move Django-specific pieces into the django_imports block.
## Code After:
"""Test for go.base.decorators."""

from go.vumitools.tests.helpers import djangotest_imports

with djangotest_imports(globals()):
    from go.base.tests.helpers import GoDjangoTestCase
    from go.base.decorators import render_exception
    from django.template.response import TemplateResponse

    class CatchableDummyError(Exception):
        """Error that will be caught by DummyView.post."""

    class UncatchableDummyError(Exception):
        """Error that will not be caught by DummyView.post."""

    class DummyView(object):
        @render_exception(CatchableDummyError, 400, "Meep.")
        def post(self, request, err=None):
            if err is None:
                return "Success"
            raise err


class TestRenderException(GoDjangoTestCase):

    def test_no_exception(self):
        d = DummyView()
        self.assertEqual(d.post("request"), "Success")

    def test_expected_exception(self):
        d = DummyView()
        self.assertRaises(
            UncatchableDummyError, d.post, "request", UncatchableDummyError())

    def test_other_exception(self):
        d = DummyView()
        response = d.post("request", CatchableDummyError("foo"))
        self.assertTrue(isinstance(response, TemplateResponse))
        self.assertEqual(response.template_name, 'error.html')
        self.assertEqual(response.status_code, 400)
