summaryrefslogtreecommitdiff
path: root/tests/one_to_one
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2015-07-24 07:51:40 -0400
committerTim Graham <timograham@gmail.com>2015-08-10 08:51:32 -0400
commit5980b05c1fad69eef907e0076aa2dc837edab529 (patch)
tree559858b70445d26700fcf6ef09c655d2fa050557 /tests/one_to_one
parent12f91f6ebdd2470197fff4e053b50f3e54294028 (diff)
downloaddjango-5980b05c1fad69eef907e0076aa2dc837edab529.tar.gz
Fixed #25160 -- Moved unsaved model instance data loss check to Model.save()
This mostly reverts 5643a3b51be338196d0b292d5626ad43648448d3 and 81e1a35c364e5353d2bf99368ad30a4184fbb653. Thanks Carl Meyer for review.
Diffstat (limited to 'tests/one_to_one')
-rw-r--r--tests/one_to_one/tests.py49
1 files changed, 13 insertions, 36 deletions
diff --git a/tests/one_to_one/tests.py b/tests/one_to_one/tests.py
index 807d504998..ee0536906c 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, models, transaction
+from django.db import IntegrityError, connection, transaction
from django.test import TestCase
from .models import (
@@ -134,41 +134,9 @@ class OneToOneTests(TestCase):
should raise an exception.
"""
place = Place(name='User', address='London')
- with self.assertRaisesMessage(ValueError,
- 'Cannot assign "%r": "%s" instance isn\'t saved in the database.'
- % (place, Restaurant.place.field.remote_field.model._meta.object_name)):
+ msg = "save() prohibited to prevent data loss due to unsaved related object 'place'."
+ with self.assertRaisesMessage(ValueError, msg):
Restaurant.objects.create(place=place, serves_hot_dogs=True, serves_pizza=False)
- bar = UndergroundBar()
- p = Place(name='User', address='London')
- with self.assertRaisesMessage(ValueError,
- 'Cannot assign "%r": "%s" instance isn\'t saved in the database.'
- % (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, models.CASCADE)
- 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):
"""
@@ -249,6 +217,11 @@ class OneToOneTests(TestCase):
r = Restaurant(place=p)
self.assertIs(r.place, p)
+ # Creation using keyword argument and unsaved related instance (#8070).
+ p = Place()
+ r = Restaurant(place=p)
+ self.assertTrue(r.place is p)
+
# Creation using attname keyword argument and an id will cause the related
# object to be fetched.
p = Place.objects.get(name="Demon Dogs")
@@ -392,8 +365,12 @@ class OneToOneTests(TestCase):
"""
p = Place()
b = UndergroundBar.objects.create()
+ msg = (
+ 'Cannot assign "<UndergroundBar: UndergroundBar object>": "Place" '
+ 'instance isn\'t saved in the database.'
+ )
with self.assertNumQueries(0):
- with self.assertRaises(ValueError):
+ with self.assertRaisesMessage(ValueError, msg):
p.undergroundbar = b
def test_nullable_o2o_delete(self):