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.assertQuerysetEqual(
    
  21.             Author.objects.all(),
    
  22.             [
    
  23.                 "Peter Jones",
    
  24.                 "John Smith",
    
  25.             ],
    
  26.             str,
    
  27.         )
    
  28. 
    
  29.     def test_get_first_name(self):
    
  30.         self.assertEqual(
    
  31.             Author.objects.get(first_name__exact="John"),
    
  32.             self.a1,
    
  33.         )
    
  34. 
    
  35.     def test_filter_first_name(self):
    
  36.         self.assertQuerysetEqual(
    
  37.             Author.objects.filter(first_name__exact="John"),
    
  38.             [
    
  39.                 "John Smith",
    
  40.             ],
    
  41.             str,
    
  42.         )
    
  43. 
    
  44.     def test_field_error(self):
    
  45.         msg = (
    
  46.             "Cannot resolve keyword 'firstname' into field. Choices are: "
    
  47.             "Author_ID, article, first_name, last_name, primary_set"
    
  48.         )
    
  49.         with self.assertRaisesMessage(FieldError, msg):
    
  50.             Author.objects.filter(firstname__exact="John")
    
  51. 
    
  52.     def test_attribute_error(self):
    
  53.         with self.assertRaises(AttributeError):
    
  54.             self.a1.firstname
    
  55. 
    
  56.         with self.assertRaises(AttributeError):
    
  57.             self.a1.last
    
  58. 
    
  59.     def test_get_all_authors_for_an_article(self):
    
  60.         self.assertQuerysetEqual(
    
  61.             self.article.authors.all(),
    
  62.             [
    
  63.                 "Peter Jones",
    
  64.                 "John Smith",
    
  65.             ],
    
  66.             str,
    
  67.         )
    
  68. 
    
  69.     def test_get_all_articles_for_an_author(self):
    
  70.         self.assertQuerysetEqual(
    
  71.             self.a1.article_set.all(),
    
  72.             [
    
  73.                 "Django lets you build web apps easily",
    
  74.             ],
    
  75.             lambda a: a.headline,
    
  76.         )
    
  77. 
    
  78.     def test_get_author_m2m_relation(self):
    
  79.         self.assertQuerysetEqual(
    
  80.             self.article.authors.filter(last_name="Jones"), ["Peter Jones"], str
    
  81.         )
    
  82. 
    
  83.     def test_author_querying(self):
    
  84.         self.assertSequenceEqual(
    
  85.             Author.objects.order_by("last_name"),
    
  86.             [self.a2, self.a1],
    
  87.         )
    
  88. 
    
  89.     def test_author_filtering(self):
    
  90.         self.assertSequenceEqual(
    
  91.             Author.objects.filter(first_name__exact="John"),
    
  92.             [self.a1],
    
  93.         )
    
  94. 
    
  95.     def test_author_get(self):
    
  96.         self.assertEqual(self.a1, Author.objects.get(first_name__exact="John"))
    
  97. 
    
  98.     def test_filter_on_nonexistent_field(self):
    
  99.         msg = (
    
  100.             "Cannot resolve keyword 'firstname' into field. Choices are: "
    
  101.             "Author_ID, article, first_name, last_name, primary_set"
    
  102.         )
    
  103.         with self.assertRaisesMessage(FieldError, msg):
    
  104.             Author.objects.filter(firstname__exact="John")
    
  105. 
    
  106.     def test_author_get_attributes(self):
    
  107.         a = Author.objects.get(last_name__exact="Smith")
    
  108.         self.assertEqual("John", a.first_name)
    
  109.         self.assertEqual("Smith", a.last_name)
    
  110.         with self.assertRaisesMessage(
    
  111.             AttributeError, "'Author' object has no attribute 'firstname'"
    
  112.         ):
    
  113.             getattr(a, "firstname")
    
  114. 
    
  115.         with self.assertRaisesMessage(
    
  116.             AttributeError, "'Author' object has no attribute 'last'"
    
  117.         ):
    
  118.             getattr(a, "last")
    
  119. 
    
  120.     def test_m2m_table(self):
    
  121.         self.assertSequenceEqual(
    
  122.             self.article.authors.order_by("last_name"),
    
  123.             [self.a2, self.a1],
    
  124.         )
    
  125.         self.assertSequenceEqual(self.a1.article_set.all(), [self.article])
    
  126.         self.assertSequenceEqual(
    
  127.             self.article.authors.filter(last_name="Jones"),
    
  128.             [self.a2],
    
  129.         )