Code

Ticket #12749: 12749-tests.diff

File 12749-tests.diff, 2.6 KB (added by jkocherhans, 4 years ago)

Pull the code from nessita's patches into an extra test case for admin_inlines.

Line 
1diff --git a/tests/regressiontests/admin_inlines/models.py b/tests/regressiontests/admin_inlines/models.py
2index 303c06a..14f4339 100644
3--- a/tests/regressiontests/admin_inlines/models.py
4+++ b/tests/regressiontests/admin_inlines/models.py
5@@ -48,6 +48,30 @@ class InnerInline(admin.StackedInline):
6 # Test bug #12561
7 admin.site.register(Holder, inlines=[InnerInline])
8 
9+
10+# Models for #12749
11+
12+class Person(models.Model):
13+    firstname = models.CharField(max_length=15)
14+
15+class OutfitItem(models.Model):
16+    name = models.CharField(max_length=15)
17+
18+class Fashionista(models.Model):
19+    person = models.OneToOneField(Person, primary_key=True)
20+    weaknesses = models.ManyToManyField(OutfitItem, through='ShoppingWeakness', blank=True)
21+
22+class ShoppingWeakness(models.Model):
23+    fashionista = models.ForeignKey(Fashionista)
24+    item = models.ForeignKey(OutfitItem)
25+
26+class InlineWeakness(admin.TabularInline):
27+    model = ShoppingWeakness
28+    extra = 1
29+
30+admin.site.register(Fashionista, inlines=[InlineWeakness])
31+
32+
33 __test__ = {'API_TESTS': """
34 
35 # Regression test for #9362
36diff --git a/tests/regressiontests/admin_inlines/tests.py b/tests/regressiontests/admin_inlines/tests.py
37index 75a6658..a845f96 100644
38--- a/tests/regressiontests/admin_inlines/tests.py
39+++ b/tests/regressiontests/admin_inlines/tests.py
40@@ -28,3 +28,34 @@ class TestInline(TestCase):
41         actual = inner_formset.can_delete
42         self.assertEqual(expected, actual, 'can_delete must be equal')
43 
44+
45+# Tests for #12749
46+
47+from models import Person, OutfitItem, Fashionista
48+
49+class AdminTest(TestCase):
50+    fixtures = ['admin-views-users.xml']
51+
52+    def setUp(self):
53+        result = self.client.login(username='super', password='secret')
54+        self.failUnlessEqual(result, True)
55+
56+    def tearDown(self):
57+        self.client.logout()
58+
59+    def test_inline_primary(self):
60+        person = Person.objects.create(firstname='Imelda')
61+        item = OutfitItem.objects.create(name='Shoes')
62+        # Imelda likes shoes, but can't cary her own bags.
63+        data = {
64+            'shoppingweakness_set-TOTAL_FORMS': 1,
65+            'shoppingweakness_set-INITIAL_FORMS': 0,
66+            'shoppingweakness_set-MAX_NUM_FORMS': 0,
67+            '_save': u'Save',
68+            'person': person.id,
69+            'max_weight': 0,
70+            'shoppingweakness_set-0-item': item.id,
71+        }
72+        response = self.client.post('/test_admin/admin/admin_inlines/fashionista/add/', data)
73+        self.assertEqual(response.status_code, 302)
74+        self.assertEqual(len(Fashionista.objects.filter(person__firstname='Imelda')), 1)