|
1 from django.test import TestCase |
|
2 |
|
3 from models import RelatedModel, RestrictedModel, OneToOneRestrictedModel |
|
4 |
|
5 class CustomManagersRegressTestCase(TestCase): |
|
6 def test_filtered_default_manager(self): |
|
7 """Even though the default manager filters out some records, |
|
8 we must still be able to save (particularly, save by updating |
|
9 existing records) those filtered instances. This is a |
|
10 regression test for #8990, #9527""" |
|
11 related = RelatedModel.objects.create(name="xyzzy") |
|
12 obj = RestrictedModel.objects.create(name="hidden", related=related) |
|
13 obj.name = "still hidden" |
|
14 obj.save() |
|
15 |
|
16 # If the hidden object wasn't seen during the save process, |
|
17 # there would now be two objects in the database. |
|
18 self.assertEqual(RestrictedModel.plain_manager.count(), 1) |
|
19 |
|
20 def test_delete_related_on_filtered_manager(self): |
|
21 """Deleting related objects should also not be distracted by a |
|
22 restricted manager on the related object. This is a regression |
|
23 test for #2698.""" |
|
24 related = RelatedModel.objects.create(name="xyzzy") |
|
25 |
|
26 for name, public in (('one', True), ('two', False), ('three', False)): |
|
27 RestrictedModel.objects.create(name=name, is_public=public, related=related) |
|
28 |
|
29 obj = RelatedModel.objects.get(name="xyzzy") |
|
30 obj.delete() |
|
31 |
|
32 # All of the RestrictedModel instances should have been |
|
33 # deleted, since they *all* pointed to the RelatedModel. If |
|
34 # the default manager is used, only the public one will be |
|
35 # deleted. |
|
36 self.assertEqual(len(RestrictedModel.plain_manager.all()), 0) |
|
37 |
|
38 def test_delete_one_to_one_manager(self): |
|
39 # The same test case as the last one, but for one-to-one |
|
40 # models, which are implemented slightly different internally, |
|
41 # so it's a different code path. |
|
42 obj = RelatedModel.objects.create(name="xyzzy") |
|
43 OneToOneRestrictedModel.objects.create(name="foo", is_public=False, related=obj) |
|
44 obj = RelatedModel.objects.get(name="xyzzy") |
|
45 obj.delete() |
|
46 self.assertEqual(len(OneToOneRestrictedModel.plain_manager.all()), 0) |
|
47 |