Ticket #14459: many_to_one_test_conversion.diff

File many_to_one_test_conversion.diff, 9.4 KB (added by Gabriel Hurley, 14 years ago)
  • tests/regressiontests/many_to_one_regress/models.py

     
    4444
    4545    def __unicode__(self):
    4646        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 # Nor can you explicitly assign None to Child.parent during object creation
    110 # (regression for #9649).
    111 >>> Child(name='xyzzy', parent=None)
    112 Traceback (most recent call last):
    113     ...
    114 ValueError: Cannot assign None: "Child.parent" does not allow null values.
    115 >>> Child.objects.create(name='xyzzy', parent=None)
    116 Traceback (most recent call last):
    117     ...
    118 ValueError: Cannot assign None: "Child.parent" does not allow null values.
    119 
    120 # Creation using keyword argument should cache the related object.
    121 >>> p = Parent.objects.get(name="Parent")
    122 >>> c = Child(parent=p)
    123 >>> c.parent is p
    124 True
    125 
    126 # Creation using keyword argument and unsaved related instance (#8070).
    127 >>> p = Parent()
    128 >>> c = Child(parent=p)
    129 >>> c.parent is p
    130 True
    131 
    132 # Creation using attname keyword argument and an id will cause the related
    133 # object to be fetched.
    134 >>> p = Parent.objects.get(name="Parent")
    135 >>> c = Child(parent_id=p.id)
    136 >>> c.parent is p
    137 False
    138 >>> c.parent == p
    139 True
    140 
    141 
    142 #
    143 # Test of multiple ForeignKeys to the same model (bug #7125).
    144 #
    145 >>> c1 = Category.objects.create(name='First')
    146 >>> c2 = Category.objects.create(name='Second')
    147 >>> c3 = Category.objects.create(name='Third')
    148 >>> r1 = Record.objects.create(category=c1)
    149 >>> r2 = Record.objects.create(category=c1)
    150 >>> r3 = Record.objects.create(category=c2)
    151 >>> r4 = Record.objects.create(category=c2)
    152 >>> r5 = Record.objects.create(category=c3)
    153 >>> r = Relation.objects.create(left=r1, right=r2)
    154 >>> r = Relation.objects.create(left=r3, right=r4)
    155 >>> r = Relation.objects.create(left=r1, right=r3)
    156 >>> r = Relation.objects.create(left=r5, right=r2)
    157 >>> r = Relation.objects.create(left=r3, right=r2)
    158 
    159 >>> Relation.objects.filter(left__category__name__in=['First'], right__category__name__in=['Second'])
    160 [<Relation: First - Second>]
    161 
    162 >>> Category.objects.filter(record__left_set__right__category__name='Second').order_by('name')
    163 [<Category: First>, <Category: Second>]
    164 
    165 >>> c2 = Child.objects.create(name="Grandchild", parent=c)
    166 Traceback (most recent call last):
    167     ...
    168 ValueError: Cannot assign "<Child: Child object>": "Child.parent" must be a "Parent" instance.
    169 
    170 # Regression for #12190 -- Should be able to instantiate a FK
    171 # outside of a model, and interrogate its related field.
    172 >>> cat = models.ForeignKey(Category)
    173 >>> cat.rel.get_related_field().name
    174 'id'
    175 
    176 """}
  • tests/regressiontests/many_to_one_regress/tests.py

     
     1from django.db import models
     2from django.test import TestCase
     3
     4from models import First, Second, Third, Parent, Child, Category, Record, Relation
     5
     6class ManyToOneRegressionTests(TestCase):
     7    def test_object_creation(self):
     8        Third.objects.create(id='3', name='An example')
     9        parent = Parent(name = 'fred')
     10        parent.save()
     11        Child.objects.create(name='bam-bam', parent=parent)
     12       
     13    def test_fk_assignment_and_related_object_cache(self):
     14        # Tests of ForeignKey assignment and the related-object cache (see #6886).
     15       
     16        p = Parent.objects.create(name="Parent")
     17        c = Child.objects.create(name="Child", parent=p)
     18       
     19        # Look up the object again so that we get a "fresh" object.
     20        c = Child.objects.get(name="Child")
     21        p = c.parent
     22       
     23        # Accessing the related object again returns the exactly same object.
     24        self.assertTrue(c.parent is p)
     25       
     26        # But if we kill the cache, we get a new object.
     27        del c._parent_cache
     28        self.assertFalse(c.parent is p)
     29       
     30        # Assigning a new object results in that object getting cached immediately.
     31        p2 = Parent.objects.create(name="Parent 2")
     32        c.parent = p2
     33        self.assertTrue(c.parent is p2)
     34       
     35        # Assigning None succeeds if field is null=True.
     36        p.bestchild = None
     37        self.assertTrue(p.bestchild is None)
     38       
     39        # bestchild should still be None after saving.
     40        p.save()
     41        self.assertTrue(p.bestchild is None)
     42       
     43        # bestchild should still be None after fetching the object again.
     44        p = Parent.objects.get(name="Parent")
     45        self.assertTrue(p.bestchild is None)
     46       
     47        # Assigning None fails: Child.parent is null=False.
     48        self.assertRaises(ValueError, setattr, c, "parent", None)
     49       
     50        # You also can't assign an object of the wrong type here
     51        self.assertRaises(ValueError, setattr, c, "parent", First(id=1, second=1))
     52       
     53        # Nor can you explicitly assign None to Child.parent during object creation
     54        # (regression for #9649).
     55        self.assertRaises(ValueError, Child, name='xyzzy', parent=None)
     56        self.assertRaises(ValueError, Child.objects.create, name='xyzzy', parent=None)
     57       
     58        # Creation using keyword argument should cache the related object.
     59        p = Parent.objects.get(name="Parent")
     60        c = Child(parent=p)
     61        self.assertTrue(c.parent is p)
     62       
     63        # Creation using keyword argument and unsaved related instance (#8070).
     64        p = Parent()
     65        c = Child(parent=p)
     66        self.assertTrue(c.parent is p)
     67       
     68        # Creation using attname keyword argument and an id will cause the related
     69        # object to be fetched.
     70        p = Parent.objects.get(name="Parent")
     71        c = Child(parent_id=p.id)
     72        self.assertFalse(c.parent is p)
     73        self.assertEqual(c.parent, p)
     74   
     75    def test_multiple_foreignkeys(self):
     76        # Test of multiple ForeignKeys to the same model (bug #7125).
     77        c1 = Category.objects.create(name='First')
     78        c2 = Category.objects.create(name='Second')
     79        c3 = Category.objects.create(name='Third')
     80        r1 = Record.objects.create(category=c1)
     81        r2 = Record.objects.create(category=c1)
     82        r3 = Record.objects.create(category=c2)
     83        r4 = Record.objects.create(category=c2)
     84        r5 = Record.objects.create(category=c3)
     85        r = Relation.objects.create(left=r1, right=r2)
     86        r = Relation.objects.create(left=r3, right=r4)
     87        r = Relation.objects.create(left=r1, right=r3)
     88        r = Relation.objects.create(left=r5, right=r2)
     89        r = Relation.objects.create(left=r3, right=r2)
     90       
     91        q1 = Relation.objects.filter(left__category__name__in=['First'], right__category__name__in=['Second'])
     92        self.assertQuerysetEqual(q1, ["<Relation: First - Second>"])
     93       
     94        q2 = Category.objects.filter(record__left_set__right__category__name='Second').order_by('name')
     95        self.assertQuerysetEqual(q2, ["<Category: First>", "<Category: Second>"])
     96       
     97        p = Parent.objects.create(name="Parent")
     98        c = Child.objects.create(name="Child", parent=p)
     99        self.assertRaises(ValueError, Child.objects.create, name="Grandchild", parent=c)
     100       
     101    def test_fk_instantiation_outside_model(self):
     102        # Regression for #12190 -- Should be able to instantiate a FK
     103        # outside of a model, and interrogate its related field.
     104        cat = models.ForeignKey(Category)
     105        self.assertEqual('id', cat.rel.get_related_field().name)
Back to Top