PageRenderTime 18ms CodeModel.GetById 7ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/dates/tests.py

https://code.google.com/p/mango-py/
Python | 81 lines | 74 code | 7 blank | 0 comment | 0 complexity | ce03898d9efe01389f4f382a56619f97 MD5 | raw file
 1from datetime import datetime
 2
 3from django.test import TestCase
 4
 5from models import Article, Comment, Category
 6
 7
 8class DatesTests(TestCase):
 9    def test_related_model_traverse(self):
10        a1 = Article.objects.create(
11            title="First one",
12            pub_date=datetime(2005, 7, 28),
13        )
14        a2 = Article.objects.create(
15            title="Another one",
16            pub_date=datetime(2010, 7, 28),
17        )
18        a3 = Article.objects.create(
19            title="Third one, in the first day",
20            pub_date=datetime(2005, 7, 28),
21        )
22
23        a1.comments.create(
24            text="Im the HULK!",
25            pub_date=datetime(2005, 7, 28),
26        )
27        a1.comments.create(
28            text="HULK SMASH!",
29            pub_date=datetime(2005, 7, 29),
30        )
31        a2.comments.create(
32            text="LMAO",
33            pub_date=datetime(2010, 7, 28),
34        )
35        a3.comments.create(
36            text="+1",
37            pub_date=datetime(2005, 8, 29),
38        )
39
40        c = Category.objects.create(name="serious-news")
41        c.articles.add(a1, a3)
42
43        self.assertQuerysetEqual(
44            Comment.objects.dates("article__pub_date", "year"), [
45                datetime(2005, 1, 1),
46                datetime(2010, 1, 1),
47            ],
48            lambda d: d,
49        )
50        self.assertQuerysetEqual(
51            Comment.objects.dates("article__pub_date", "month"), [
52                datetime(2005, 7, 1),
53                datetime(2010, 7, 1),
54            ],
55            lambda d: d
56        )
57        self.assertQuerysetEqual(
58            Comment.objects.dates("article__pub_date", "day"), [
59                datetime(2005, 7, 28),
60                datetime(2010, 7, 28),
61            ],
62            lambda d: d
63        )
64        self.assertQuerysetEqual(
65            Article.objects.dates("comments__pub_date", "day"), [
66                datetime(2005, 7, 28),
67                datetime(2005, 7, 29),
68                datetime(2005, 8, 29),
69                datetime(2010, 7, 28),
70            ],
71            lambda d: d
72        )
73        self.assertQuerysetEqual(
74            Article.objects.dates("comments__approval_date", "day"), []
75        )
76        self.assertQuerysetEqual(
77            Category.objects.dates("articles__pub_date", "day"), [
78                datetime(2005, 7, 28),
79            ],
80            lambda d: d,
81        )