summaryrefslogtreecommitdiff
path: root/tests/custom_managers/tests.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/custom_managers/tests.py')
-rw-r--r--tests/custom_managers/tests.py47
1 files changed, 46 insertions, 1 deletions
diff --git a/tests/custom_managers/tests.py b/tests/custom_managers/tests.py
index 437d75deea..383ccc0e44 100644
--- a/tests/custom_managers/tests.py
+++ b/tests/custom_managers/tests.py
@@ -3,7 +3,8 @@ from __future__ import unicode_literals
from django.test import TestCase
from django.utils import six
-from .models import Person, FunPerson, Book, Car, PersonManager, PublishedBookManager
+from .models import (Book, Car, FunPerson, OneToOneRestrictedModel, Person,
+ PersonManager, PublishedBookManager, RelatedModel, RestrictedModel)
class CustomManagerTests(TestCase):
@@ -463,3 +464,47 @@ class CustomManagerTests(TestCase):
lambda c: c.first_name,
ordered=False,
)
+
+
+class CustomManagersRegressTestCase(TestCase):
+ def test_filtered_default_manager(self):
+ """Even though the default manager filters out some records,
+ we must still be able to save (particularly, save by updating
+ existing records) those filtered instances. This is a
+ regression test for #8990, #9527"""
+ related = RelatedModel.objects.create(name="xyzzy")
+ obj = RestrictedModel.objects.create(name="hidden", related=related)
+ obj.name = "still hidden"
+ obj.save()
+
+ # If the hidden object wasn't seen during the save process,
+ # there would now be two objects in the database.
+ self.assertEqual(RestrictedModel.plain_manager.count(), 1)
+
+ def test_delete_related_on_filtered_manager(self):
+ """Deleting related objects should also not be distracted by a
+ restricted manager on the related object. This is a regression
+ test for #2698."""
+ related = RelatedModel.objects.create(name="xyzzy")
+
+ for name, public in (('one', True), ('two', False), ('three', False)):
+ RestrictedModel.objects.create(name=name, is_public=public, related=related)
+
+ obj = RelatedModel.objects.get(name="xyzzy")
+ obj.delete()
+
+ # All of the RestrictedModel instances should have been
+ # deleted, since they *all* pointed to the RelatedModel. If
+ # the default manager is used, only the public one will be
+ # deleted.
+ self.assertEqual(len(RestrictedModel.plain_manager.all()), 0)
+
+ def test_delete_one_to_one_manager(self):
+ # The same test case as the last one, but for one-to-one
+ # models, which are implemented slightly different internally,
+ # so it's a different code path.
+ obj = RelatedModel.objects.create(name="xyzzy")
+ OneToOneRestrictedModel.objects.create(name="foo", is_public=False, related=obj)
+ obj = RelatedModel.objects.get(name="xyzzy")
+ obj.delete()
+ self.assertEqual(len(OneToOneRestrictedModel.plain_manager.all()), 0)