| 10 | |
| 11 | class DatesTests(TestCase): |
| 12 | def test_related_model_traverse(self): |
| 13 | a1 = Article.objects.create( |
| 14 | title="First one", |
| 15 | pub_date=datetime.date(2005, 7, 28), |
| 16 | ) |
| 17 | a2 = Article.objects.create( |
| 18 | title="Another one", |
| 19 | pub_date=datetime.date(2010, 7, 28), |
| 20 | ) |
| 21 | a3 = Article.objects.create( |
| 22 | title="Third one, in the first day", |
| 23 | pub_date=datetime.date(2005, 7, 28), |
| 24 | ) |
| 25 | |
| 26 | a1.comments.create( |
| 27 | text="Im the HULK!", |
| 28 | pub_date=datetime.date(2005, 7, 28), |
| 29 | ) |
| 30 | a1.comments.create( |
| 31 | text="HULK SMASH!", |
| 32 | pub_date=datetime.date(2005, 7, 29), |
| 33 | ) |
| 34 | a2.comments.create( |
| 35 | text="LMAO", |
| 36 | pub_date=datetime.date(2010, 7, 28), |
| 37 | ) |
| 38 | a3.comments.create( |
| 39 | text="+1", |
| 40 | pub_date=datetime.date(2005, 8, 29), |
| 41 | ) |
| 42 | |
| 43 | c = Category.objects.create(name="serious-news") |
| 44 | c.articles.add(a1, a3) |
| 45 | |
| 46 | self.assertSequenceEqual( |
| 47 | Comment.objects.dates("article__pub_date", "year"), |
| 48 | [ |
| 49 | datetime.date(2005, 1, 1), |
| 50 | datetime.date(2010, 1, 1), |
| 51 | ], |
| 52 | ) |
| 53 | self.assertSequenceEqual( |
| 54 | Comment.objects.dates("article__pub_date", "month"), |
| 55 | [ |
| 56 | datetime.date(2005, 7, 1), |
| 57 | datetime.date(2010, 7, 1), |
| 58 | ], |
| 59 | ) |
| 60 | self.assertSequenceEqual( |
| 61 | Comment.objects.dates("article__pub_date", "week"), |
| 62 | [ |
| 63 | datetime.date(2005, 7, 25), |
| 64 | datetime.date(2010, 7, 26), |
| 65 | ], |
| 66 | ) |
| 67 | self.assertSequenceEqual( |
| 68 | Comment.objects.dates("article__pub_date", "day"), |
| 69 | [ |