tests.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import datetime
  2. from unittest import skipUnless
  3. from django.core.exceptions import FieldError
  4. from django.db import connection
  5. from django.test import TestCase, override_settings
  6. from .models import Article, Category, Comment
  7. class DatesTests(TestCase):
  8. def test_related_model_traverse(self):
  9. a1 = Article.objects.create(
  10. title="First one",
  11. pub_date=datetime.date(2005, 7, 28),
  12. )
  13. a2 = Article.objects.create(
  14. title="Another one",
  15. pub_date=datetime.date(2010, 7, 28),
  16. )
  17. a3 = Article.objects.create(
  18. title="Third one, in the first day",
  19. pub_date=datetime.date(2005, 7, 28),
  20. )
  21. a1.comments.create(
  22. text="Im the HULK!",
  23. pub_date=datetime.date(2005, 7, 28),
  24. )
  25. a1.comments.create(
  26. text="HULK SMASH!",
  27. pub_date=datetime.date(2005, 7, 29),
  28. )
  29. a2.comments.create(
  30. text="LMAO",
  31. pub_date=datetime.date(2010, 7, 28),
  32. )
  33. a3.comments.create(
  34. text="+1",
  35. pub_date=datetime.date(2005, 8, 29),
  36. )
  37. c = Category.objects.create(name="serious-news")
  38. c.articles.add(a1, a3)
  39. self.assertSequenceEqual(
  40. Comment.objects.dates("article__pub_date", "year"), [
  41. datetime.date(2005, 1, 1),
  42. datetime.date(2010, 1, 1),
  43. ],
  44. )
  45. self.assertSequenceEqual(
  46. Comment.objects.dates("article__pub_date", "month"), [
  47. datetime.date(2005, 7, 1),
  48. datetime.date(2010, 7, 1),
  49. ],
  50. )
  51. self.assertSequenceEqual(
  52. Comment.objects.dates("article__pub_date", "week"), [
  53. datetime.date(2005, 7, 25),
  54. datetime.date(2010, 7, 26),
  55. ],
  56. )
  57. self.assertSequenceEqual(
  58. Comment.objects.dates("article__pub_date", "day"), [
  59. datetime.date(2005, 7, 28),
  60. datetime.date(2010, 7, 28),
  61. ],
  62. )
  63. self.assertSequenceEqual(
  64. Article.objects.dates("comments__pub_date", "day"), [
  65. datetime.date(2005, 7, 28),
  66. datetime.date(2005, 7, 29),
  67. datetime.date(2005, 8, 29),
  68. datetime.date(2010, 7, 28),
  69. ],
  70. )
  71. self.assertQuerysetEqual(
  72. Article.objects.dates("comments__approval_date", "day"), []
  73. )
  74. self.assertSequenceEqual(
  75. Category.objects.dates("articles__pub_date", "day"), [
  76. datetime.date(2005, 7, 28),
  77. ],
  78. )
  79. def test_dates_fails_when_no_arguments_are_provided(self):
  80. with self.assertRaises(TypeError):
  81. Article.objects.dates()
  82. def test_dates_fails_when_given_invalid_field_argument(self):
  83. self.assertRaisesMessage(
  84. FieldError,
  85. "Cannot resolve keyword 'invalid_field' into field. Choices are: "
  86. "categories, comments, id, pub_date, pub_datetime, title",
  87. Article.objects.dates,
  88. "invalid_field",
  89. "year",
  90. )
  91. def test_dates_fails_when_given_invalid_kind_argument(self):
  92. msg = "'kind' must be one of 'year', 'month', 'week', or 'day'."
  93. with self.assertRaisesMessage(AssertionError, msg):
  94. Article.objects.dates("pub_date", "bad_kind")
  95. def test_dates_fails_when_given_invalid_order_argument(self):
  96. with self.assertRaisesMessage(AssertionError, "'order' must be either 'ASC' or 'DESC'."):
  97. Article.objects.dates("pub_date", "year", order="bad order")
  98. @override_settings(USE_TZ=False)
  99. def test_dates_trunc_datetime_fields(self):
  100. Article.objects.bulk_create(
  101. Article(pub_date=pub_datetime.date(), pub_datetime=pub_datetime)
  102. for pub_datetime in [
  103. datetime.datetime(2015, 10, 21, 18, 1),
  104. datetime.datetime(2015, 10, 21, 18, 2),
  105. datetime.datetime(2015, 10, 22, 18, 1),
  106. datetime.datetime(2015, 10, 22, 18, 2),
  107. ]
  108. )
  109. self.assertSequenceEqual(
  110. Article.objects.dates('pub_datetime', 'day', order='ASC'), [
  111. datetime.date(2015, 10, 21),
  112. datetime.date(2015, 10, 22),
  113. ]
  114. )
  115. @skipUnless(connection.vendor == 'mysql', "Test checks MySQL query syntax")
  116. def test_dates_avoid_datetime_cast(self):
  117. Article.objects.create(pub_date=datetime.date(2015, 10, 21))
  118. for kind in ['day', 'month', 'year']:
  119. qs = Article.objects.dates('pub_date', kind)
  120. if kind == 'day':
  121. self.assertIn('DATE(', str(qs.query))
  122. else:
  123. self.assertIn(' AS DATE)', str(qs.query))