Django

Code

root/django/trunk/tests/regressiontests/many_to_one_regress/models.py

Revision 8609, 4.6 kB (checked in by mtredinnick, 1 week ago)

Added a test from kcarnold to show that #7498 is fixed. Refs #7498.

  • Property svn:eol-style set to native
Line 
1 """
2 Regression tests for a few FK bugs: #1578, #6886
3 """
4
5 from django.db import models
6
7 # If ticket #1578 ever slips back in, these models will not be able to be
8 # created (the field names being lower-cased versions of their opposite
9 # classes is important here).
10
11 class First(models.Model):
12     second = models.IntegerField()
13
14 class Second(models.Model):
15     first = models.ForeignKey(First, related_name = 'the_first')
16
17 # Protect against repetition of #1839, #2415 and #2536.
18 class Third(models.Model):
19     name = models.CharField(max_length=20)
20     third = models.ForeignKey('self', null=True, related_name='child_set')
21
22 class Parent(models.Model):
23     name = models.CharField(max_length=20)
24     bestchild = models.ForeignKey('Child', null=True, related_name='favored_by')
25
26 class Child(models.Model):
27     name = models.CharField(max_length=20)
28     parent = models.ForeignKey(Parent)
29
30
31 # Multiple paths to the same model (#7110, #7125)
32 class Category(models.Model):
33     name = models.CharField(max_length=20)
34
35     def __unicode__(self):
36         return self.name
37
38 class Record(models.Model):
39     category = models.ForeignKey(Category)
40
41 class Relation(models.Model):
42     left = models.ForeignKey(Record, related_name='left_set')
43     right = models.ForeignKey(Record, related_name='right_set')
44
45     def __unicode__(self):
46         return u"%s - %s" % (self.left.category.name, self.right.category.name)
47
48
49 __test__ = {'API_TESTS':"""
50 >>> Third.objects.create(id='3', name='An example')
51 <Third: Third object>
52 >>> parent = Parent(name = 'fred')
53 >>> parent.save()
54 >>> Child.objects.create(name='bam-bam', parent=parent)
55 <Child: Child object>
56
57 #
58 # Tests of ForeignKey assignment and the related-object cache (see #6886).
59 #
60 >>> p = Parent.objects.create(name="Parent")
61 >>> c = Child.objects.create(name="Child", parent=p)
62
63 # Look up the object again so that we get a "fresh" object.
64 >>> c = Child.objects.get(name="Child")
65 >>> p = c.parent
66
67 # Accessing the related object again returns the exactly same object.
68 >>> c.parent is p
69 True
70
71 # But if we kill the cache, we get a new object.
72 >>> del c._parent_cache
73 >>> c.parent is p
74 False
75
76 # Assigning a new object results in that object getting cached immediately.
77 >>> p2 = Parent.objects.create(name="Parent 2")
78 >>> c.parent = p2
79 >>> c.parent is p2
80 True
81
82 # Assigning None succeeds if field is null=True.
83 >>> p.bestchild = None
84 >>> p.bestchild is None
85 True
86
87 # bestchild should still be None after saving.
88 >>> p.save()
89 >>> p.bestchild is None
90 True
91
92 # bestchild should still be None after fetching the object again.
93 >>> p = Parent.objects.get(name="Parent")
94 >>> p.bestchild is None
95 True
96
97 # Assigning None fails: Child.parent is null=False.
98 >>> c.parent = None
99 Traceback (most recent call last):
100     ...
101 ValueError: Cannot assign None: "Child.parent" does not allow null values.
102
103 # You also can't assign an object of the wrong type here
104 >>> c.parent = First(id=1, second=1)
105 Traceback (most recent call last):
106     ...
107 ValueError: Cannot assign "<First: First object>": "Child.parent" must be a "Parent" instance.
108
109 # Creation using keyword argument should cache the related object.
110 >>> p = Parent.objects.get(name="Parent")
111 >>> c = Child(parent=p)
112 >>> c.parent is p
113 True
114
115 # Creation using keyword argument and unsaved related instance (#8070).
116 >>> p = Parent()
117 >>> c = Child(parent=p)
118 >>> c.parent is p
119 True
120
121 # Creation using attname keyword argument and an id will cause the related
122 # object to be fetched.
123 >>> p = Parent.objects.get(name="Parent")
124 >>> c = Child(parent_id=p.id)
125 >>> c.parent is p
126 False
127 >>> c.parent == p
128 True
129
130
131 #
132 # Test of multiple ForeignKeys to the same model (bug #7125).
133 #
134 >>> c1 = Category.objects.create(name='First')
135 >>> c2 = Category.objects.create(name='Second')
136 >>> c3 = Category.objects.create(name='Third')
137 >>> r1 = Record.objects.create(category=c1)
138 >>> r2 = Record.objects.create(category=c1)
139 >>> r3 = Record.objects.create(category=c2)
140 >>> r4 = Record.objects.create(category=c2)
141 >>> r5 = Record.objects.create(category=c3)
142 >>> r = Relation.objects.create(left=r1, right=r2)
143 >>> r = Relation.objects.create(left=r3, right=r4)
144 >>> r = Relation.objects.create(left=r1, right=r3)
145 >>> r = Relation.objects.create(left=r5, right=r2)
146 >>> r = Relation.objects.create(left=r3, right=r2)
147
148 >>> Relation.objects.filter(left__category__name__in=['First'], right__category__name__in=['Second'])
149 [<Relation: First - Second>]
150
151 >>> Category.objects.filter(record__left_set__right__category__name='Second').order_by('name')
152 [<Category: First>, <Category: Second>]
153
154 >>> c2 = Child.objects.create(name="Grandchild", parent=c)
155 Traceback (most recent call last):
156     ...
157 ValueError: Cannot assign "<Child: Child object>": "Child.parent" must be a "Parent" instance.
158
159 """}
Note: See TracBrowser for help on using the browser.