Ticket #13935: 13935_tests.diff

File 13935_tests.diff, 3.4 KB (added by Valentin Golev, 14 years ago)

tests

  • modeltests/dates/models.py

     
     1# coding: utf-8
     2"""
     31. Tests for ticket #13935 (QuerySet .dates() method should span relationships)
     4
     5There are two models, one-to-many, and .dates() on second one with reference to first one field must succeed
     6"""
     7from django.db import models, DEFAULT_DB_ALIAS
     8
     9class Article(models.Model):
     10    title = models.CharField(max_length=100)
     11    pub_date = models.DateField()
     12    pub_date_time = models.DateTimeField()
     13
     14    def __unicode__(self):
     15        return self.title
     16
     17class Comment(models.Model):
     18    article = models.ForeignKey(Article)
     19    text = models.TextField()
     20    pub_date = models.DateField()
     21    pub_date_time = models.DateTimeField()
     22
     23    def __unicode__(self):
     24        return 'Comment to %s (%s)' % (self.article.title, self.pub_date_time)
     25
     26
     27__test__ = {'API_TESTS': """
     28# first, some data
     29>>> from datetime import datetime
     30>>> a1 = Article(title="First one", pub_date=datetime(2005, 7, 28), pub_date_time=datetime(2005, 7, 28))
     31>>> a1.save()
     32>>> a2 = Article(title="Another one", pub_date=datetime(2010, 7, 28), pub_date_time=datetime(2010, 7, 28))
     33>>> a2.save()
     34>>> a3 = Article(title="Third one, in the first day", pub_date=datetime(2005, 7, 28), pub_date_time=datetime(2005, 7, 28))
     35>>> a3.save()
     36
     37# are we sane?
     38>>> Article.objects.all()
     39[<Article: First one>, <Article: Another one>, <Article: Third one, in the first day>]
     40
     41# awesome! some comments
     42>>> c1 = Comment(text="Im the HULK!", article=a1, pub_date=datetime(2005, 7, 28), pub_date_time=datetime(2005, 7, 28))
     43>>> c1.save()
     44>>> c2 = Comment(text="HULK SMASH!", article=a1, pub_date=datetime(2005, 7, 29), pub_date_time=datetime(2005, 7, 29))
     45>>> c2.save()
     46>>> c3 = Comment(text="LMAO", article=a2, pub_date=datetime(2010, 7, 28), pub_date_time=datetime(2010, 7, 28))
     47>>> c3.save()
     48>>> c4 = Comment(text="+1", article=a3, pub_date=datetime(2005, 8, 29), pub_date_time=datetime(2005, 8, 29))
     49>>> c4.save()
     50
     51# good?
     52>>> Comment.objects.all()
     53[<Comment: Comment to First one (2005-07-28 00:00:00)>, <Comment: Comment to First one (2005-07-29 00:00:00)>, <Comment: Comment to Another one (2010-07-28 00:00:00)>, <Comment: Comment to Third one, in the first day (2005-08-29 00:00:00)>]
     54
     55# okay, we are ready now. let's go straight to the point
     56>>> Comment.objects.dates('pub_date', 'year')
     57[datetime.datetime(2005, 1, 1, 0, 0), datetime.datetime(2010, 1, 1, 0, 0)]
     58>>> Comment.objects.dates('pub_date', 'month')
     59[datetime.datetime(2005, 7, 1, 0, 0), datetime.datetime(2005, 8, 1, 0, 0), datetime.datetime(2010, 7, 1, 0, 0)]
     60>>> Comment.objects.dates('pub_date', 'day')
     61[datetime.datetime(2005, 7, 28, 0, 0), datetime.datetime(2005, 7, 29, 0, 0), datetime.datetime(2005, 8, 29, 0, 0), datetime.datetime(2010, 7, 28, 0, 0)]
     62
     63# that was expected. now, something not so easy?
     64>>> Comment.objects.dates('article__pub_date', 'year')
     65[datetime.datetime(2005, 1, 1, 0, 0), datetime.datetime(2010, 1, 1, 0, 0)]
     66>>> Comment.objects.dates('article__pub_date', 'month')
     67[datetime.datetime(2005, 7, 1, 0, 0), datetime.datetime(2010, 7, 1, 0, 0)]
     68>>> Comment.objects.dates('article__pub_date', 'day')
     69[datetime.datetime(2005, 7, 28, 0, 0), datetime.datetime(2010, 7, 28, 0, 0)]
     70"""}
     71
Back to Top