tests.py 3.65 KB
Newer Older
1
from __future__ import unicode_literals
2

3
import datetime
4

5
from django.core.exceptions import FieldError
6
from django.test import TestCase
7
from django.utils import six
8

9
from .models import Article, Category, Comment
10 11 12 13 14 15


class DatesTests(TestCase):
    def test_related_model_traverse(self):
        a1 = Article.objects.create(
            title="First one",
16
            pub_date=datetime.date(2005, 7, 28),
17 18 19
        )
        a2 = Article.objects.create(
            title="Another one",
20
            pub_date=datetime.date(2010, 7, 28),
21 22 23
        )
        a3 = Article.objects.create(
            title="Third one, in the first day",
24
            pub_date=datetime.date(2005, 7, 28),
25 26 27 28
        )

        a1.comments.create(
            text="Im the HULK!",
29
            pub_date=datetime.date(2005, 7, 28),
30 31 32
        )
        a1.comments.create(
            text="HULK SMASH!",
33
            pub_date=datetime.date(2005, 7, 29),
34 35 36
        )
        a2.comments.create(
            text="LMAO",
37
            pub_date=datetime.date(2010, 7, 28),
38 39 40
        )
        a3.comments.create(
            text="+1",
41
            pub_date=datetime.date(2005, 8, 29),
42 43 44 45 46 47 48
        )

        c = Category.objects.create(name="serious-news")
        c.articles.add(a1, a3)

        self.assertQuerysetEqual(
            Comment.objects.dates("article__pub_date", "year"), [
49 50
                datetime.date(2005, 1, 1),
                datetime.date(2010, 1, 1),
51 52 53 54 55
            ],
            lambda d: d,
        )
        self.assertQuerysetEqual(
            Comment.objects.dates("article__pub_date", "month"), [
56 57
                datetime.date(2005, 7, 1),
                datetime.date(2010, 7, 1),
58 59 60 61 62
            ],
            lambda d: d
        )
        self.assertQuerysetEqual(
            Comment.objects.dates("article__pub_date", "day"), [
63 64
                datetime.date(2005, 7, 28),
                datetime.date(2010, 7, 28),
65 66 67 68 69
            ],
            lambda d: d
        )
        self.assertQuerysetEqual(
            Article.objects.dates("comments__pub_date", "day"), [
70 71 72 73
                datetime.date(2005, 7, 28),
                datetime.date(2005, 7, 29),
                datetime.date(2005, 8, 29),
                datetime.date(2010, 7, 28),
74 75 76 77 78 79 80 81
            ],
            lambda d: d
        )
        self.assertQuerysetEqual(
            Article.objects.dates("comments__approval_date", "day"), []
        )
        self.assertQuerysetEqual(
            Category.objects.dates("articles__pub_date", "day"), [
82
                datetime.date(2005, 7, 28),
83 84 85
            ],
            lambda d: d,
        )
86 87 88 89 90 91 92 93 94 95

    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,
96 97 98
            FieldError,
            "Cannot resolve keyword u?'invalid_field' into field. Choices are: "
            "categories, comments, id, pub_date, title",
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
            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",
        )