diff options
| author | Jacob Kaplan-Moss <jacob@jacobian.org> | 2008-06-05 00:39:32 +0000 |
|---|---|---|
| committer | Jacob Kaplan-Moss <jacob@jacobian.org> | 2008-06-05 00:39:32 +0000 |
| commit | 1452d46240609ff39dacf9ea2f759ed600c2f09f (patch) | |
| tree | 523f61fd8ca3e9fa25a08ee532cda6e52ba1a406 /tests/regressiontests | |
| parent | d78f86a2204e610cb0cd1e2762500b3be88be184 (diff) | |
Fixed #6886: Tightened up ForeignKey and OneToOne field assignment. Specifically:
* Raise a ValueError if you try to assign the wrong type of object.
* Raise a ValueError if you try to assign None to a field not specified with null=True.
* Cache the set value at set time instead of just at lookup time.
This is a slightly backwards-incompatible change; see BackwardsIncompatibleChanges for more details.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7574 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests')
| -rw-r--r-- | tests/regressiontests/many_to_one_regress/models.py | 46 | ||||
| -rw-r--r-- | tests/regressiontests/one_to_one_regress/models.py | 38 |
2 files changed, 82 insertions, 2 deletions
diff --git a/tests/regressiontests/many_to_one_regress/models.py b/tests/regressiontests/many_to_one_regress/models.py index 57bbcd8489..4e49df1555 100644 --- a/tests/regressiontests/many_to_one_regress/models.py +++ b/tests/regressiontests/many_to_one_regress/models.py @@ -1,3 +1,7 @@ +""" +Regression tests for a few FK bugs: #1578, #6886 +""" + from django.db import models # If ticket #1578 ever slips back in, these models will not be able to be @@ -25,10 +29,48 @@ class Child(models.Model): __test__ = {'API_TESTS':""" ->>> Third.AddManipulator().save(dict(id='3', name='An example', another=None)) +>>> Third.objects.create(id='3', name='An example') <Third: Third object> >>> parent = Parent(name = 'fred') >>> parent.save() ->>> Child.AddManipulator().save(dict(name='bam-bam', parent=parent.id)) +>>> Child.objects.create(name='bam-bam', parent=parent) <Child: Child object> + +# +# Tests of ForeignKey assignment and the related-object cache (see #6886) +# +>>> p = Parent.objects.create(name="Parent") +>>> c = Child.objects.create(name="Child", parent=p) + +# Look up the object again so that we get a "fresh" object +>>> c = Child.objects.get(name="Child") +>>> p = c.parent + +# Accessing the related object again returns the exactly same object +>>> c.parent is p +True + +# But if we kill the cache, we get a new object +>>> del c._parent_cache +>>> c.parent is p +False + +# Assigning a new object results in that object getting cached immediately +>>> p2 = Parent.objects.create(name="Parent 2") +>>> c.parent = p2 +>>> c.parent is p2 +True + +# Assigning None fails: Child.parent is null=False +>>> c.parent = None +Traceback (most recent call last): + ... +ValueError: Cannot assign None: "Child.parent" does not allow null values. + +# You also can't assign an object of the wrong type here +>>> c.parent = First(id=1, second=1) +Traceback (most recent call last): + ... +ValueError: Cannot assign "<First: First object>": "Child.parent" must be a "Parent" instance. + """} diff --git a/tests/regressiontests/one_to_one_regress/models.py b/tests/regressiontests/one_to_one_regress/models.py index c68fdfc780..99022882f2 100644 --- a/tests/regressiontests/one_to_one_regress/models.py +++ b/tests/regressiontests/one_to_one_regress/models.py @@ -50,4 +50,42 @@ __test__ = {'API_TESTS':""" <Restaurant: Demon Dogs the restaurant> >>> p1.bar <Bar: Demon Dogs the bar> + +# +# Regression test for #6886 (the related-object cache) +# + +# Look up the objects again so that we get "fresh" objects +>>> p = Place.objects.get(name="Demon Dogs") +>>> r = p.restaurant + +# Accessing the related object again returns the exactly same object +>>> p.restaurant is r +True + +# But if we kill the cache, we get a new object +>>> del p._restaurant_cache +>>> p.restaurant is r +False + +# Reassigning the Restaurant object results in an immediate cache update +# We can't use a new Restaurant because that'll violate one-to-one, but +# with a new *instance* the is test below will fail if #6886 regresses. +>>> r2 = Restaurant.objects.get(pk=r.pk) +>>> p.restaurant = r2 +>>> p.restaurant is r2 +True + +# Assigning None fails: Place.restaurant is null=False +>>> p.restaurant = None +Traceback (most recent call last): + ... +ValueError: Cannot assign None: "Place.restaurant" does not allow null values. + +# You also can't assign an object of the wrong type here +>>> p.restaurant = p +Traceback (most recent call last): + ... +ValueError: Cannot assign "<Place: Demon Dogs the place>": "Place.restaurant" must be a "Restaurant" instance. + """} |
