Code

Ticket #11807: django-2009-08-31-admin-onetoone-pk-inline-test.diff

File django-2009-08-31-admin-onetoone-pk-inline-test.diff, 3.4 KB (added by AdamG, 5 years ago)

Patch that adds a regression test for this issue.

Line 
1diff -r e3845055edd8 tests/regressiontests/admin_views/models.py
2--- a/tests/regressiontests/admin_views/models.py       Sat Aug 29 12:40:47 2009 +0000
3+++ b/tests/regressiontests/admin_views/models.py       Mon Aug 31 10:14:27 2009 -0400
4@@ -417,8 +417,17 @@ class CategoryAdmin(admin.ModelAdmin):
5 class CategoryInline(admin.StackedInline):
6     model = Category
7 
8+class CollectorAux(models.Model):
9+    collector = models.OneToOneField(Collector, primary_key=True)
10+    aux_data = models.TextField(blank=True, null=True, default="foo")
11+
12+class CollectorAuxInline(admin.StackedInline):
13+    model = CollectorAux
14+
15 class CollectorAdmin(admin.ModelAdmin):
16-    inlines = [WidgetInline, DooHickeyInline, GrommetInline, WhatsitInline, FancyDoodadInline, CategoryInline]
17+    inlines = [WidgetInline, DooHickeyInline, GrommetInline, WhatsitInline, FancyDoodadInline, CategoryInline, CollectorAuxInline]
18+
19+
20 
21 admin.site.register(Article, ArticleAdmin)
22 admin.site.register(CustomArticle, CustomArticleAdmin)
23diff -r e3845055edd8 tests/regressiontests/admin_views/tests.py
24--- a/tests/regressiontests/admin_views/tests.py        Sat Aug 29 12:40:47 2009 +0000
25+++ b/tests/regressiontests/admin_views/tests.py        Mon Aug 31 10:14:27 2009 -0400
26@@ -18,7 +18,7 @@ from models import Article, BarAccount,
27     ExternalSubscriber, FooAccount, Gallery, ModelWithStringPrimaryKey, \
28     Person, Persona, Picture, Podcast, Section, Subscriber, Vodcast, \
29     Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit, \
30-    Category
31+    Category, CollectorAux
32 
33 try:
34     set
35@@ -1312,6 +1312,12 @@ class AdminInlineTests(TestCase):
36             "category_set-2-order": "",
37             "category_set-2-id": "",
38             "category_set-2-collector": "1",
39+
40+            "collectoraux-TOTAL_FORMS": "1",
41+            "collectoraux-INITIAL_FORMS": "0",
42+            "collectoraux-0-id": "",
43+            "collectoraux-0-collector": "1",
44+            "collectoraux-0-aux_data": "foo",
45         }
46 
47         result = self.client.login(username='super', password='secret')
48@@ -1534,6 +1540,32 @@ class AdminInlineTests(TestCase):
49         self.failUnlessEqual(Category.objects.get(id=3).order, 1)
50         self.failUnlessEqual(Category.objects.get(id=4).order, 0)
51 
52+    def test_onetoone_pk_inline(self):
53+        """Check that related OneToOnes that have primary_key=True are
54+        saved when the default value isn't changed.
55+
56+        Regression for #11807.
57+
58+        """
59+        self.failUnlessEqual(CollectorAux.objects.count(), 0)
60+        self.post_data["collectoraux-0-aux_data"] = "foo"
61+        response = self.client.post('/test_admin/admin/admin_views/collector/1/', self.post_data)
62+        self.failUnlessEqual(response.status_code, 302)
63+        self.failUnlessEqual(CollectorAux.objects.count(), 1)
64+
65+    def test_onetoone_pk_inline_changed(self):
66+        """Check that related OneToOnes that have primary_key=True are
67+        saved when the default value *is* changed.
68+
69+        Related to  #11807.
70+
71+        """
72+        self.failUnlessEqual(CollectorAux.objects.count(), 0)
73+        self.post_data['collectoraux-0-aux_data']  = "foo2"
74+        response = self.client.post('/test_admin/admin/admin_views/collector/1/', self.post_data)
75+        self.failUnlessEqual(response.status_code, 302)
76+        self.failUnlessEqual(CollectorAux.objects.count(), 1)
77+
78 
79 class NeverCacheTests(TestCase):
80     fixtures = ['admin-views-users.xml', 'admin-views-colors.xml', 'admin-views-fabrics.xml']