Ticket #391: object_detail_tests.diff

File object_detail_tests.diff, 3.4 KB (added by Rob Hunter <robertjhunter@…>, 17 years ago)

Basic tests for the date_based generic view, object_detail

  • tests/regressiontests/generic_views/tests.py

     
     1# coding: utf-8
     2from django.test import TestCase
     3from datetime import datetime
     4from models import Article
     5
     6_today = datetime.now()
     7_long_ago = datetime(2001, 1, 1)
     8_far_future = datetime(3000, 01, 01)
     9
     10class DateBasedGenericViewTest(TestCase):
     11    def setUp(self):
     12        current_article = Article(slug="current_article", date_created=_today)
     13        current_article.save()
     14        past_article = Article(slug="old_article", date_created=_long_ago)
     15        past_article.save()
     16        future_article = Article(slug="future_article", date_created=_far_future)
     17        future_article.save()
     18       
     19    def test_object_detail_finds_past(self):
     20        response = self.client.get('/generic_views/date_based/object_detail/2001/01/01/old_article/')
     21        self.assertEqual(response.context['object'].slug, "old_article")
     22
     23    def test_object_detail_finds_today(self):
     24        today_url = _today.strftime('%Y/%m/%d')
     25        response = self.client.get('/generic_views/date_based/object_detail/%s/current_article/' % today_url)
     26        self.assertEqual(response.context['object'].slug, "current_article")
     27
     28    def test_object_detail_ignores_future(self):
     29        # careful, this is a year 3000 bug waiting to happen ;-)
     30        response = self.client.get('/generic_views/date_based/object_detail/3000/jan/01/future_article/')
     31        self.assertFalse(response.context.has_key('object'))
     32
     33# vim: expandtab tabstop=8 shiftwidth=4 autoindent
  • 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
     8# vim: expandtab tabstop=8 shiftwidth=4 autoindent
  • 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    'slug_field': 'slug',
     9    'month_format': '%m',
     10}
     11
     12urlpatterns = patterns('',
     13    url(r'^date_based/object_detail/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/$', 'django.views.generic.date_based.object_detail', info_dict, name="object_detail"),
     14)
     15
     16# vim: autoindent tabstop=4 shiftwidth=4 expandtab
     17
  • tests/templates/generic_views/article_detail.html

     
     1This template intentionally left blank.
     2
Back to Top