PageRenderTime 9ms CodeModel.GetById 2ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/reverse_single_related/tests.py

https://code.google.com/p/mango-py/
Python | 36 lines | 17 code | 9 blank | 10 comment | 0 complexity | fdc8bc26b7deead078b020dc7b482110 MD5 | raw file
 1from django.test import TestCase
 2
 3from regressiontests.reverse_single_related.models import *
 4
 5class ReverseSingleRelatedTests(TestCase):
 6    """
 7    Regression tests for an object that cannot access a single related
 8    object due to a restrictive default manager.
 9    """
10
11    def test_reverse_single_related(self):
12
13        public_source = Source.objects.create(is_public=True)
14        public_item = Item.objects.create(source=public_source)
15
16        private_source = Source.objects.create(is_public=False)
17        private_item = Item.objects.create(source=private_source)
18
19        # Only one source is available via all() due to the custom default manager.
20        self.assertQuerysetEqual(
21                Source.objects.all(),
22                ["<Source: Source object>"]
23        )
24
25        self.assertEqual(public_item.source, public_source)
26
27        # Make sure that an item can still access its related source even if the default
28        # manager doesn't normally allow it.
29        self.assertEqual(private_item.source, private_source)
30
31        # If the manager is marked "use_for_related_fields", it'll get used instead
32        # of the "bare" queryset. Usually you'd define this as a property on the class,
33        # but this approximates that in a way that's easier in tests.
34        Source.objects.use_for_related_fields = True
35        private_item = Item.objects.get(pk=private_item.pk)
36        self.assertRaises(Source.DoesNotExist, lambda: private_item.source)