Code

Ticket #9023: 9023-test.diff

File 9023-test.diff, 2.7 KB (added by adurdin, 5 years ago)

Regression test to exemplify the bug.

Line 
1Index: tests/regressiontests/model_regress/tests.py
2===================================================================
3--- tests/regressiontests/model_regress/tests.py        (revision 0)
4+++ tests/regressiontests/model_regress/tests.py        (revision 0)
5@@ -0,0 +1,43 @@
6+from django.test import TestCase
7+
8+class OneToOneDeletion(TestCase):
9+    """Regression test for #9023"""
10+
11+    def setUp(self):
12+        from models import Ticket9023Widget, Ticket9023Sprocket
13+        self.w = Ticket9023Widget(name="Some Widget")
14+        self.w.save()
15+        self.s = Ticket9023Sprocket(name="Some Sprocket")
16+        self.s.w = self.w
17+        self.s.save()
18+
19+    def testNullAndDelete(self):
20+        from models import Ticket9023Widget, Ticket9023Sprocket
21+        self.assertEqual(Ticket9023Widget.objects.all().count(), 1)
22+        self.assertEqual(Ticket9023Sprocket.objects.all().count(), 1)
23+
24+        self.s.w = None
25+        self.s.save()
26+        self.w.delete()
27+
28+        self.assertEqual(Ticket9023Widget.objects.all().count(), 0)
29+        self.assertEqual(Ticket9023Sprocket.objects.all().count(), 1)
30+
31+    def testNullAccessAndDelete(self):
32+        from models import Ticket9023Widget, Ticket9023Sprocket
33+        self.assertEqual(Ticket9023Widget.objects.all().count(), 1)
34+        self.assertEqual(Ticket9023Sprocket.objects.all().count(), 1)
35+
36+        self.w.sprocket.name
37+        self.s.w = None
38+        self.s.save()
39+        self.w.delete()
40+
41+        self.assertEqual(Ticket9023Widget.objects.all().count(), 0)
42+        self.assertEqual(Ticket9023Sprocket.objects.all().count(), 1)
43+
44+    def tearDown(self):
45+        from models import Ticket9023Widget, Ticket9023Sprocket
46+        Ticket9023Widget.objects.all().delete()
47+        Ticket9023Sprocket.objects.all().delete()
48+
49Index: tests/regressiontests/model_regress/models.py
50===================================================================
51--- tests/regressiontests/model_regress/models.py       (revision 10680)
52+++ tests/regressiontests/model_regress/models.py       (working copy)
53@@ -58,7 +58,14 @@
54         # object).
55         return 'Názov: %s' % self.name
56 
57+class Ticket9023Widget(models.Model):
58+    name = models.CharField(max_length=10)
59 
60+class Ticket9023Sprocket(models.Model):
61+    name = models.CharField(max_length=10)
62+    w = models.OneToOneField(Ticket9023Widget, null=True, blank=True,
63+        related_name='sprocket')
64+
65 __test__ = {'API_TESTS': """
66 (NOTE: Part of the regression test here is merely parsing the model
67 declaration. The verbose_name, in particular, did not always work.)
68@@ -146,7 +153,6 @@
69 >>> b.save()
70 >>> BrokenUnicodeMethod.objects.all()
71 [<BrokenUnicodeMethod: [Bad Unicode data]>]
72-
73 """}
74 
75 if settings.DATABASE_ENGINE not in ("mysql", "oracle"):
76@@ -167,4 +173,3 @@
77 1
78 
79 """
80-