Debian Codegraph @

tests/custom_columns/tests.py

raw
1 from django.core.exceptions import FieldError
2 from django.test import TestCase
3
4 from .models import Article, Author
5
6
7 class CustomColumnsTests(TestCase):
8 @classmethod
9 def setUpTestData(cls):
10 cls.a1 = Author.objects.create(first_name="John", last_name="Smith")
11 cls.a2 = Author.objects.create(first_name="Peter", last_name="Jones")
12 cls.authors = [cls.a1, cls.a2]
13
14 cls.article = Article.objects.create(
15 headline="Django lets you build web apps easily", primary_author=cls.a1
16 )
17 cls.article.authors.set(cls.authors)
18
19 def test_query_all_available_authors(self):
20 self.assertSequenceEqual(Author.objects.all(), [self.a2, self.a1])
21
22 def test_get_first_name(self):
23 self.assertEqual(
24 Author.objects.get(first_name__exact="John"),
25 self.a1,
26 )
27
28 def test_filter_first_name(self):
29 self.assertSequenceEqual(
30 Author.objects.filter(first_name__exact="John"),
31 [self.a1],
32 )
33
34 def test_field_error(self):
35 msg = (
36 "Cannot resolve keyword 'firstname' into field. Choices are: "
37 "Author_ID, article, first_name, last_name, primary_set"
38 )
39 with self.assertRaisesMessage(FieldError, msg):
40 Author.objects.filter(firstname__exact="John")
41
42 def test_attribute_error(self):
43 with self.assertRaises(AttributeError):
44 self.a1.firstname
45
46 with self.assertRaises(AttributeError):
47 self.a1.last
48
49 def test_get_all_authors_for_an_article(self):
50 self.assertSequenceEqual(self.article.authors.all(), [self.a2, self.a1])
51
52 def test_get_all_articles_for_an_author(self):
53 self.assertQuerySetEqual(
54 self.a1.article_set.all(),
55 [
56 "Django lets you build web apps easily",
57 ],
58 lambda a: a.headline,
59 )
60
61 def test_get_author_m2m_relation(self):
62 self.assertSequenceEqual(
63 self.article.authors.filter(last_name="Jones"), [self.a2]
64 )
65
66 def test_author_querying(self):
67 self.assertSequenceEqual(
68 Author.objects.order_by("last_name"),
69 [self.a2, self.a1],
70 )
71
72 def test_author_filtering(self):
73 self.assertSequenceEqual(
74 Author.objects.filter(first_name__exact="John"),
75 [self.a1],
76 )
77
78 def test_author_get(self):
79 self.assertEqual(self.a1, Author.objects.get(first_name__exact="John"))
80
81 def test_filter_on_nonexistent_field(self):
82 msg = (
83 "Cannot resolve keyword 'firstname' into field. Choices are: "
84 "Author_ID, article, first_name, last_name, primary_set"
85 )
86 with self.assertRaisesMessage(FieldError, msg):
87 Author.objects.filter(firstname__exact="John")
88
89 def test_author_get_attributes(self):
90 a = Author.objects.get(last_name__exact="Smith")
91 self.assertEqual("John", a.first_name)
92 self.assertEqual("Smith", a.last_name)
93 with self.assertRaisesMessage(
94 AttributeError, "'Author' object has no attribute 'firstname'"
95 ):
96 getattr(a, "firstname")
97
98 with self.assertRaisesMessage(
99 AttributeError, "'Author' object has no attribute 'last'"
100 ):
101 getattr(a, "last")
102
103 def test_m2m_table(self):
104 self.assertSequenceEqual(
105 self.article.authors.order_by("last_name"),
106 [self.a2, self.a1],
107 )
108 self.assertSequenceEqual(self.a1.article_set.all(), [self.article])
109 self.assertSequenceEqual(
110 self.article.authors.filter(last_name="Jones"),
111 [self.a2],
112 )