|
1 # coding: utf-8 |
|
2 |
|
3 from django.conf import settings |
|
4 from django.contrib.contenttypes.generic import generic_inlineformset_factory |
|
5 from django.test import TestCase |
|
6 |
|
7 # local test models |
|
8 from models import Episode, EpisodeExtra, EpisodeMaxNum, EpisodeExclude, \ |
|
9 Media, EpisodePermanent, MediaPermanentInline |
|
10 |
|
11 |
|
12 class GenericAdminViewTest(TestCase): |
|
13 fixtures = ['users.xml'] |
|
14 |
|
15 def setUp(self): |
|
16 # set TEMPLATE_DEBUG to True to ensure {% include %} will raise |
|
17 # exceptions since that is how inlines are rendered and #9498 will |
|
18 # bubble up if it is an issue. |
|
19 self.original_template_debug = settings.TEMPLATE_DEBUG |
|
20 settings.TEMPLATE_DEBUG = True |
|
21 self.client.login(username='super', password='secret') |
|
22 |
|
23 # Can't load content via a fixture (since the GenericForeignKey |
|
24 # relies on content type IDs, which will vary depending on what |
|
25 # other tests have been run), thus we do it here. |
|
26 e = Episode.objects.create(name='This Week in Django') |
|
27 self.episode_pk = e.pk |
|
28 m = Media(content_object=e, url='http://example.com/podcast.mp3') |
|
29 m.save() |
|
30 self.mp3_media_pk = m.pk |
|
31 |
|
32 m = Media(content_object=e, url='http://example.com/logo.png') |
|
33 m.save() |
|
34 self.png_media_pk = m.pk |
|
35 |
|
36 def tearDown(self): |
|
37 self.client.logout() |
|
38 settings.TEMPLATE_DEBUG = self.original_template_debug |
|
39 |
|
40 def testBasicAddGet(self): |
|
41 """ |
|
42 A smoke test to ensure GET on the add_view works. |
|
43 """ |
|
44 response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episode/add/') |
|
45 self.assertEqual(response.status_code, 200) |
|
46 |
|
47 def testBasicEditGet(self): |
|
48 """ |
|
49 A smoke test to ensure GET on the change_view works. |
|
50 """ |
|
51 response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episode/%d/' % self.episode_pk) |
|
52 self.assertEqual(response.status_code, 200) |
|
53 |
|
54 def testBasicAddPost(self): |
|
55 """ |
|
56 A smoke test to ensure POST on add_view works. |
|
57 """ |
|
58 post_data = { |
|
59 "name": u"This Week in Django", |
|
60 # inline data |
|
61 "generic_inline_admin-media-content_type-object_id-TOTAL_FORMS": u"1", |
|
62 "generic_inline_admin-media-content_type-object_id-INITIAL_FORMS": u"0", |
|
63 "generic_inline_admin-media-content_type-object_id-MAX_NUM_FORMS": u"0", |
|
64 } |
|
65 response = self.client.post('/generic_inline_admin/admin/generic_inline_admin/episode/add/', post_data) |
|
66 self.assertEqual(response.status_code, 302) # redirect somewhere |
|
67 |
|
68 def testBasicEditPost(self): |
|
69 """ |
|
70 A smoke test to ensure POST on edit_view works. |
|
71 """ |
|
72 post_data = { |
|
73 "name": u"This Week in Django", |
|
74 # inline data |
|
75 "generic_inline_admin-media-content_type-object_id-TOTAL_FORMS": u"3", |
|
76 "generic_inline_admin-media-content_type-object_id-INITIAL_FORMS": u"2", |
|
77 "generic_inline_admin-media-content_type-object_id-MAX_NUM_FORMS": u"0", |
|
78 "generic_inline_admin-media-content_type-object_id-0-id": u"%d" % self.mp3_media_pk, |
|
79 "generic_inline_admin-media-content_type-object_id-0-url": u"http://example.com/podcast.mp3", |
|
80 "generic_inline_admin-media-content_type-object_id-1-id": u"%d" % self.png_media_pk, |
|
81 "generic_inline_admin-media-content_type-object_id-1-url": u"http://example.com/logo.png", |
|
82 "generic_inline_admin-media-content_type-object_id-2-id": u"", |
|
83 "generic_inline_admin-media-content_type-object_id-2-url": u"", |
|
84 } |
|
85 url = '/generic_inline_admin/admin/generic_inline_admin/episode/%d/' % self.episode_pk |
|
86 response = self.client.post(url, post_data) |
|
87 self.assertEqual(response.status_code, 302) # redirect somewhere |
|
88 |
|
89 def testGenericInlineFormset(self): |
|
90 EpisodeMediaFormSet = generic_inlineformset_factory(Media, can_delete=False, extra=3) |
|
91 e = Episode.objects.get(name='This Week in Django') |
|
92 |
|
93 # Works with no queryset |
|
94 formset = EpisodeMediaFormSet(instance=e) |
|
95 self.assertEquals(len(formset.forms), 5) |
|
96 self.assertEquals(formset.forms[0].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-0-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-0-url" type="text" name="generic_inline_admin-media-content_type-object_id-0-url" value="http://example.com/podcast.mp3" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-0-id" value="%s" id="id_generic_inline_admin-media-content_type-object_id-0-id" /></p>' % self.mp3_media_pk) |
|
97 self.assertEquals(formset.forms[1].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-1-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-1-url" type="text" name="generic_inline_admin-media-content_type-object_id-1-url" value="http://example.com/logo.png" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-1-id" value="%s" id="id_generic_inline_admin-media-content_type-object_id-1-id" /></p>' % self.png_media_pk) |
|
98 self.assertEquals(formset.forms[2].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-2-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-2-url" type="text" name="generic_inline_admin-media-content_type-object_id-2-url" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-2-id" id="id_generic_inline_admin-media-content_type-object_id-2-id" /></p>') |
|
99 |
|
100 # A queryset can be used to alter display ordering |
|
101 formset = EpisodeMediaFormSet(instance=e, queryset=Media.objects.order_by('url')) |
|
102 self.assertEquals(len(formset.forms), 5) |
|
103 self.assertEquals(formset.forms[0].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-0-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-0-url" type="text" name="generic_inline_admin-media-content_type-object_id-0-url" value="http://example.com/logo.png" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-0-id" value="%s" id="id_generic_inline_admin-media-content_type-object_id-0-id" /></p>' % self.png_media_pk) |
|
104 self.assertEquals(formset.forms[1].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-1-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-1-url" type="text" name="generic_inline_admin-media-content_type-object_id-1-url" value="http://example.com/podcast.mp3" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-1-id" value="%s" id="id_generic_inline_admin-media-content_type-object_id-1-id" /></p>' % self.mp3_media_pk) |
|
105 self.assertEquals(formset.forms[2].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-2-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-2-url" type="text" name="generic_inline_admin-media-content_type-object_id-2-url" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-2-id" id="id_generic_inline_admin-media-content_type-object_id-2-id" /></p>') |
|
106 |
|
107 |
|
108 # Works with a queryset that omits items |
|
109 formset = EpisodeMediaFormSet(instance=e, queryset=Media.objects.filter(url__endswith=".png")) |
|
110 self.assertEquals(len(formset.forms), 4) |
|
111 self.assertEquals(formset.forms[0].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-0-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-0-url" type="text" name="generic_inline_admin-media-content_type-object_id-0-url" value="http://example.com/logo.png" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-0-id" value="%s" id="id_generic_inline_admin-media-content_type-object_id-0-id" /></p>' % self.png_media_pk) |
|
112 self.assertEquals(formset.forms[1].as_p(), '<p><label for="id_generic_inline_admin-media-content_type-object_id-1-url">Url:</label> <input id="id_generic_inline_admin-media-content_type-object_id-1-url" type="text" name="generic_inline_admin-media-content_type-object_id-1-url" maxlength="200" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-1-id" id="id_generic_inline_admin-media-content_type-object_id-1-id" /></p>') |
|
113 |
|
114 def testGenericInlineFormsetFactory(self): |
|
115 # Regression test for #10522. |
|
116 inline_formset = generic_inlineformset_factory(Media, |
|
117 exclude=('url',)) |
|
118 |
|
119 # Regression test for #12340. |
|
120 e = Episode.objects.get(name='This Week in Django') |
|
121 formset = inline_formset(instance=e) |
|
122 self.assertTrue(formset.get_queryset().ordered) |
|
123 |
|
124 class GenericInlineAdminParametersTest(TestCase): |
|
125 fixtures = ['users.xml'] |
|
126 |
|
127 def setUp(self): |
|
128 self.client.login(username='super', password='secret') |
|
129 |
|
130 def tearDown(self): |
|
131 self.client.logout() |
|
132 |
|
133 def _create_object(self, model): |
|
134 """ |
|
135 Create a model with an attached Media object via GFK. We can't |
|
136 load content via a fixture (since the GenericForeignKey relies on |
|
137 content type IDs, which will vary depending on what other tests |
|
138 have been run), thus we do it here. |
|
139 """ |
|
140 e = model.objects.create(name='This Week in Django') |
|
141 Media.objects.create(content_object=e, url='http://example.com/podcast.mp3') |
|
142 return e |
|
143 |
|
144 def testNoParam(self): |
|
145 """ |
|
146 With one initial form, extra (default) at 3, there should be 4 forms. |
|
147 """ |
|
148 e = self._create_object(Episode) |
|
149 response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episode/%s/' % e.pk) |
|
150 formset = response.context['inline_admin_formsets'][0].formset |
|
151 self.assertEqual(formset.total_form_count(), 4) |
|
152 self.assertEqual(formset.initial_form_count(), 1) |
|
153 |
|
154 def testExtraParam(self): |
|
155 """ |
|
156 With extra=0, there should be one form. |
|
157 """ |
|
158 e = self._create_object(EpisodeExtra) |
|
159 response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episodeextra/%s/' % e.pk) |
|
160 formset = response.context['inline_admin_formsets'][0].formset |
|
161 self.assertEqual(formset.total_form_count(), 1) |
|
162 self.assertEqual(formset.initial_form_count(), 1) |
|
163 |
|
164 def testMaxNumParam(self): |
|
165 """ |
|
166 With extra=5 and max_num=2, there should be only 2 forms. |
|
167 """ |
|
168 e = self._create_object(EpisodeMaxNum) |
|
169 inline_form_data = '<input type="hidden" name="generic_inline_admin-media-content_type-object_id-TOTAL_FORMS" value="2" id="id_generic_inline_admin-media-content_type-object_id-TOTAL_FORMS" /><input type="hidden" name="generic_inline_admin-media-content_type-object_id-INITIAL_FORMS" value="1" id="id_generic_inline_admin-media-content_type-object_id-INITIAL_FORMS" />' |
|
170 response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episodemaxnum/%s/' % e.pk) |
|
171 formset = response.context['inline_admin_formsets'][0].formset |
|
172 self.assertEqual(formset.total_form_count(), 2) |
|
173 self.assertEqual(formset.initial_form_count(), 1) |
|
174 |
|
175 def testExcludeParam(self): |
|
176 """ |
|
177 Generic inline formsets should respect include. |
|
178 """ |
|
179 e = self._create_object(EpisodeExclude) |
|
180 response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episodeexclude/%s/' % e.pk) |
|
181 formset = response.context['inline_admin_formsets'][0].formset |
|
182 self.assertFalse('url' in formset.forms[0], 'The formset has excluded "url" field.') |
|
183 |
|
184 class GenericInlineAdminWithUniqueTogetherTest(TestCase): |
|
185 fixtures = ['users.xml'] |
|
186 |
|
187 def setUp(self): |
|
188 self.client.login(username='super', password='secret') |
|
189 |
|
190 def tearDown(self): |
|
191 self.client.logout() |
|
192 |
|
193 def testAdd(self): |
|
194 post_data = { |
|
195 "name": u"John Doe", |
|
196 # inline data |
|
197 "generic_inline_admin-phonenumber-content_type-object_id-TOTAL_FORMS": u"1", |
|
198 "generic_inline_admin-phonenumber-content_type-object_id-INITIAL_FORMS": u"0", |
|
199 "generic_inline_admin-phonenumber-content_type-object_id-MAX_NUM_FORMS": u"0", |
|
200 "generic_inline_admin-phonenumber-content_type-object_id-0-id": "", |
|
201 "generic_inline_admin-phonenumber-content_type-object_id-0-phone_number": "555-555-5555", |
|
202 } |
|
203 response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/contact/add/') |
|
204 self.assertEqual(response.status_code, 200) |
|
205 response = self.client.post('/generic_inline_admin/admin/generic_inline_admin/contact/add/', post_data) |
|
206 self.assertEqual(response.status_code, 302) # redirect somewhere |
|
207 |
|
208 class NoInlineDeletionTest(TestCase): |
|
209 def test_no_deletion(self): |
|
210 fake_site = object() |
|
211 inline = MediaPermanentInline(EpisodePermanent, fake_site) |
|
212 fake_request = object() |
|
213 formset = inline.get_formset(fake_request) |
|
214 self.assertFalse(formset.can_delete) |