2013-07-30 01:19:04 +08:00
|
|
|
from __future__ import unicode_literals
|
2011-10-14 02:51:33 +08:00
|
|
|
|
2013-02-10 23:15:49 +08:00
|
|
|
import datetime
|
2010-11-05 00:03:05 +08:00
|
|
|
|
2014-05-16 17:55:09 +08:00
|
|
|
from django.db.models.fields import FieldDoesNotExist
|
2010-11-05 00:03:05 +08:00
|
|
|
from django.test import TestCase
|
2014-05-16 17:55:09 +08:00
|
|
|
from django.utils import six
|
2010-11-05 00:03:05 +08:00
|
|
|
|
2011-10-14 02:51:33 +08:00
|
|
|
from .models import Article, Comment, Category
|
2010-11-05 00:03:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
class DatesTests(TestCase):
|
|
|
|
def test_related_model_traverse(self):
|
|
|
|
a1 = Article.objects.create(
|
|
|
|
title="First one",
|
2013-02-10 23:15:49 +08:00
|
|
|
pub_date=datetime.date(2005, 7, 28),
|
2010-11-05 00:03:05 +08:00
|
|
|
)
|
|
|
|
a2 = Article.objects.create(
|
|
|
|
title="Another one",
|
2013-02-10 23:15:49 +08:00
|
|
|
pub_date=datetime.date(2010, 7, 28),
|
2010-11-05 00:03:05 +08:00
|
|
|
)
|
|
|
|
a3 = Article.objects.create(
|
|
|
|
title="Third one, in the first day",
|
2013-02-10 23:15:49 +08:00
|
|
|
pub_date=datetime.date(2005, 7, 28),
|
2010-11-05 00:03:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
a1.comments.create(
|
|
|
|
text="Im the HULK!",
|
2013-02-10 23:15:49 +08:00
|
|
|
pub_date=datetime.date(2005, 7, 28),
|
2010-11-05 00:03:05 +08:00
|
|
|
)
|
|
|
|
a1.comments.create(
|
|
|
|
text="HULK SMASH!",
|
2013-02-10 23:15:49 +08:00
|
|
|
pub_date=datetime.date(2005, 7, 29),
|
2010-11-05 00:03:05 +08:00
|
|
|
)
|
|
|
|
a2.comments.create(
|
|
|
|
text="LMAO",
|
2013-02-10 23:15:49 +08:00
|
|
|
pub_date=datetime.date(2010, 7, 28),
|
2010-11-05 00:03:05 +08:00
|
|
|
)
|
|
|
|
a3.comments.create(
|
|
|
|
text="+1",
|
2013-02-10 23:15:49 +08:00
|
|
|
pub_date=datetime.date(2005, 8, 29),
|
2010-11-05 00:03:05 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
c = Category.objects.create(name="serious-news")
|
|
|
|
c.articles.add(a1, a3)
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Comment.objects.dates("article__pub_date", "year"), [
|
2013-02-10 23:15:49 +08:00
|
|
|
datetime.date(2005, 1, 1),
|
|
|
|
datetime.date(2010, 1, 1),
|
2010-11-05 00:03:05 +08:00
|
|
|
],
|
|
|
|
lambda d: d,
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Comment.objects.dates("article__pub_date", "month"), [
|
2013-02-10 23:15:49 +08:00
|
|
|
datetime.date(2005, 7, 1),
|
|
|
|
datetime.date(2010, 7, 1),
|
2010-11-05 00:03:05 +08:00
|
|
|
],
|
|
|
|
lambda d: d
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Comment.objects.dates("article__pub_date", "day"), [
|
2013-02-10 23:15:49 +08:00
|
|
|
datetime.date(2005, 7, 28),
|
|
|
|
datetime.date(2010, 7, 28),
|
2010-11-05 00:03:05 +08:00
|
|
|
],
|
|
|
|
lambda d: d
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Article.objects.dates("comments__pub_date", "day"), [
|
2013-02-10 23:15:49 +08:00
|
|
|
datetime.date(2005, 7, 28),
|
|
|
|
datetime.date(2005, 7, 29),
|
|
|
|
datetime.date(2005, 8, 29),
|
|
|
|
datetime.date(2010, 7, 28),
|
2010-11-05 00:03:05 +08:00
|
|
|
],
|
|
|
|
lambda d: d
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Article.objects.dates("comments__approval_date", "day"), []
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Category.objects.dates("articles__pub_date", "day"), [
|
2013-02-10 23:15:49 +08:00
|
|
|
datetime.date(2005, 7, 28),
|
2010-11-05 00:03:05 +08:00
|
|
|
],
|
|
|
|
lambda d: d,
|
|
|
|
)
|
2014-05-16 17:55:09 +08:00
|
|
|
|
|
|
|
def test_dates_fails_when_no_arguments_are_provided(self):
|
|
|
|
self.assertRaises(
|
|
|
|
TypeError,
|
|
|
|
Article.objects.dates,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_dates_fails_when_given_invalid_field_argument(self):
|
|
|
|
six.assertRaisesRegex(
|
|
|
|
self,
|
|
|
|
FieldDoesNotExist,
|
|
|
|
"Article has no field named 'invalid_field'",
|
|
|
|
Article.objects.dates,
|
|
|
|
"invalid_field",
|
|
|
|
"year",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_dates_fails_when_given_invalid_kind_argument(self):
|
|
|
|
six.assertRaisesRegex(
|
|
|
|
self,
|
|
|
|
AssertionError,
|
|
|
|
"'kind' must be one of 'year', 'month' or 'day'.",
|
|
|
|
Article.objects.dates,
|
|
|
|
"pub_date",
|
|
|
|
"bad_kind",
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_dates_fails_when_given_invalid_order_argument(self):
|
|
|
|
six.assertRaisesRegex(
|
|
|
|
self,
|
|
|
|
AssertionError,
|
|
|
|
"'order' must be either 'ASC' or 'DESC'.",
|
|
|
|
Article.objects.dates,
|
|
|
|
"pub_date",
|
|
|
|
"year",
|
|
|
|
order="bad order",
|
|
|
|
)
|