PageRenderTime 40ms CodeModel.GetById 13ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/admin_inlines/tests.py

https://code.google.com/p/mango-py/
Python | 157 lines | 137 code | 10 blank | 10 comment | 0 complexity | cf95380694e7af87cad78b5769998bd8 MD5 | raw file
  1from django.contrib.admin.helpers import InlineAdminForm
  2from django.contrib.contenttypes.models import ContentType
  3from django.test import TestCase
  4
  5# local test models
  6from models import (Holder, Inner, InnerInline, Holder2, Inner2, Holder3,
  7    Inner3, Person, OutfitItem, Fashionista, Teacher, Parent, Child)
  8
  9
 10class TestInline(TestCase):
 11    fixtures = ['admin-views-users.xml']
 12
 13    def setUp(self):
 14        holder = Holder(dummy=13)
 15        holder.save()
 16        Inner(dummy=42, holder=holder).save()
 17        self.change_url = '/test_admin/admin/admin_inlines/holder/%i/' % holder.id
 18
 19        result = self.client.login(username='super', password='secret')
 20        self.assertEqual(result, True)
 21
 22    def tearDown(self):
 23        self.client.logout()
 24
 25    def test_can_delete(self):
 26        """
 27        can_delete should be passed to inlineformset factory.
 28        """
 29        response = self.client.get(self.change_url)
 30        inner_formset = response.context[-1]['inline_admin_formsets'][0].formset
 31        expected = InnerInline.can_delete
 32        actual = inner_formset.can_delete
 33        self.assertEqual(expected, actual, 'can_delete must be equal')
 34
 35    def test_readonly_stacked_inline_label(self):
 36        """Bug #13174."""
 37        holder = Holder.objects.create(dummy=42)
 38        inner = Inner.objects.create(holder=holder, dummy=42, readonly='')
 39        response = self.client.get('/test_admin/admin/admin_inlines/holder/%i/'
 40                                   % holder.id)
 41        self.assertContains(response, '<label>Inner readonly label:</label>')
 42
 43    def test_many_to_many_inlines(self):
 44        "Autogenerated many-to-many inlines are displayed correctly (#13407)"
 45        response = self.client.get('/test_admin/admin/admin_inlines/author/add/')
 46        # The heading for the m2m inline block uses the right text
 47        self.assertContains(response, '<h2>Author-book relationships</h2>')
 48        # The "add another" label is correct
 49        self.assertContains(response, 'Add another Author-Book Relationship')
 50        # The '+' is dropped from the autogenerated form prefix (Author_books+)
 51        self.assertContains(response, 'id="id_Author_books-TOTAL_FORMS"')
 52
 53    def test_inline_primary(self):
 54        person = Person.objects.create(firstname='Imelda')
 55        item = OutfitItem.objects.create(name='Shoes')
 56        # Imelda likes shoes, but can't cary her own bags.
 57        data = {
 58            'shoppingweakness_set-TOTAL_FORMS': 1,
 59            'shoppingweakness_set-INITIAL_FORMS': 0,
 60            'shoppingweakness_set-MAX_NUM_FORMS': 0,
 61            '_save': u'Save',
 62            'person': person.id,
 63            'max_weight': 0,
 64            'shoppingweakness_set-0-item': item.id,
 65        }
 66        response = self.client.post('/test_admin/admin/admin_inlines/fashionista/add/', data)
 67        self.assertEqual(response.status_code, 302)
 68        self.assertEqual(len(Fashionista.objects.filter(person__firstname='Imelda')), 1)
 69
 70    def test_tabular_non_field_errors(self):
 71        """
 72        Ensure that non_field_errors are displayed correctly, including the
 73        right value for colspan. Refs #13510.
 74        """
 75        data = {
 76            'title_set-TOTAL_FORMS': 1,
 77            'title_set-INITIAL_FORMS': 0,
 78            'title_set-MAX_NUM_FORMS': 0,
 79            '_save': u'Save',
 80            'title_set-0-title1': 'a title',
 81            'title_set-0-title2': 'a different title',
 82        }
 83        response = self.client.post('/test_admin/admin/admin_inlines/titlecollection/add/', data)
 84        # Here colspan is "4": two fields (title1 and title2), one hidden field and the delete checkbock.
 85        self.assertContains(response, '<tr><td colspan="4"><ul class="errorlist"><li>The two titles must be the same</li></ul></td></tr>')
 86
 87    def test_no_parent_callable_lookup(self):
 88        """Admin inline `readonly_field` shouldn't invoke parent ModelAdmin callable"""
 89        # Identically named callable isn't present in the parent ModelAdmin,
 90        # rendering of the add view shouldn't explode
 91        response = self.client.get('/test_admin/admin/admin_inlines/novel/add/')
 92        self.assertEqual(response.status_code, 200)
 93        # View should have the child inlines section
 94        self.assertContains(response, '<div class="inline-group" id="chapter_set-group">')
 95
 96    def test_callable_lookup(self):
 97        """Admin inline should invoke local callable when its name is listed in readonly_fields"""
 98        response = self.client.get('/test_admin/admin/admin_inlines/poll/add/')
 99        self.assertEqual(response.status_code, 200)
100        # Add parent object view should have the child inlines section
101        self.assertContains(response, '<div class="inline-group" id="question_set-group">')
102        # The right callabe should be used for the inline readonly_fields
103        # column cells
104        self.assertContains(response, '<p>Callable in QuestionInline</p>')
105
106class TestInlineMedia(TestCase):
107    fixtures = ['admin-views-users.xml']
108
109    def setUp(self):
110
111        result = self.client.login(username='super', password='secret')
112        self.assertEqual(result, True)
113
114    def tearDown(self):
115        self.client.logout()
116
117    def test_inline_media_only_base(self):
118        holder = Holder(dummy=13)
119        holder.save()
120        Inner(dummy=42, holder=holder).save()
121        change_url = '/test_admin/admin/admin_inlines/holder/%i/' % holder.id
122        response = self.client.get(change_url)
123        self.assertContains(response, 'my_awesome_admin_scripts.js')
124
125    def test_inline_media_only_inline(self):
126        holder = Holder3(dummy=13)
127        holder.save()
128        Inner3(dummy=42, holder=holder).save()
129        change_url = '/test_admin/admin/admin_inlines/holder3/%i/' % holder.id
130        response = self.client.get(change_url)
131        self.assertContains(response, 'my_awesome_inline_scripts.js')
132
133    def test_all_inline_media(self):
134        holder = Holder2(dummy=13)
135        holder.save()
136        Inner2(dummy=42, holder=holder).save()
137        change_url = '/test_admin/admin/admin_inlines/holder2/%i/' % holder.id
138        response = self.client.get(change_url)
139        self.assertContains(response, 'my_awesome_admin_scripts.js')
140        self.assertContains(response, 'my_awesome_inline_scripts.js')
141
142class TestInlineAdminForm(TestCase):
143
144    def test_immutable_content_type(self):
145        """Regression for #9362
146        The problem depends only on InlineAdminForm and its "original"
147        argument, so we can safely set the other arguments to None/{}. We just
148        need to check that the content_type argument of Child isn't altered by
149        the internals of the inline form."""
150
151        sally = Teacher.objects.create(name='Sally')
152        john = Parent.objects.create(name='John')
153        joe = Child.objects.create(name='Joe', teacher=sally, parent=john)
154
155        iaf = InlineAdminForm(None, None, {}, {}, joe)
156        parent_ct = ContentType.objects.get_for_model(Parent)
157        self.assertEqual(iaf.original.content_type, parent_ct)