tests.py 4.0 KB

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