summaryrefslogtreecommitdiff
path: root/tests/dates
diff options
context:
space:
mode:
authorFlorian Apolloner <florian@apolloner.eu>2013-02-26 09:53:47 +0100
committerFlorian Apolloner <florian@apolloner.eu>2013-02-26 14:36:57 +0100
commit89f40e36246100df6a11316c31a76712ebc6c501 (patch)
tree6e65639683ddaf2027908d1ecb1739e0e2ff853b /tests/dates
parentb3d2ccb5bfbaf6e7fe1f98843baaa48c35a70950 (diff)
downloaddjango-89f40e36246100df6a11316c31a76712ebc6c501.tar.gz
Merged regressiontests and modeltests into the test root.
Diffstat (limited to 'tests/dates')
-rw-r--r--tests/dates/__init__.py0
-rw-r--r--tests/dates/models.py28
-rw-r--r--tests/dates/tests.py83
3 files changed, 111 insertions, 0 deletions
diff --git a/tests/dates/__init__.py b/tests/dates/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/dates/__init__.py
diff --git a/tests/dates/models.py b/tests/dates/models.py
new file mode 100644
index 0000000000..23350755e7
--- /dev/null
+++ b/tests/dates/models.py
@@ -0,0 +1,28 @@
+from __future__ import unicode_literals
+
+from django.db import models
+from django.utils.encoding import python_2_unicode_compatible
+
+
+@python_2_unicode_compatible
+class Article(models.Model):
+ title = models.CharField(max_length=100)
+ pub_date = models.DateField()
+
+ categories = models.ManyToManyField("Category", related_name="articles")
+
+ def __str__(self):
+ return self.title
+
+@python_2_unicode_compatible
+class Comment(models.Model):
+ article = models.ForeignKey(Article, related_name="comments")
+ text = models.TextField()
+ pub_date = models.DateField()
+ approval_date = models.DateField(null=True)
+
+ def __str__(self):
+ return 'Comment to %s (%s)' % (self.article.title, self.pub_date)
+
+class Category(models.Model):
+ name = models.CharField(max_length=255)
diff --git a/tests/dates/tests.py b/tests/dates/tests.py
new file mode 100644
index 0000000000..6c02d597de
--- /dev/null
+++ b/tests/dates/tests.py
@@ -0,0 +1,83 @@
+from __future__ import absolute_import
+
+import datetime
+
+from django.test import TestCase
+
+from .models import Article, Comment, Category
+
+
+class DatesTests(TestCase):
+ def test_related_model_traverse(self):
+ a1 = Article.objects.create(
+ title="First one",
+ pub_date=datetime.date(2005, 7, 28),
+ )
+ a2 = Article.objects.create(
+ title="Another one",
+ pub_date=datetime.date(2010, 7, 28),
+ )
+ a3 = Article.objects.create(
+ title="Third one, in the first day",
+ pub_date=datetime.date(2005, 7, 28),
+ )
+
+ a1.comments.create(
+ text="Im the HULK!",
+ pub_date=datetime.date(2005, 7, 28),
+ )
+ a1.comments.create(
+ text="HULK SMASH!",
+ pub_date=datetime.date(2005, 7, 29),
+ )
+ a2.comments.create(
+ text="LMAO",
+ pub_date=datetime.date(2010, 7, 28),
+ )
+ a3.comments.create(
+ text="+1",
+ pub_date=datetime.date(2005, 8, 29),
+ )
+
+ c = Category.objects.create(name="serious-news")
+ c.articles.add(a1, a3)
+
+ self.assertQuerysetEqual(
+ Comment.objects.dates("article__pub_date", "year"), [
+ datetime.date(2005, 1, 1),
+ datetime.date(2010, 1, 1),
+ ],
+ lambda d: d,
+ )
+ self.assertQuerysetEqual(
+ Comment.objects.dates("article__pub_date", "month"), [
+ datetime.date(2005, 7, 1),
+ datetime.date(2010, 7, 1),
+ ],
+ lambda d: d
+ )
+ self.assertQuerysetEqual(
+ Comment.objects.dates("article__pub_date", "day"), [
+ datetime.date(2005, 7, 28),
+ datetime.date(2010, 7, 28),
+ ],
+ lambda d: d
+ )
+ self.assertQuerysetEqual(
+ Article.objects.dates("comments__pub_date", "day"), [
+ datetime.date(2005, 7, 28),
+ datetime.date(2005, 7, 29),
+ datetime.date(2005, 8, 29),
+ datetime.date(2010, 7, 28),
+ ],
+ lambda d: d
+ )
+ self.assertQuerysetEqual(
+ Article.objects.dates("comments__approval_date", "day"), []
+ )
+ self.assertQuerysetEqual(
+ Category.objects.dates("articles__pub_date", "day"), [
+ datetime.date(2005, 7, 28),
+ ],
+ lambda d: d,
+ )