PageRenderTime 27ms CodeModel.GetById 23ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/custom_managers_regress/tests.py

https://code.google.com/p/mango-py/
Python | 47 lines | 22 code | 9 blank | 16 comment | 2 complexity | 61247b5ed1e7020fb6942d97c015601b MD5 | raw file
 1from django.test import TestCase
 2
 3from models import RelatedModel, RestrictedModel, OneToOneRestrictedModel
 4
 5class 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