2013-02-10 15:15:49 +00:00
|
|
|
import datetime
|
2015-10-13 18:25:06 +00:00
|
|
|
from unittest import skipUnless
|
2010-11-04 16:03:05 +00:00
|
|
|
|
2014-11-18 09:24:33 +00:00
|
|
|
from django.core.exceptions import FieldError
|
2015-10-13 18:25:06 +00:00
|
|
|
from django.db import connection
|
|
|
|
from django.test import TestCase, override_settings
|
2010-11-04 16:03:05 +00:00
|
|
|
|
2015-01-28 12:35:27 +00:00
|
|
|
from .models import Article, Category, Comment
|
2010-11-04 16:03:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DatesTests(TestCase):
|
|
|
|
def test_related_model_traverse(self):
|
|
|
|
a1 = Article.objects.create(
|
|
|
|
title="First one",
|
2013-02-10 15:15:49 +00:00
|
|
|
pub_date=datetime.date(2005, 7, 28),
|
2010-11-04 16:03:05 +00:00
|
|
|
)
|
|
|
|
a2 = Article.objects.create(
|
|
|
|
title="Another one",
|
2013-02-10 15:15:49 +00:00
|
|
|
pub_date=datetime.date(2010, 7, 28),
|
2010-11-04 16:03:05 +00:00
|
|
|
)
|
|
|
|
a3 = Article.objects.create(
|
|
|
|
title="Third one, in the first day",
|
2013-02-10 15:15:49 +00:00
|
|
|
pub_date=datetime.date(2005, 7, 28),
|
2010-11-04 16:03:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
a1.comments.create(
|
|
|
|
text="Im the HULK!",
|
2013-02-10 15:15:49 +00:00
|
|
|
pub_date=datetime.date(2005, 7, 28),
|
2010-11-04 16:03:05 +00:00
|
|
|
)
|
|
|
|
a1.comments.create(
|
|
|
|
text="HULK SMASH!",
|
2013-02-10 15:15:49 +00:00
|
|
|
pub_date=datetime.date(2005, 7, 29),
|
2010-11-04 16:03:05 +00:00
|
|
|
)
|
|
|
|
a2.comments.create(
|
|
|
|
text="LMAO",
|
2013-02-10 15:15:49 +00:00
|
|
|
pub_date=datetime.date(2010, 7, 28),
|
2010-11-04 16:03:05 +00:00
|
|
|
)
|
|
|
|
a3.comments.create(
|
|
|
|
text="+1",
|
2013-02-10 15:15:49 +00:00
|
|
|
pub_date=datetime.date(2005, 8, 29),
|
2010-11-04 16:03:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
c = Category.objects.create(name="serious-news")
|
|
|
|
c.articles.add(a1, a3)
|
|
|
|
|
2016-09-10 09:36:27 +00:00
|
|
|
self.assertSequenceEqual(
|
2010-11-04 16:03:05 +00:00
|
|
|
Comment.objects.dates("article__pub_date", "year"), [
|
2013-02-10 15:15:49 +00:00
|
|
|
datetime.date(2005, 1, 1),
|
|
|
|
datetime.date(2010, 1, 1),
|
2010-11-04 16:03:05 +00:00
|
|
|
],
|
|
|
|
)
|
2016-09-10 09:36:27 +00:00
|
|
|
self.assertSequenceEqual(
|
2010-11-04 16:03:05 +00:00
|
|
|
Comment.objects.dates("article__pub_date", "month"), [
|
2013-02-10 15:15:49 +00:00
|
|
|
datetime.date(2005, 7, 1),
|
|
|
|
datetime.date(2010, 7, 1),
|
2010-11-04 16:03:05 +00:00
|
|
|
],
|
|
|
|
)
|
2017-09-28 20:39:03 +00:00
|
|
|
self.assertSequenceEqual(
|
|
|
|
Comment.objects.dates("article__pub_date", "week"), [
|
|
|
|
datetime.date(2005, 7, 25),
|
|
|
|
datetime.date(2010, 7, 26),
|
|
|
|
],
|
|
|
|
)
|
2016-09-10 09:36:27 +00:00
|
|
|
self.assertSequenceEqual(
|
2010-11-04 16:03:05 +00:00
|
|
|
Comment.objects.dates("article__pub_date", "day"), [
|
2013-02-10 15:15:49 +00:00
|
|
|
datetime.date(2005, 7, 28),
|
|
|
|
datetime.date(2010, 7, 28),
|
2010-11-04 16:03:05 +00:00
|
|
|
],
|
|
|
|
)
|
2016-09-10 09:36:27 +00:00
|
|
|
self.assertSequenceEqual(
|
2010-11-04 16:03:05 +00:00
|
|
|
Article.objects.dates("comments__pub_date", "day"), [
|
2013-02-10 15:15:49 +00:00
|
|
|
datetime.date(2005, 7, 28),
|
|
|
|
datetime.date(2005, 7, 29),
|
|
|
|
datetime.date(2005, 8, 29),
|
|
|
|
datetime.date(2010, 7, 28),
|
2010-11-04 16:03:05 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Article.objects.dates("comments__approval_date", "day"), []
|
|
|
|
)
|
2016-09-10 09:36:27 +00:00
|
|
|
self.assertSequenceEqual(
|
2010-11-04 16:03:05 +00:00
|
|
|
Category.objects.dates("articles__pub_date", "day"), [
|
2013-02-10 15:15:49 +00:00
|
|
|
datetime.date(2005, 7, 28),
|
2010-11-04 16:03:05 +00:00
|
|
|
],
|
|
|
|
)
|
2014-05-16 09:55:09 +00:00
|
|
|
|
|
|
|
def test_dates_fails_when_no_arguments_are_provided(self):
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
Article.objects.dates()
|
2014-05-16 09:55:09 +00:00
|
|
|
|
|
|
|
def test_dates_fails_when_given_invalid_field_argument(self):
|
2019-04-28 13:15:19 +00:00
|
|
|
with self.assertRaisesMessage(
|
2014-11-18 09:24:33 +00:00
|
|
|
FieldError,
|
2016-09-17 13:29:14 +00:00
|
|
|
"Cannot resolve keyword 'invalid_field' into field. Choices are: "
|
2015-10-13 18:25:06 +00:00
|
|
|
"categories, comments, id, pub_date, pub_datetime, title",
|
2019-04-28 13:15:19 +00:00
|
|
|
):
|
|
|
|
Article.objects.dates('invalid_field', 'year')
|
2014-05-16 09:55:09 +00:00
|
|
|
|
|
|
|
def test_dates_fails_when_given_invalid_kind_argument(self):
|
2017-09-28 20:39:03 +00:00
|
|
|
msg = "'kind' must be one of 'year', 'month', 'week', or 'day'."
|
|
|
|
with self.assertRaisesMessage(AssertionError, msg):
|
2016-01-18 08:45:45 +00:00
|
|
|
Article.objects.dates("pub_date", "bad_kind")
|
2014-05-16 09:55:09 +00:00
|
|
|
|
|
|
|
def test_dates_fails_when_given_invalid_order_argument(self):
|
2016-01-18 08:45:45 +00:00
|
|
|
with self.assertRaisesMessage(AssertionError, "'order' must be either 'ASC' or 'DESC'."):
|
|
|
|
Article.objects.dates("pub_date", "year", order="bad order")
|
2015-10-13 18:25:06 +00:00
|
|
|
|
|
|
|
@override_settings(USE_TZ=False)
|
|
|
|
def test_dates_trunc_datetime_fields(self):
|
|
|
|
Article.objects.bulk_create(
|
|
|
|
Article(pub_date=pub_datetime.date(), pub_datetime=pub_datetime)
|
|
|
|
for pub_datetime in [
|
|
|
|
datetime.datetime(2015, 10, 21, 18, 1),
|
|
|
|
datetime.datetime(2015, 10, 21, 18, 2),
|
|
|
|
datetime.datetime(2015, 10, 22, 18, 1),
|
|
|
|
datetime.datetime(2015, 10, 22, 18, 2),
|
|
|
|
]
|
|
|
|
)
|
2016-09-10 09:36:27 +00:00
|
|
|
self.assertSequenceEqual(
|
2015-10-13 18:25:06 +00:00
|
|
|
Article.objects.dates('pub_datetime', 'day', order='ASC'), [
|
2016-09-10 09:36:27 +00:00
|
|
|
datetime.date(2015, 10, 21),
|
|
|
|
datetime.date(2015, 10, 22),
|
2015-10-13 18:25:06 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
@skipUnless(connection.vendor == 'mysql', "Test checks MySQL query syntax")
|
|
|
|
def test_dates_avoid_datetime_cast(self):
|
|
|
|
Article.objects.create(pub_date=datetime.date(2015, 10, 21))
|
|
|
|
for kind in ['day', 'month', 'year']:
|
|
|
|
qs = Article.objects.dates('pub_date', kind)
|
|
|
|
if kind == 'day':
|
|
|
|
self.assertIn('DATE(', str(qs.query))
|
|
|
|
else:
|
|
|
|
self.assertIn(' AS DATE)', str(qs.query))
|