Skip to content

Commit

Permalink
Tested exception messages in generic_views tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
francoisfreitag authored and timgraham committed Dec 5, 2018
1 parent 196b420 commit fbc7e41
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions tests/generic_views/test_base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import time
import unittest

from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponse
Expand Down Expand Up @@ -65,7 +64,7 @@ def get(self, request):
return self


class ViewTest(unittest.TestCase):
class ViewTest(SimpleTestCase):
rf = RequestFactory()

def _assert_simple(self, response):
Expand All @@ -76,14 +75,16 @@ def test_no_init_kwargs(self):
"""
A view can't be accidentally instantiated before deployment
"""
with self.assertRaises(AttributeError):
msg = 'This method is available only on the class, not on instances.'
with self.assertRaisesMessage(AttributeError, msg):
SimpleView(key='value').as_view()

def test_no_init_args(self):
"""
A view can't be accidentally instantiated before deployment
"""
with self.assertRaises(TypeError):
msg = 'as_view() takes 1 positional argument but 2 were given'
with self.assertRaisesMessage(TypeError, msg):
SimpleView.as_view('value')

def test_pathological_http_method(self):
Expand Down Expand Up @@ -134,15 +135,24 @@ def test_invalid_keyword_argument(self):
View arguments must be predefined on the class and can't
be named like a HTTP method.
"""
msg = (
"You tried to pass in the %s method name as a keyword argument "
"to SimpleView(). Don't do that."
)
# Check each of the allowed method names
for method in SimpleView.http_method_names:
with self.assertRaises(TypeError):
with self.assertRaisesMessage(TypeError, msg % method):
SimpleView.as_view(**{method: 'value'})

# Check the case view argument is ok if predefined on the class...
CustomizableView.as_view(parameter="value")
# ...but raises errors otherwise.
with self.assertRaises(TypeError):
msg = (
"CustomizableView() received an invalid keyword 'foobar'. "
"as_view only accepts arguments that are already attributes of "
"the class."
)
with self.assertRaisesMessage(TypeError, msg):
CustomizableView.as_view(foobar="value")

def test_calling_more_than_once(self):
Expand Down Expand Up @@ -466,7 +476,7 @@ def test_direct_instantiation(self):
self.assertEqual(response.status_code, 410)


class GetContextDataTest(unittest.TestCase):
class GetContextDataTest(SimpleTestCase):

def test_get_context_data_super(self):
test_view = views.CustomContextView()
Expand Down Expand Up @@ -495,7 +505,7 @@ def test_object_in_get_context_data(self):
self.assertEqual(context['object'], test_view.object)


class UseMultipleObjectMixinTest(unittest.TestCase):
class UseMultipleObjectMixinTest(SimpleTestCase):
rf = RequestFactory()

def test_use_queryset_from_view(self):
Expand All @@ -515,7 +525,7 @@ def test_overwrite_queryset(self):
self.assertEqual(context['object_list'], queryset)


class SingleObjectTemplateResponseMixinTest(unittest.TestCase):
class SingleObjectTemplateResponseMixinTest(SimpleTestCase):

def test_template_mixin_without_template(self):
"""
Expand All @@ -524,5 +534,9 @@ def test_template_mixin_without_template(self):
TemplateDoesNotExist.
"""
view = views.TemplateResponseWithoutTemplate()
with self.assertRaises(ImproperlyConfigured):
msg = (
"TemplateResponseMixin requires either a definition of "
"'template_name' or an implementation of 'get_template_names()'"
)
with self.assertRaisesMessage(ImproperlyConfigured, msg):
view.get_template_names()

0 comments on commit fbc7e41

Please sign in to comment.