Ticket #13815: tests.py

File tests.py, 2.7 KB (added by Bas Peschier, 14 years ago)

unittest for checking isnull on reverse relations

Line 
1from django.test import TestCase
2from models import Outer, Inner
3
4class ReverseIsNullTests(TestCase):
5 """ Test isnull filters on reverse relations. """
6
7 # Tests are in order: filter(isnull=True), filter(isnull=False),
8 # exclude(isnull=True), exclude(isnull=False)
9
10 def setUp(self):
11 # Create outer objects
12 self.outerA = Outer.objects.create(name="A")
13 self.outerB = Outer.objects.create(name="B")
14
15 def test_isnull_empty_reverse(self):
16 # No Inner-object: expecting [A,B], [], [], [A,B]
17 self.assertEquals(list(Outer.objects.filter(inner__isnull=True)), [self.outerA, self.outerB])
18 self.assertEquals(list(Outer.objects.filter(inner__isnull=False)), [])
19 self.assertEquals(list(Outer.objects.exclude(inner__isnull=True)), [])
20 self.assertEquals(list(Outer.objects.exclude(inner__isnull=False)), [self.outerA, self.outerB])
21
22 def test_isnull_related_reverse(self):
23 # Create Inner-object with relation to A
24 inner = Inner.objects.create(outer=self.outerA)
25
26 # One related Inner-object to A: expecting [B], [A], [A], [B]
27 self.assertEquals(list(Outer.objects.filter(inner__isnull=True)), [self.outerB])
28 self.assertEquals(list(Outer.objects.filter(inner__isnull=False)), [self.outerA])
29 self.assertEquals(list(Outer.objects.exclude(inner__isnull=True)), [self.outerA])
30 self.assertEquals(list(Outer.objects.exclude(inner__isnull=False)), [self.outerB])
31
32 inner.delete()
33
34 def test_isnull_nonrelated_reverse(self):
35 # Create Inner object without relation
36 inner = Inner.objects.create()
37
38 # No related Inner-object: expecting [A,B], [], [], [A,B]
39 self.assertEquals(list(Outer.objects.filter(inner__isnull=True)), [self.outerA, self.outerB])
40 self.assertEquals(list(Outer.objects.filter(inner__isnull=False)), [])
41 self.assertEquals(list(Outer.objects.exclude(inner__isnull=True)), [])
42 self.assertEquals(list(Outer.objects.exclude(inner__isnull=False)), [self.outerA, self.outerB])
43
44 inner.delete()
45
46 def test_isnull_reverse(self):
47 # Create Inner object without relation and with
48 inner = Inner.objects.create()
49 innerR = Inner.objects.create(outer=self.outerA)
50
51 # One related Inner-object to A, one with no relation: expecting [B], [A], [A], [B]
52 self.assertEquals(list(Outer.objects.filter(inner__isnull=True)), [self.outerB])
53 self.assertEquals(list(Outer.objects.filter(inner__isnull=False)), [self.outerA])
54 self.assertEquals(list(Outer.objects.exclude(inner__isnull=True)), [self.outerA])
55 self.assertEquals(list(Outer.objects.exclude(inner__isnull=False)), [self.outerB])
56
57 inner.delete()
58 innerR.delete()
59
60
Back to Top