2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2011-10-13 21:34:56 +00:00
|
|
|
|
2013-07-01 12:22:27 +00:00
|
|
|
from unittest import expectedFailure
|
2014-10-14 18:56:39 +00:00
|
|
|
import warnings
|
2013-02-21 21:56:55 +00:00
|
|
|
|
2010-10-18 13:34:47 +00:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2010-10-18 15:53:08 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
2010-10-18 13:34:47 +00:00
|
|
|
from django import forms
|
2014-04-05 06:04:46 +00:00
|
|
|
from django.test import TestCase, override_settings
|
2013-06-22 11:12:43 +00:00
|
|
|
from django.test.client import RequestFactory
|
2014-10-14 18:56:39 +00:00
|
|
|
from django.utils.deprecation import RemovedInDjango20Warning
|
2012-08-18 13:46:17 +00:00
|
|
|
from django.views.generic.base import View
|
2014-02-09 10:53:23 +00:00
|
|
|
from django.views.generic.edit import FormMixin, ModelFormMixin, CreateView
|
2010-10-18 13:34:47 +00:00
|
|
|
|
2011-10-13 21:34:56 +00:00
|
|
|
from . import views
|
|
|
|
from .models import Artist, Author
|
|
|
|
|
2010-10-18 13:34:47 +00:00
|
|
|
|
2012-03-17 22:31:03 +00:00
|
|
|
class FormMixinTests(TestCase):
|
2013-02-11 07:39:14 +00:00
|
|
|
def test_initial_data(self):
|
|
|
|
""" Test instance independence of initial data dict (see #16138) """
|
|
|
|
initial_1 = FormMixin().get_initial()
|
|
|
|
initial_1['foo'] = 'bar'
|
|
|
|
initial_2 = FormMixin().get_initial()
|
|
|
|
self.assertNotEqual(initial_1, initial_2)
|
2012-03-17 22:31:03 +00:00
|
|
|
|
2013-06-22 11:12:43 +00:00
|
|
|
def test_get_prefix(self):
|
|
|
|
""" Test prefix can be set (see #18872) """
|
|
|
|
test_string = 'test'
|
|
|
|
|
|
|
|
rf = RequestFactory()
|
|
|
|
get_request = rf.get('/')
|
|
|
|
|
|
|
|
class TestFormMixin(FormMixin):
|
|
|
|
request = get_request
|
|
|
|
|
|
|
|
default_kwargs = TestFormMixin().get_form_kwargs()
|
|
|
|
self.assertEqual(None, default_kwargs.get('prefix'))
|
|
|
|
|
|
|
|
set_mixin = TestFormMixin()
|
|
|
|
set_mixin.prefix = test_string
|
|
|
|
set_kwargs = set_mixin.get_form_kwargs()
|
|
|
|
self.assertEqual(test_string, set_kwargs.get('prefix'))
|
|
|
|
|
2014-10-14 18:56:39 +00:00
|
|
|
def test_get_form(self):
|
|
|
|
class TestFormMixin(FormMixin):
|
|
|
|
request = RequestFactory().get('/')
|
|
|
|
|
|
|
|
self.assertIsInstance(
|
|
|
|
TestFormMixin().get_form(forms.Form), forms.Form,
|
|
|
|
'get_form() should use provided form class.'
|
|
|
|
)
|
|
|
|
|
|
|
|
class FormClassTestFormMixin(TestFormMixin):
|
|
|
|
form_class = forms.Form
|
|
|
|
|
|
|
|
self.assertIsInstance(
|
|
|
|
FormClassTestFormMixin().get_form(), forms.Form,
|
|
|
|
'get_form() should fallback to get_form_class() if none is provided.'
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_get_form_missing_form_class_default_value(self):
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
class MissingDefaultValue(FormMixin):
|
|
|
|
request = RequestFactory().get('/')
|
|
|
|
form_class = forms.Form
|
|
|
|
|
|
|
|
def get_form(self, form_class):
|
|
|
|
return form_class(**self.get_form_kwargs())
|
|
|
|
self.assertEqual(len(w), 1)
|
|
|
|
self.assertEqual(w[0].category, RemovedInDjango20Warning)
|
|
|
|
self.assertEqual(
|
|
|
|
str(w[0].message),
|
|
|
|
'`generic_views.test_edit.MissingDefaultValue.get_form` method '
|
|
|
|
'must define a default value for its `form_class` argument.'
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertIsInstance(
|
|
|
|
MissingDefaultValue().get_form(), forms.Form,
|
|
|
|
)
|
|
|
|
|
2012-12-04 12:18:57 +00:00
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='generic_views.urls')
|
2012-12-04 12:18:57 +00:00
|
|
|
class BasicFormTests(TestCase):
|
|
|
|
|
|
|
|
def test_post_data(self):
|
|
|
|
res = self.client.post('/contact/', {'name': "Me", 'message': "Hello"})
|
|
|
|
self.assertRedirects(res, 'http://testserver/list/authors/')
|
|
|
|
|
|
|
|
|
2010-12-04 11:20:52 +00:00
|
|
|
class ModelFormMixinTests(TestCase):
|
|
|
|
def test_get_form(self):
|
|
|
|
form_class = views.AuthorGetQuerySetFormView().get_form_class()
|
2011-08-12 14:14:58 +00:00
|
|
|
self.assertEqual(form_class._meta.model, Author)
|
2010-10-18 13:34:47 +00:00
|
|
|
|
2014-02-09 10:53:23 +00:00
|
|
|
def test_get_form_checks_for_object(self):
|
|
|
|
mixin = ModelFormMixin()
|
|
|
|
mixin.request = RequestFactory().get('/')
|
|
|
|
self.assertEqual({'initial': {}, 'prefix': None},
|
|
|
|
mixin.get_form_kwargs())
|
|
|
|
|
2013-02-21 21:56:55 +00:00
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='generic_views.urls')
|
2010-10-18 13:34:47 +00:00
|
|
|
class CreateViewTests(TestCase):
|
|
|
|
|
|
|
|
def test_create(self):
|
|
|
|
res = self.client.get('/edit/authors/create/')
|
|
|
|
self.assertEqual(res.status_code, 200)
|
2013-05-21 09:42:15 +00:00
|
|
|
self.assertIsInstance(res.context['form'], forms.ModelForm)
|
|
|
|
self.assertIsInstance(res.context['view'], View)
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertNotIn('object', res.context)
|
|
|
|
self.assertNotIn('author', res.context)
|
2010-10-18 13:34:47 +00:00
|
|
|
self.assertTemplateUsed(res, 'generic_views/author_form.html')
|
|
|
|
|
|
|
|
res = self.client.post('/edit/authors/create/',
|
|
|
|
{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
|
|
|
|
self.assertEqual(res.status_code, 302)
|
|
|
|
self.assertRedirects(res, 'http://testserver/list/authors/')
|
|
|
|
self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
|
|
|
|
|
|
|
|
def test_create_invalid(self):
|
|
|
|
res = self.client.post('/edit/authors/create/',
|
|
|
|
{'name': 'A' * 101, 'slug': 'randall-munroe'})
|
|
|
|
self.assertEqual(res.status_code, 200)
|
|
|
|
self.assertTemplateUsed(res, 'generic_views/author_form.html')
|
|
|
|
self.assertEqual(len(res.context['form'].errors), 1)
|
|
|
|
self.assertEqual(Author.objects.count(), 0)
|
|
|
|
|
|
|
|
def test_create_with_object_url(self):
|
|
|
|
res = self.client.post('/edit/artists/create/',
|
|
|
|
{'name': 'Rene Magritte'})
|
|
|
|
self.assertEqual(res.status_code, 302)
|
2011-01-04 05:57:32 +00:00
|
|
|
artist = Artist.objects.get(name='Rene Magritte')
|
|
|
|
self.assertRedirects(res, 'http://testserver/detail/artist/%d/' % artist.pk)
|
2010-10-18 13:34:47 +00:00
|
|
|
self.assertQuerysetEqual(Artist.objects.all(), ['<Artist: Rene Magritte>'])
|
|
|
|
|
|
|
|
def test_create_with_redirect(self):
|
|
|
|
res = self.client.post('/edit/authors/create/redirect/',
|
|
|
|
{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
|
|
|
|
self.assertEqual(res.status_code, 302)
|
|
|
|
self.assertRedirects(res, 'http://testserver/edit/authors/create/')
|
|
|
|
self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
|
|
|
|
|
2010-12-04 11:20:30 +00:00
|
|
|
def test_create_with_interpolated_redirect(self):
|
|
|
|
res = self.client.post('/edit/authors/create/interpolate_redirect/',
|
|
|
|
{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
|
|
|
|
self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
|
|
|
|
self.assertEqual(res.status_code, 302)
|
|
|
|
pk = Author.objects.all()[0].pk
|
|
|
|
self.assertRedirects(res, 'http://testserver/edit/author/%d/update/' % pk)
|
|
|
|
|
2010-10-18 13:34:47 +00:00
|
|
|
def test_create_with_special_properties(self):
|
|
|
|
res = self.client.get('/edit/authors/create/special/')
|
|
|
|
self.assertEqual(res.status_code, 200)
|
2013-05-21 09:42:15 +00:00
|
|
|
self.assertIsInstance(res.context['form'], views.AuthorForm)
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertNotIn('object', res.context)
|
|
|
|
self.assertNotIn('author', res.context)
|
2010-10-18 13:34:47 +00:00
|
|
|
self.assertTemplateUsed(res, 'generic_views/form.html')
|
|
|
|
|
|
|
|
res = self.client.post('/edit/authors/create/special/',
|
|
|
|
{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
|
|
|
|
self.assertEqual(res.status_code, 302)
|
2010-10-18 15:53:08 +00:00
|
|
|
obj = Author.objects.get(slug='randall-munroe')
|
|
|
|
self.assertRedirects(res, reverse('author_detail', kwargs={'pk': obj.pk}))
|
2010-10-18 13:34:47 +00:00
|
|
|
self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
|
|
|
|
|
|
|
|
def test_create_without_redirect(self):
|
|
|
|
try:
|
2013-10-19 12:31:38 +00:00
|
|
|
self.client.post('/edit/authors/create/naive/',
|
|
|
|
{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
|
2010-10-18 13:34:47 +00:00
|
|
|
self.fail('Should raise exception -- No redirect URL provided, and no get_absolute_url provided')
|
|
|
|
except ImproperlyConfigured:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_create_restricted(self):
|
|
|
|
res = self.client.post('/edit/authors/create/restricted/',
|
2013-10-19 12:31:38 +00:00
|
|
|
{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
|
2010-10-18 13:34:47 +00:00
|
|
|
self.assertEqual(res.status_code, 302)
|
|
|
|
self.assertRedirects(res, 'http://testserver/accounts/login/?next=/edit/authors/create/restricted/')
|
|
|
|
|
2013-02-21 21:56:55 +00:00
|
|
|
def test_create_view_with_restricted_fields(self):
|
|
|
|
|
|
|
|
class MyCreateView(CreateView):
|
|
|
|
model = Author
|
|
|
|
fields = ['name']
|
|
|
|
|
|
|
|
self.assertEqual(list(MyCreateView().get_form_class().base_fields),
|
|
|
|
['name'])
|
|
|
|
|
|
|
|
def test_create_view_all_fields(self):
|
2014-03-22 00:44:34 +00:00
|
|
|
class MyCreateView(CreateView):
|
|
|
|
model = Author
|
|
|
|
fields = '__all__'
|
2013-02-21 21:56:55 +00:00
|
|
|
|
2014-03-22 00:44:34 +00:00
|
|
|
self.assertEqual(list(MyCreateView().get_form_class().base_fields),
|
|
|
|
['name', 'slug'])
|
2013-02-21 21:56:55 +00:00
|
|
|
|
|
|
|
def test_create_view_without_explicit_fields(self):
|
2014-03-22 00:44:34 +00:00
|
|
|
class MyCreateView(CreateView):
|
|
|
|
model = Author
|
2013-02-21 21:56:55 +00:00
|
|
|
|
2014-03-22 00:44:34 +00:00
|
|
|
message = (
|
|
|
|
"Using ModelFormMixin (base class of MyCreateView) without the "
|
|
|
|
"'fields' attribute is prohibited."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, message):
|
|
|
|
MyCreateView().get_form_class()
|
2013-02-21 21:56:55 +00:00
|
|
|
|
2012-08-18 13:46:17 +00:00
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='generic_views.urls')
|
2010-10-18 13:34:47 +00:00
|
|
|
class UpdateViewTests(TestCase):
|
|
|
|
|
|
|
|
def test_update_post(self):
|
|
|
|
a = Author.objects.create(
|
|
|
|
name='Randall Munroe',
|
|
|
|
slug='randall-munroe',
|
|
|
|
)
|
2010-10-18 15:53:08 +00:00
|
|
|
res = self.client.get('/edit/author/%d/update/' % a.pk)
|
2010-10-18 13:34:47 +00:00
|
|
|
self.assertEqual(res.status_code, 200)
|
2013-05-21 09:42:15 +00:00
|
|
|
self.assertIsInstance(res.context['form'], forms.ModelForm)
|
2010-10-18 15:53:08 +00:00
|
|
|
self.assertEqual(res.context['object'], Author.objects.get(pk=a.pk))
|
|
|
|
self.assertEqual(res.context['author'], Author.objects.get(pk=a.pk))
|
2010-10-18 13:34:47 +00:00
|
|
|
self.assertTemplateUsed(res, 'generic_views/author_form.html')
|
|
|
|
|
|
|
|
# Modification with both POST and PUT (browser compatible)
|
2010-10-18 15:53:08 +00:00
|
|
|
res = self.client.post('/edit/author/%d/update/' % a.pk,
|
2010-10-18 13:34:47 +00:00
|
|
|
{'name': 'Randall Munroe (xkcd)', 'slug': 'randall-munroe'})
|
|
|
|
self.assertEqual(res.status_code, 302)
|
|
|
|
self.assertRedirects(res, 'http://testserver/list/authors/')
|
|
|
|
self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (xkcd)>'])
|
|
|
|
|
|
|
|
@expectedFailure
|
|
|
|
def test_update_put(self):
|
|
|
|
a = Author.objects.create(
|
|
|
|
name='Randall Munroe',
|
|
|
|
slug='randall-munroe',
|
|
|
|
)
|
2010-10-18 15:53:08 +00:00
|
|
|
res = self.client.get('/edit/author/%d/update/' % a.pk)
|
2010-10-18 13:34:47 +00:00
|
|
|
self.assertEqual(res.status_code, 200)
|
|
|
|
self.assertTemplateUsed(res, 'generic_views/author_form.html')
|
|
|
|
|
2010-10-18 15:53:08 +00:00
|
|
|
res = self.client.put('/edit/author/%d/update/' % a.pk,
|
2010-10-18 13:34:47 +00:00
|
|
|
{'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'})
|
2012-03-13 16:33:56 +00:00
|
|
|
# Here is the expected failure. PUT data are not processed in any special
|
|
|
|
# way by django. So the request will equal to a POST without data, hence
|
|
|
|
# the form will be invalid and redisplayed with errors (status code 200).
|
|
|
|
# See also #12635
|
2010-10-18 13:34:47 +00:00
|
|
|
self.assertEqual(res.status_code, 302)
|
|
|
|
self.assertRedirects(res, 'http://testserver/list/authors/')
|
|
|
|
self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>'])
|
|
|
|
|
|
|
|
def test_update_invalid(self):
|
|
|
|
a = Author.objects.create(
|
|
|
|
name='Randall Munroe',
|
|
|
|
slug='randall-munroe',
|
|
|
|
)
|
2010-10-18 15:53:08 +00:00
|
|
|
res = self.client.post('/edit/author/%d/update/' % a.pk,
|
2010-10-18 13:34:47 +00:00
|
|
|
{'name': 'A' * 101, 'slug': 'randall-munroe'})
|
|
|
|
self.assertEqual(res.status_code, 200)
|
|
|
|
self.assertTemplateUsed(res, 'generic_views/author_form.html')
|
|
|
|
self.assertEqual(len(res.context['form'].errors), 1)
|
|
|
|
self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
|
|
|
|
|
|
|
|
def test_update_with_object_url(self):
|
|
|
|
a = Artist.objects.create(name='Rene Magritte')
|
2010-10-18 15:53:08 +00:00
|
|
|
res = self.client.post('/edit/artists/%d/update/' % a.pk,
|
2010-10-18 13:34:47 +00:00
|
|
|
{'name': 'Rene Magritte'})
|
|
|
|
self.assertEqual(res.status_code, 302)
|
2010-10-18 15:53:08 +00:00
|
|
|
self.assertRedirects(res, 'http://testserver/detail/artist/%d/' % a.pk)
|
2010-10-18 13:34:47 +00:00
|
|
|
self.assertQuerysetEqual(Artist.objects.all(), ['<Artist: Rene Magritte>'])
|
|
|
|
|
|
|
|
def test_update_with_redirect(self):
|
|
|
|
a = Author.objects.create(
|
|
|
|
name='Randall Munroe',
|
|
|
|
slug='randall-munroe',
|
|
|
|
)
|
2010-10-18 15:53:08 +00:00
|
|
|
res = self.client.post('/edit/author/%d/update/redirect/' % a.pk,
|
2010-10-18 13:34:47 +00:00
|
|
|
{'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'})
|
|
|
|
self.assertEqual(res.status_code, 302)
|
|
|
|
self.assertRedirects(res, 'http://testserver/edit/authors/create/')
|
|
|
|
self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>'])
|
|
|
|
|
2010-12-04 11:20:30 +00:00
|
|
|
def test_update_with_interpolated_redirect(self):
|
|
|
|
a = Author.objects.create(
|
|
|
|
name='Randall Munroe',
|
|
|
|
slug='randall-munroe',
|
|
|
|
)
|
|
|
|
res = self.client.post('/edit/author/%d/update/interpolate_redirect/' % a.pk,
|
|
|
|
{'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'})
|
|
|
|
self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>'])
|
|
|
|
self.assertEqual(res.status_code, 302)
|
|
|
|
pk = Author.objects.all()[0].pk
|
|
|
|
self.assertRedirects(res, 'http://testserver/edit/author/%d/update/' % pk)
|
|
|
|
|
2010-10-18 13:34:47 +00:00
|
|
|
def test_update_with_special_properties(self):
|
|
|
|
a = Author.objects.create(
|
|
|
|
name='Randall Munroe',
|
|
|
|
slug='randall-munroe',
|
|
|
|
)
|
2010-10-18 15:53:08 +00:00
|
|
|
res = self.client.get('/edit/author/%d/update/special/' % a.pk)
|
2010-10-18 13:34:47 +00:00
|
|
|
self.assertEqual(res.status_code, 200)
|
2013-05-21 09:42:15 +00:00
|
|
|
self.assertIsInstance(res.context['form'], views.AuthorForm)
|
2010-10-18 15:53:08 +00:00
|
|
|
self.assertEqual(res.context['object'], Author.objects.get(pk=a.pk))
|
|
|
|
self.assertEqual(res.context['thingy'], Author.objects.get(pk=a.pk))
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertNotIn('author', res.context)
|
2010-10-18 13:34:47 +00:00
|
|
|
self.assertTemplateUsed(res, 'generic_views/form.html')
|
|
|
|
|
2010-10-18 15:53:08 +00:00
|
|
|
res = self.client.post('/edit/author/%d/update/special/' % a.pk,
|
2010-10-18 13:34:47 +00:00
|
|
|
{'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'})
|
|
|
|
self.assertEqual(res.status_code, 302)
|
2010-10-18 15:53:08 +00:00
|
|
|
self.assertRedirects(res, 'http://testserver/detail/author/%d/' % a.pk)
|
2010-10-18 13:34:47 +00:00
|
|
|
self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>'])
|
|
|
|
|
|
|
|
def test_update_without_redirect(self):
|
2013-10-19 12:31:38 +00:00
|
|
|
a = Author.objects.create(
|
|
|
|
name='Randall Munroe',
|
|
|
|
slug='randall-munroe',
|
|
|
|
)
|
|
|
|
# Should raise exception -- No redirect URL provided, and no
|
|
|
|
# get_absolute_url provided
|
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
self.client.post('/edit/author/%d/update/naive/' % a.pk,
|
2010-10-18 13:34:47 +00:00
|
|
|
{'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'})
|
|
|
|
|
2011-02-15 08:12:29 +00:00
|
|
|
def test_update_get_object(self):
|
|
|
|
a = Author.objects.create(
|
2011-03-03 23:54:12 +00:00
|
|
|
pk=1,
|
2011-02-15 08:12:29 +00:00
|
|
|
name='Randall Munroe',
|
|
|
|
slug='randall-munroe',
|
|
|
|
)
|
|
|
|
res = self.client.get('/edit/author/update/')
|
|
|
|
self.assertEqual(res.status_code, 200)
|
2013-05-21 09:42:15 +00:00
|
|
|
self.assertIsInstance(res.context['form'], forms.ModelForm)
|
|
|
|
self.assertIsInstance(res.context['view'], View)
|
2011-02-15 08:12:29 +00:00
|
|
|
self.assertEqual(res.context['object'], Author.objects.get(pk=a.pk))
|
|
|
|
self.assertEqual(res.context['author'], Author.objects.get(pk=a.pk))
|
|
|
|
self.assertTemplateUsed(res, 'generic_views/author_form.html')
|
|
|
|
|
|
|
|
# Modification with both POST and PUT (browser compatible)
|
|
|
|
res = self.client.post('/edit/author/update/',
|
|
|
|
{'name': 'Randall Munroe (xkcd)', 'slug': 'randall-munroe'})
|
|
|
|
self.assertEqual(res.status_code, 302)
|
|
|
|
self.assertRedirects(res, 'http://testserver/list/authors/')
|
|
|
|
self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (xkcd)>'])
|
|
|
|
|
2012-08-18 13:46:17 +00:00
|
|
|
|
2014-04-05 06:04:46 +00:00
|
|
|
@override_settings(ROOT_URLCONF='generic_views.urls')
|
2010-10-18 13:34:47 +00:00
|
|
|
class DeleteViewTests(TestCase):
|
|
|
|
|
|
|
|
def test_delete_by_post(self):
|
2010-10-18 15:53:08 +00:00
|
|
|
a = Author.objects.create(**{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
|
|
|
|
res = self.client.get('/edit/author/%d/delete/' % a.pk)
|
2010-10-18 13:34:47 +00:00
|
|
|
self.assertEqual(res.status_code, 200)
|
2010-10-18 15:53:08 +00:00
|
|
|
self.assertEqual(res.context['object'], Author.objects.get(pk=a.pk))
|
|
|
|
self.assertEqual(res.context['author'], Author.objects.get(pk=a.pk))
|
2010-10-18 13:34:47 +00:00
|
|
|
self.assertTemplateUsed(res, 'generic_views/author_confirm_delete.html')
|
|
|
|
|
|
|
|
# Deletion with POST
|
2010-10-18 15:53:08 +00:00
|
|
|
res = self.client.post('/edit/author/%d/delete/' % a.pk)
|
2010-10-18 13:34:47 +00:00
|
|
|
self.assertEqual(res.status_code, 302)
|
|
|
|
self.assertRedirects(res, 'http://testserver/list/authors/')
|
|
|
|
self.assertQuerysetEqual(Author.objects.all(), [])
|
|
|
|
|
|
|
|
def test_delete_by_delete(self):
|
|
|
|
# Deletion with browser compatible DELETE method
|
2010-10-18 15:53:08 +00:00
|
|
|
a = Author.objects.create(**{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
|
|
|
|
res = self.client.delete('/edit/author/%d/delete/' % a.pk)
|
2010-10-18 13:34:47 +00:00
|
|
|
self.assertEqual(res.status_code, 302)
|
|
|
|
self.assertRedirects(res, 'http://testserver/list/authors/')
|
|
|
|
self.assertQuerysetEqual(Author.objects.all(), [])
|
|
|
|
|
|
|
|
def test_delete_with_redirect(self):
|
2010-10-18 15:53:08 +00:00
|
|
|
a = Author.objects.create(**{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
|
|
|
|
res = self.client.post('/edit/author/%d/delete/redirect/' % a.pk)
|
2010-10-18 13:34:47 +00:00
|
|
|
self.assertEqual(res.status_code, 302)
|
|
|
|
self.assertRedirects(res, 'http://testserver/edit/authors/create/')
|
|
|
|
self.assertQuerysetEqual(Author.objects.all(), [])
|
|
|
|
|
2013-02-11 07:39:14 +00:00
|
|
|
def test_delete_with_interpolated_redirect(self):
|
|
|
|
a = Author.objects.create(**{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
|
|
|
|
res = self.client.post('/edit/author/%d/delete/interpolate_redirect/' % a.pk)
|
|
|
|
self.assertEqual(res.status_code, 302)
|
|
|
|
self.assertRedirects(res, 'http://testserver/edit/authors/create/?deleted=%d' % a.pk)
|
|
|
|
self.assertQuerysetEqual(Author.objects.all(), [])
|
|
|
|
|
2010-10-18 13:34:47 +00:00
|
|
|
def test_delete_with_special_properties(self):
|
2010-10-18 15:53:08 +00:00
|
|
|
a = Author.objects.create(**{'name': 'Randall Munroe', 'slug': 'randall-munroe'})
|
|
|
|
res = self.client.get('/edit/author/%d/delete/special/' % a.pk)
|
2010-10-18 13:34:47 +00:00
|
|
|
self.assertEqual(res.status_code, 200)
|
2010-10-18 15:53:08 +00:00
|
|
|
self.assertEqual(res.context['object'], Author.objects.get(pk=a.pk))
|
|
|
|
self.assertEqual(res.context['thingy'], Author.objects.get(pk=a.pk))
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertNotIn('author', res.context)
|
2010-10-18 13:34:47 +00:00
|
|
|
self.assertTemplateUsed(res, 'generic_views/confirm_delete.html')
|
|
|
|
|
2010-10-18 15:53:08 +00:00
|
|
|
res = self.client.post('/edit/author/%d/delete/special/' % a.pk)
|
2010-10-18 13:34:47 +00:00
|
|
|
self.assertEqual(res.status_code, 302)
|
|
|
|
self.assertRedirects(res, 'http://testserver/list/authors/')
|
|
|
|
self.assertQuerysetEqual(Author.objects.all(), [])
|
|
|
|
|
|
|
|
def test_delete_without_redirect(self):
|
2013-10-19 12:31:38 +00:00
|
|
|
a = Author.objects.create(
|
|
|
|
name='Randall Munroe',
|
|
|
|
slug='randall-munroe',
|
|
|
|
)
|
|
|
|
# Should raise exception -- No redirect URL provided, and no
|
|
|
|
# get_absolute_url provided
|
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
self.client.post('/edit/author/%d/delete/naive/' % a.pk)
|