summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorKarl Hobley <karlhobley10@gmail.com>2015-03-16 19:28:53 +0000
committerTim Graham <timograham@gmail.com>2015-03-18 19:00:09 -0400
commit81e1a35c364e5353d2bf99368ad30a4184fbb653 (patch)
tree2cbfcc6a605600d4f4c387d4c49d17dfe300b91f /tests
parent02d78bb1a80706d941ffc6c892cc75208eb6b782 (diff)
Fixed #24495 -- Allowed unsaved model instance assignment check to be bypassed.
Diffstat (limited to 'tests')
-rw-r--r--tests/contenttypes_tests/tests.py27
-rw-r--r--tests/many_to_one/tests.py25
-rw-r--r--tests/one_to_one/tests.py27
3 files changed, 78 insertions, 1 deletions
diff --git a/tests/contenttypes_tests/tests.py b/tests/contenttypes_tests/tests.py
index 3ae4774033..ef01a56301 100644
--- a/tests/contenttypes_tests/tests.py
+++ b/tests/contenttypes_tests/tests.py
@@ -257,6 +257,33 @@ class GenericForeignKeyTests(IsolatedModelsTestCase):
author.save()
model.content_object = author # no error because the instance is saved
+ def test_unsaved_instance_on_generic_foreign_key_allowed_when_wanted(self):
+ """
+ #24495 - Assigning an unsaved object to a GenericForeignKey
+ should be allowed when the allow_unsaved_instance_assignment
+ attribute has been set to True.
+ """
+ class UnsavedGenericForeignKey(GenericForeignKey):
+ # A GenericForeignKey which can point to an unsaved object
+ allow_unsaved_instance_assignment = True
+
+ class Band(models.Model):
+ name = models.CharField(max_length=50)
+
+ class BandMember(models.Model):
+ band_ct = models.ForeignKey(ContentType)
+ band_id = models.PositiveIntegerField()
+ band = UnsavedGenericForeignKey('band_ct', 'band_id')
+ first_name = models.CharField(max_length=50)
+ last_name = models.CharField(max_length=50)
+
+ beatles = Band(name='The Beatles')
+ john = BandMember(first_name='John', last_name='Lennon')
+ # This should not raise an exception as the GenericForeignKey between
+ # member and band has allow_unsaved_instance_assignment=True.
+ john.band = beatles
+ self.assertEqual(john.band, beatles)
+
class GenericRelationshipTests(IsolatedModelsTestCase):
diff --git a/tests/many_to_one/tests.py b/tests/many_to_one/tests.py
index fdb77b4711..af7503fba8 100644
--- a/tests/many_to_one/tests.py
+++ b/tests/many_to_one/tests.py
@@ -159,6 +159,31 @@ class ManyToOneTests(TestCase):
self.assertFalse(hasattr(self.r2.article_set, 'remove'))
self.assertFalse(hasattr(self.r2.article_set, 'clear'))
+ def test_assign_unsaved_check_override(self):
+ """
+ #24495 - Assigning an unsaved object to a ForeignKey
+ should be allowed when the allow_unsaved_instance_assignment
+ attribute has been set to True.
+ """
+ class UnsavedForeignKey(models.ForeignKey):
+ # A ForeignKey which can point to an unsaved object
+ allow_unsaved_instance_assignment = True
+
+ class Band(models.Model):
+ name = models.CharField(max_length=50)
+
+ class BandMember(models.Model):
+ band = UnsavedForeignKey(Band)
+ first_name = models.CharField(max_length=50)
+ last_name = models.CharField(max_length=50)
+
+ beatles = Band(name='The Beatles')
+ john = BandMember(first_name='John', last_name='Lennon')
+ # This should not raise an exception as the ForeignKey between member
+ # and band has allow_unsaved_instance_assignment=True.
+ john.band = beatles
+ self.assertEqual(john.band, beatles)
+
def test_selects(self):
self.r.article_set.create(headline="John's second story",
pub_date=datetime.date(2005, 7, 29))
diff --git a/tests/one_to_one/tests.py b/tests/one_to_one/tests.py
index d7d5793b0a..2cda25b0aa 100644
--- a/tests/one_to_one/tests.py
+++ b/tests/one_to_one/tests.py
@@ -1,6 +1,6 @@
from __future__ import unicode_literals
-from django.db import IntegrityError, connection, transaction
+from django.db import IntegrityError, connection, models, transaction
from django.test import TestCase
from .models import (
@@ -145,6 +145,31 @@ class OneToOneTests(TestCase):
% (bar, p._meta.object_name)):
p.undergroundbar = bar
+ def test_unsaved_object_check_override(self):
+ """
+ #24495 - Assigning an unsaved object to a OneToOneField
+ should be allowed when the allow_unsaved_instance_assignment
+ attribute has been set to True.
+ """
+ class UnsavedOneToOneField(models.OneToOneField):
+ # A OneToOneField which can point to an unsaved object
+ allow_unsaved_instance_assignment = True
+
+ class Band(models.Model):
+ name = models.CharField(max_length=50)
+
+ class BandManager(models.Model):
+ band = UnsavedOneToOneField(Band)
+ first_name = models.CharField(max_length=50)
+ last_name = models.CharField(max_length=50)
+
+ band = Band(name='The Beatles')
+ manager = BandManager(first_name='Brian', last_name='Epstein')
+ # This should not raise an exception as the OneToOneField between
+ # manager and band has allow_unsaved_instance_assignment=True.
+ manager.band = band
+ self.assertEqual(manager.band, band)
+
def test_reverse_relationship_cache_cascade(self):
"""
Regression test for #9023: accessing the reverse relationship shouldn't