Ticket #5506: 5506.diff

File 5506.diff, 6.9 KB (added by robh, 15 years ago)

Tests for #391 (object_detail can't find 'today') and #3031 (archive_month includes too much)

  • tests/regressiontests/generic_views/tests.py

    === tests/regressiontests/generic_views/__init__.py
    ==================================================================
    === tests/regressiontests/generic_views/tests.py
    ==================================================================
     
     1# coding: utf-8
     2from django.test import TestCase
     3from datetime import datetime
     4from datetime import timedelta
     5from models import Article
     6
     7_today = datetime.now()
     8_long_ago = datetime(2001, 1, 1)
     9_far_future = datetime(3000, 01, 01)
     10
     11class DateBasedGenericViewTest(TestCase):
     12    def setUp(self):
     13        current_article = Article(slug="current_article", date_created=_today)
     14        current_article.save()
     15        past_article = Article(slug="old_article", date_created=_long_ago)
     16        past_article.save()
     17        future_article = Article(slug="future_article", date_created=_far_future)
     18        future_article.save()
     19       
     20    def test_object_detail_finds_past(self):
     21        response = self.client.get('/generic_views/date_based/object_detail/2001/01/01/old_article/')
     22        self.assertEqual(response.context['object'].slug, "old_article")
     23
     24    def test_object_detail_finds_today(self):
     25        today_url = _today.strftime('%Y/%m/%d')
     26        response = self.client.get('/generic_views/date_based/object_detail/%s/current_article/' % today_url)
     27        self.assertEqual(response.context['object'].slug, "current_article")
     28
     29    def test_object_detail_ignores_future_unless_allowed(self):
     30        # careful, this is a year 3000 bug waiting to happen ;-)
     31        disallowed_response = self.client.get('/generic_views/date_based/object_detail/3000/01/01/future_article/')
     32        self.assertEqual(disallowed_response.status_code, 404)
     33        # the same view, with allow_future=True
     34        allowed_response = self.client.get('/generic_views/date_based/object_detail/3000/01/01/future_article/allow_future/')
     35        self.assertEqual(allowed_response.context['object'].slug, "future_article")
     36
     37class MonthArchive(TestCase):
     38    def test_archive_month_includes_only_month(self):
     39        # 2004 was a leap year, so it should be weird enough to not cheat
     40        first_second_of_feb = datetime(2004, 2, 1, 0, 0, 0)
     41        first_second_of_mar = datetime(2004, 3, 1, 0, 0, 0)
     42        one_microsecond = timedelta(0, 0, 1)
     43        article = Article(slug="example")
     44
     45        article.date_created = first_second_of_feb
     46        article.save()
     47        response = self.client.get('/generic_views/date_based/archive_month/2004/02/')
     48        self.assertEqual(response.status_code, 200)
     49         
     50        article.date_created = first_second_of_feb-one_microsecond
     51        article.save()
     52        response = self.client.get('/generic_views/date_based/archive_month/2004/02/')
     53        self.assertEqual(response.status_code, 404)
     54
     55        article.date_created = first_second_of_mar-one_microsecond
     56        article.save()
     57        response = self.client.get('/generic_views/date_based/archive_month/2004/02/')
     58        self.assertEqual(response.status_code, 200)
     59
     60        article.date_created = first_second_of_mar
     61        article.save()
     62        response = self.client.get('/generic_views/date_based/archive_month/2004/02/')
     63        self.assertEqual(response.status_code, 404)
     64       
     65
     66# vim: expandtab tabstop=8 shiftwidth=4 autoindent
  • tests/regressiontests/generic_views/models.py

    === tests/regressiontests/generic_views/models.py
    ==================================================================
     
     1# coding: utf-8
     2from django.db import models
     3
     4class Article(models.Model):
     5    slug = models.CharField(max_length=20)
     6    date_created = models.DateTimeField()
     7    def __repr__(self):
     8        return "<Article: %s>" % self.slug
     9
     10# vim: expandtab tabstop=8 shiftwidth=4 autoindent
  • tests/regressiontests/generic_views/urls.py

    === tests/regressiontests/generic_views/urls.py
    ==================================================================
     
     1from django.conf.urls.defaults import *
     2from django.views.generic import date_based
     3from models import Article
     4
     5info_dict = {
     6    'queryset': Article.objects.all(),
     7    'date_field': 'date_created',
     8    'month_format': '%m',
     9}
     10
     11urlpatterns = patterns('django.views.generic.date_based',
     12    url(r'^date_based/object_detail/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/$', 'object_detail', dict(slug_field='slug', **info_dict), name="object_detail"),
     13    url(r'^date_based/object_detail/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/allow_future/$', 'object_detail', dict(allow_future=True, slug_field='slug', **info_dict), name="object_detail_allow_future"),
     14    url(r'^date_based/archive_month/(?P<year>\d{4})/(?P<month>\d{1,2})/$', 'archive_month', info_dict, name="archive_month"),
     15)
     16
     17
     18# vim: autoindent tabstop=4 shiftwidth=4 expandtab
     19
  • tests/urls.py

    Property changes on: tests/regressiontests/generic_views
    ___________________________________________________________________
    Name: svn:ignore
     +*.pyc
    
    === tests/urls.py
    ==================================================================
     
    44    # test_client modeltest urls
    55    (r'^test_client/', include('modeltests.test_client.urls')),
    66    (r'^test_client_regress/', include('regressiontests.test_client_regress.urls')),
     7    (r'^generic_views/', include('regressiontests.generic_views.urls')),
    78
    89    # Always provide the auth system login and logout views
    910    (r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
  • tests/templates/generic_views/article_detail.html

    === tests/templates/generic_views/article_detail.html
    ==================================================================
     
     1This template intentionally left blank.
     2
  • tests/templates/generic_views/article_archive_month.html

    === tests/templates/generic_views/article_archive_month.html
    ==================================================================
     
     1This template intentionally left blank.
     2
Back to Top