Ticket #6888: month_tests.diff

File month_tests.diff, 4.1 KB (added by scottb, 16 years ago)

Tests

  • tests/regressiontests/views/tests/generic/date_based.py

     
    11# coding: utf-8
    22from django.test import TestCase
    3 from datetime import datetime 
     3from datetime import datetime, date
    44from datetime import timedelta
    5 from regressiontests.views.models import Article, Author
     5from regressiontests.views.models import Article, Author, Issue
    66
    77class ObjectDetailTest(TestCase):
    88    fixtures = ['testdata.json']
     
    6767        article.save()
    6868        response = self.client.get('/views/date_based/archive_month/2004/02/')
    6969        self.assertEqual(response.status_code, 404)
    70                  
    71          
    72  No newline at end of file
     70
     71    def test_archive_month_midnight(self):
     72        "Archive for month should not include item at start of next month"
     73        february_issues = [
     74            Issue.objects.create(pub_date=date(2008, 2, 1)),
     75            Issue.objects.create(pub_date=date(2008, 2, 10)),
     76            Issue.objects.create(pub_date=date(2008, 2, 20)),
     77            Issue.objects.create(pub_date=date(2008, 2, 29)),
     78        ]
     79        march_issues = [
     80            Issue.objects.create(pub_date=date(2008, 3, 1)),
     81            Issue.objects.create(pub_date=datetime(2008, 3, 1, 0, 0, 0)),
     82            Issue.objects.create(pub_date=date(2008, 3, 10)),
     83        ]
     84
     85        self.assertEqual(Issue.objects.filter(pub_date__month=2).count(), len(february_issues))
     86       
     87        response = self.client.get('/views/date_based/issues/archive_month/2008/02/')
     88        self.assertEqual(response.status_code, 200)
     89       
     90        object_list = response.context['object_list']
     91       
     92        self.assertEqual(len(object_list), len(february_issues))
     93        for issue in object_list:
     94            self.assertEqual(issue.pub_date.month, 2)
  • tests/regressiontests/views/models.py

     
    2424    def __unicode__(self):
    2525        return self.title
    2626
     27
     28class Issue(models.Model):
     29    pub_date = models.DateTimeField()
     30   
     31    def __unicode__(self):
     32        return self.pub_date.strftime('%Y-%m-%d')
  • tests/regressiontests/views/urls.py

     
    1414    'packages': ('regressiontests.views',),
    1515}
    1616
    17 date_based_info_dict = {
     17article_date_based_info_dict = {
    1818    'queryset': Article.objects.all(),
    1919    'date_field': 'date_created',
    2020    'month_format': '%m',
    2121}
    2222
     23issue_date_based_info_dict = {
     24    'queryset': Issue.objects.all(),
     25    'date_field': 'pub_date',
     26    'month_format': '%m',
     27}
     28
    2329urlpatterns = patterns('',
    2430    (r'^$', views.index_page),
    2531   
     
    3844        # Date-based generic views
    3945    (r'^date_based/object_detail/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/$',
    4046        'django.views.generic.date_based.object_detail',
    41         dict(slug_field='slug', **date_based_info_dict)),
     47        dict(slug_field='slug', **article_date_based_info_dict)),
    4248    (r'^date_based/object_detail/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/allow_future/$',
    4349        'django.views.generic.date_based.object_detail',
    44         dict(allow_future=True, slug_field='slug', **date_based_info_dict)),
     50        dict(allow_future=True, slug_field='slug', **article_date_based_info_dict)),
    4551    (r'^date_based/archive_month/(?P<year>\d{4})/(?P<month>\d{1,2})/$',
    4652        'django.views.generic.date_based.archive_month',
    47         date_based_info_dict),     
     53        article_date_based_info_dict),     
     54
     55    (r'^date_based/issues/archive_month/(?P<year>\d{4})/(?P<month>\d{1,2})/$',
     56        'django.views.generic.date_based.archive_month',
     57        issue_date_based_info_dict),
    4858)
Back to Top