Code

Ticket #17143: 17143_test.diff

File 17143_test.diff, 2.1 KB (added by calvinspealman, 2 years ago)

Turned the example into a test case

Line 
1diff --git a/tests/modeltests/test_17143/__init__.py b/tests/modeltests/test_17143/__init__.py
2new file mode 100644
3index 0000000..e69de29
4diff --git a/tests/modeltests/test_17143/models.py b/tests/modeltests/test_17143/models.py
5new file mode 100644
6index 0000000..dd19a87
7--- /dev/null
8+++ b/tests/modeltests/test_17143/models.py
9@@ -0,0 +1,22 @@
10+# coding: utf-8
11+"""
12+1. Bare-bones model
13+
14+This is a basic model with only two non-primary-key fields.
15+"""
16+from django.db import models
17+
18+
19+init_effect = []
20+
21+
22+class A(models.Model):
23+    foo = models.CharField(max_length=1, default='N')
24+   
25+    def __init__(self, *args, **kwargs):
26+        super(A, self).__init__(*args, **kwargs)
27+       
28+        init_effect.append(self.foo)
29+   
30+class B(A):
31+    bar = models.CharField(max_length=1, default='X')
32diff --git a/tests/modeltests/test_17143/tests.py b/tests/modeltests/test_17143/tests.py
33new file mode 100644
34index 0000000..8bde29b
35--- /dev/null
36+++ b/tests/modeltests/test_17143/tests.py
37@@ -0,0 +1,41 @@
38+from __future__ import absolute_import
39+
40+from datetime import datetime
41+
42+from django.core.exceptions import ObjectDoesNotExist
43+from django.db.models.fields import FieldDoesNotExist
44+from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
45+
46+from .models import A, B, init_effect
47+
48+
49+class ModelTest(TestCase):
50+
51+    def setUp(self):
52+        self.b = B.objects.create(foo='Y', bar='Z')
53+
54+    def tearDown(self):
55+        init_effect[:] = []
56+
57+    def test_17143_1(self):
58+        self.assertEqual(init_effect, ['Y'])
59+
60+    def test_17143_2(self):
61+        a = A.objects.get()
62+        self.assertEqual(init_effect, ['Y'] * 2)
63+
64+    def test_17143_3(self):
65+        b = B.objects.get()
66+        self.assertEqual(init_effect, ['Y'] * 2)
67+
68+    def test_17143_4(self):
69+        a = A.objects.get()
70+        b = a.b
71+        self.assertEqual(b.foo, a.foo)
72+        self.assertEqual(init_effect, ['Y'] * 3)
73+
74+    def test_17143_5(self):
75+        (a,) = A.objects.select_related('b')
76+        self.assertEqual(a.id, self.b.id)
77+        self.assertEqual(a.b.foo, 'Y')
78+        self.assertEqual(init_effect, ['Y'] * 3)