tests.py 3.9 KB

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