Code

Ticket #12561: patch-12561-1.diff

File patch-12561-1.diff, 5.2 KB (added by nessita, 4 years ago)

Improved the tests. Added default values on parent class for can_delete and can_order. Ran the whole test suite, all green.

Line 
1Index: django/contrib/admin/options.py
2===================================================================
3--- django/contrib/admin/options.py     (revision 12501)
4+++ django/contrib/admin/options.py     (working copy)
5@@ -1182,6 +1182,8 @@
6     template = None
7     verbose_name = None
8     verbose_name_plural = None
9+    can_delete = False
10+    can_order = False
11 
12     def __init__(self, parent_model, admin_site):
13         self.admin_site = admin_site
14@@ -1227,6 +1229,8 @@
15             "formfield_callback": curry(self.formfield_for_dbfield, request=request),
16             "extra": self.extra,
17             "max_num": self.max_num,
18+            "can_delete": self.can_delete,
19+            "can_order": self.can_order,
20         }
21         defaults.update(kwargs)
22         return inlineformset_factory(self.parent_model, self.model, **defaults)
23Index: tests/regressiontests/modeladmin/models.py
24===================================================================
25--- tests/regressiontests/modeladmin/models.py  (revision 12501)
26+++ tests/regressiontests/modeladmin/models.py  (working copy)
27@@ -291,6 +291,7 @@
28 ...     form = AdminConcertForm
29 ...     model = Concert
30 ...     fk_name = 'main_band'
31+...     can_delete = True
32 
33 >>> class BandAdmin(ModelAdmin):
34 ...     inlines = [
35Index: tests/regressiontests/admin_inlines/tests.py
36===================================================================
37--- tests/regressiontests/admin_inlines/tests.py        (revision 0)
38+++ tests/regressiontests/admin_inlines/tests.py        (revision 0)
39@@ -0,0 +1,41 @@
40+# coding: utf-8
41+
42+from django.test import TestCase
43+
44+# local test models
45+from models import Holder, Inner, InnerInline
46+
47+class TestInline(TestCase):
48+    fixtures = ['admin-views-users.xml']
49+
50+    def setUp(self):
51+        holder = Holder(dummy=13)
52+        holder.save()
53+        Inner(dummy=42, holder=holder).save()
54+        self.change_url = '/test_admin/admin/admin_inlines/holder/%i/' % holder.id
55+
56+        result = self.client.login(username='super', password='secret')
57+        self.failUnlessEqual(result, True)
58+
59+    def tearDown(self):
60+        self.client.logout()
61+
62+    def test_can_delete(self):
63+        """
64+        can_delete should be passed to inlineformset factory.
65+        """
66+        response = self.client.get(self.change_url)
67+        inner_formset = response.context[-1]['inline_admin_formsets'][0].formset
68+        expected = InnerInline.can_delete
69+        actual = inner_formset.can_delete
70+        self.assertEqual(expected, actual, 'can_delete must be equal')
71+
72+    def test_can_order(self):
73+        """
74+        can_order should be passed to inlineformset factory.
75+        """
76+        response = self.client.get(self.change_url)
77+        inner_formset = response.context[-1]['inline_admin_formsets'][0].formset
78+        expected = InnerInline.can_order
79+        actual = inner_formset.can_order
80+        self.assertEqual(expected, actual, 'can_order must be equal')
81Index: tests/regressiontests/admin_inlines/fixtures/admin-views-users.xml
82===================================================================
83--- tests/regressiontests/admin_inlines/fixtures/admin-views-users.xml  (revision 0)
84+++ tests/regressiontests/admin_inlines/fixtures/admin-views-users.xml  (revision 0)
85@@ -0,0 +1,17 @@
86+<?xml version="1.0" encoding="utf-8"?>
87+<django-objects version="1.0">
88+    <object pk="100" model="auth.user">
89+        <field type="CharField" name="username">super</field>
90+        <field type="CharField" name="first_name">Super</field>
91+        <field type="CharField" name="last_name">User</field>
92+        <field type="CharField" name="email">super@example.com</field>
93+        <field type="CharField" name="password">sha1$995a3$6011485ea3834267d719b4c801409b8b1ddd0158</field>
94+        <field type="BooleanField" name="is_staff">True</field>
95+        <field type="BooleanField" name="is_active">True</field>
96+        <field type="BooleanField" name="is_superuser">True</field>
97+        <field type="DateTimeField" name="last_login">2007-05-30 13:20:10</field>
98+        <field type="DateTimeField" name="date_joined">2007-05-30 13:20:10</field>
99+        <field to="auth.group" name="groups" rel="ManyToManyRel"></field>
100+        <field to="auth.permission" name="user_permissions" rel="ManyToManyRel"></field>
101+    </object>
102+</django-objects>
103Index: tests/regressiontests/admin_inlines/models.py
104===================================================================
105--- tests/regressiontests/admin_inlines/models.py       (revision 12501)
106+++ tests/regressiontests/admin_inlines/models.py       (working copy)
107@@ -3,6 +3,7 @@
108 
109 """
110 from django.db import models
111+from django.contrib import admin
112 from django.contrib.contenttypes.models import ContentType
113 from django.contrib.contenttypes import generic
114 
115@@ -29,6 +30,25 @@
116     def __unicode__(self):
117         return u'I am %s, a child of %s' % (self.name, self.parent)
118 
119+
120+class Holder(models.Model):
121+    dummy = models.IntegerField()
122+
123+
124+class Inner(models.Model):
125+    dummy = models.IntegerField()
126+    holder = models.ForeignKey(Holder)
127+
128+
129+class InnerInline(admin.StackedInline):
130+    model = Inner
131+    can_delete = False
132+    can_order = True
133+
134+
135+# Test bug #12561
136+admin.site.register(Holder, inlines=[InnerInline])
137+
138 __test__ = {'API_TESTS': """
139 
140 # Regression test for #9362