|
1 from django.test import TestCase |
|
2 from models import Reporter, Article |
|
3 |
|
4 class ManyToOneNullTests(TestCase): |
|
5 |
|
6 def setUp(self): |
|
7 # Create a Reporter. |
|
8 self.r = Reporter(name='John Smith') |
|
9 self.r.save() |
|
10 # Create an Article. |
|
11 self.a = Article(headline="First", reporter=self.r) |
|
12 self.a.save() |
|
13 # Create an Article via the Reporter object. |
|
14 self.a2 = self.r.article_set.create(headline="Second") |
|
15 # Create an Article with no Reporter by passing "reporter=None". |
|
16 self.a3 = Article(headline="Third", reporter=None) |
|
17 self.a3.save() |
|
18 # Create another article and reporter |
|
19 self.r2 = Reporter(name='Paul Jones') |
|
20 self.r2.save() |
|
21 self.a4 = self.r2.article_set.create(headline='Fourth') |
|
22 |
|
23 def test_get_related(self): |
|
24 self.assertEqual(self.a.reporter.id, self.r.id) |
|
25 # Article objects have access to their related Reporter objects. |
|
26 r = self.a.reporter |
|
27 self.assertEqual(r.id, self.r.id) |
|
28 |
|
29 def test_created_via_related_set(self): |
|
30 self.assertEqual(self.a2.reporter.id, self.r.id) |
|
31 |
|
32 def test_related_set(self): |
|
33 # Reporter objects have access to their related Article objects. |
|
34 self.assertQuerysetEqual(self.r.article_set.all(), |
|
35 ['<Article: First>', '<Article: Second>']) |
|
36 self.assertQuerysetEqual(self.r.article_set.filter(headline__startswith='Fir'), |
|
37 ['<Article: First>']) |
|
38 self.assertEqual(self.r.article_set.count(), 2) |
|
39 |
|
40 def test_created_without_related(self): |
|
41 self.assertEqual(self.a3.reporter, None) |
|
42 # Need to reget a3 to refresh the cache |
|
43 a3 = Article.objects.get(pk=self.a3.pk) |
|
44 self.assertRaises(AttributeError, getattr, a3.reporter, 'id') |
|
45 # Accessing an article's 'reporter' attribute returns None |
|
46 # if the reporter is set to None. |
|
47 self.assertEqual(a3.reporter, None) |
|
48 # To retrieve the articles with no reporters set, use "reporter__isnull=True". |
|
49 self.assertQuerysetEqual(Article.objects.filter(reporter__isnull=True), |
|
50 ['<Article: Third>']) |
|
51 # We can achieve the same thing by filtering for the case where the |
|
52 # reporter is None. |
|
53 self.assertQuerysetEqual(Article.objects.filter(reporter=None), |
|
54 ['<Article: Third>']) |
|
55 # Set the reporter for the Third article |
|
56 self.assertQuerysetEqual(self.r.article_set.all(), |
|
57 ['<Article: First>', '<Article: Second>']) |
|
58 self.r.article_set.add(a3) |
|
59 self.assertQuerysetEqual(self.r.article_set.all(), |
|
60 ['<Article: First>', '<Article: Second>', '<Article: Third>']) |
|
61 # Remove an article from the set, and check that it was removed. |
|
62 self.r.article_set.remove(a3) |
|
63 self.assertQuerysetEqual(self.r.article_set.all(), |
|
64 ['<Article: First>', '<Article: Second>']) |
|
65 self.assertQuerysetEqual(Article.objects.filter(reporter__isnull=True), |
|
66 ['<Article: Third>']) |
|
67 |
|
68 def test_remove_from_wrong_set(self): |
|
69 self.assertQuerysetEqual(self.r2.article_set.all(), ['<Article: Fourth>']) |
|
70 # Try to remove a4 from a set it does not belong to |
|
71 self.assertRaises(Reporter.DoesNotExist, self.r.article_set.remove, self.a4) |
|
72 self.assertQuerysetEqual(self.r2.article_set.all(), ['<Article: Fourth>']) |
|
73 |
|
74 def test_assign_clear_related_set(self): |
|
75 # Use descriptor assignment to allocate ForeignKey. Null is legal, so |
|
76 # existing members of set that are not in the assignment set are set null |
|
77 self.r2.article_set = [self.a2, self.a3] |
|
78 self.assertQuerysetEqual(self.r2.article_set.all(), |
|
79 ['<Article: Second>', '<Article: Third>']) |
|
80 # Clear the rest of the set |
|
81 self.r.article_set.clear() |
|
82 self.assertQuerysetEqual(self.r.article_set.all(), []) |
|
83 self.assertQuerysetEqual(Article.objects.filter(reporter__isnull=True), |
|
84 ['<Article: First>', '<Article: Fourth>']) |