Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #5506 -- Added some basic tests for date-based generic views. T…

…hanks to robh for the original patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6374 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 466871ec1b40f4e68bbe693cfdeacef4eec4370c 1 parent 1571e9c
Russell Keith-Magee authored September 19, 2007
25  tests/regressiontests/views/fixtures/testdata.json
@@ -4,10 +4,33 @@
4 4
         "model": "views.article", 
5 5
         "fields": {
6 6
             "author": 1, 
7  
-            "title": "An Article"
  7
+            "title": "Old Article",
  8
+            "slug": "old_article",
  9
+            "date_created": "2001-01-01 21:22:23"
8 10
         }
9 11
     }, 
10 12
     {
  13
+        "pk": 2, 
  14
+        "model": "views.article", 
  15
+        "fields": {
  16
+            "author": 1, 
  17
+            "title": "Current Article",
  18
+            "slug": "current_article",
  19
+            "date_created": "2007-09-17 21:22:23"
  20
+        }
  21
+    }, 
  22
+    {
  23
+        "pk": 3, 
  24
+        "model": "views.article", 
  25
+        "fields": {
  26
+            "author": 1, 
  27
+            "title": "Future Article",
  28
+            "slug": "future_article",
  29
+            "date_created": "3000-01-01 21:22:23"
  30
+        }
  31
+    }, 
  32
+
  33
+    {
11 34
         "pk": 1, 
12 35
         "model": "views.author", 
13 36
         "fields": {
4  tests/regressiontests/views/models.py
@@ -17,8 +17,10 @@ def get_absolute_url(self):
17 17
 
18 18
 class Article(models.Model):
19 19
     title = models.CharField(max_length=100)
  20
+    slug = models.SlugField()
20 21
     author = models.ForeignKey(Author)
21  
-
  22
+    date_created = models.DateTimeField()
  23
+    
22 24
     def __unicode__(self):
23 25
         return self.title
24 26
 
3  tests/regressiontests/views/tests/__init__.py
... ...
@@ -1,3 +1,4 @@
1 1
 from defaults import *
2 2
 from i18n import *
3  
-from static import *
  3
+from static import *
  4
+from generic.date_based import *
0  tests/regressiontests/views/tests/generic/__init__.py
No changes.
71  tests/regressiontests/views/tests/generic/date_based.py
... ...
@@ -0,0 +1,71 @@
  1
+# coding: utf-8 
  2
+from django.test import TestCase 
  3
+from datetime import datetime 
  4
+from datetime import timedelta 
  5
+from regressiontests.views.models import Article, Author
  6
+
  7
+class ObjectDetailTest(TestCase):
  8
+    fixtures = ['testdata.json'] 
  9
+    def setUp(self): 
  10
+        # Correct the date for the current article
  11
+        current_article = Article.objects.get(title="Current Article")
  12
+        current_article.date_created = datetime.now() 
  13
+        current_article.save() 
  14
+         
  15
+    def test_finds_past(self): 
  16
+        "date_based.object_detail can view a page in the past"
  17
+        response = self.client.get('/views/date_based/object_detail/2001/01/01/old_article/') 
  18
+        self.assertEqual(response.status_code, 200) 
  19
+        self.assertEqual(response.context['object'].title, "Old Article") 
  20
+ 
  21
+    def test_object_detail_finds_today(self): 
  22
+        "date_based.object_detail can view a page from today"
  23
+        today_url = datetime.now().strftime('%Y/%m/%d') 
  24
+        response = self.client.get('/views/date_based/object_detail/%s/current_article/' % today_url) 
  25
+        self.assertEqual(response.status_code, 200) 
  26
+        self.assertEqual(response.context['object'].title, "Current Article") 
  27
+ 
  28
+    def test_object_detail_ignores_future(self): 
  29
+        "date_based.object_detail can view a page from the future, but only if allowed."
  30
+        response = self.client.get('/views/date_based/object_detail/3000/01/01/future_article/') 
  31
+        self.assertEqual(response.status_code, 404) 
  32
+
  33
+    def test_object_detail_allowed_future_if_enabled(self): 
  34
+        "date_based.object_detail can view a page from the future if explicitly allowed."
  35
+        response = self.client.get('/views/date_based/object_detail/3000/01/01/future_article/allow_future/')
  36
+        self.assertEqual(response.status_code, 200) 
  37
+        self.assertEqual(response.context['object'].title, "Future Article") 
  38
+	 
  39
+class MonthArchiveTest(TestCase): 
  40
+    def test_archive_month_includes_only_month(self): 
  41
+        "Regression for #3031: Archives around Feburary include only one month"
  42
+        author = Author(name="John Smith")
  43
+        author.save()
  44
+
  45
+        # 2004 was a leap year, so it should be weird enough to not cheat 
  46
+        first_second_of_feb = datetime(2004, 2, 1, 0, 0, 0) 
  47
+        first_second_of_mar = datetime(2004, 3, 1, 0, 0, 0) 
  48
+        one_microsecond = timedelta(0, 0, 1) 
  49
+        article = Article(title="example", author=author) 
  50
+
  51
+        article.date_created = first_second_of_feb 
  52
+        article.save() 
  53
+        response = self.client.get('/views/date_based/archive_month/2004/02/') 
  54
+        self.assertEqual(response.status_code, 200) 
  55
+      
  56
+        article.date_created = first_second_of_feb-one_microsecond 
  57
+        article.save() 
  58
+        response = self.client.get('/views/date_based/archive_month/2004/02/') 
  59
+        self.assertEqual(response.status_code, 404) 
  60
+
  61
+        article.date_created = first_second_of_mar-one_microsecond 
  62
+        article.save() 
  63
+        response = self.client.get('/views/date_based/archive_month/2004/02/') 
  64
+        self.assertEqual(response.status_code, 200) 
  65
+
  66
+        article.date_created = first_second_of_mar 
  67
+        article.save() 
  68
+        response = self.client.get('/views/date_based/archive_month/2004/02/') 
  69
+        self.assertEqual(response.status_code, 404) 
  70
+	         
  71
+	 
24  tests/regressiontests/views/urls.py
... ...
@@ -1,6 +1,8 @@
1 1
 from os import path
2 2
 
3 3
 from django.conf.urls.defaults import *
  4
+
  5
+from models import *
4 6
 import views
5 7
 
6 8
 base_dir = path.dirname(path.abspath(__file__))
@@ -12,15 +14,35 @@
12 14
     'packages': ('regressiontests.views',),
13 15
 }
14 16
 
  17
+date_based_info_dict = { 
  18
+    'queryset': Article.objects.all(), 
  19
+    'date_field': 'date_created', 
  20
+    'month_format': '%m', 
  21
+} 
  22
+
15 23
 urlpatterns = patterns('',
16 24
     (r'^$', views.index_page),
  25
+    
  26
+    # Default views
17 27
     (r'^shortcut/(\d+)/(.*)/$', 'django.views.defaults.shortcut'),
18 28
     (r'^non_existing_url/', 'django.views.defaults.page_not_found'),
19 29
     (r'^server_error/', 'django.views.defaults.server_error'),
20 30
     
  31
+    # i18n views
21 32
     (r'^i18n/', include('django.conf.urls.i18n')),    
22 33
     (r'^jsi18n/$', 'django.views.i18n.javascript_catalog', js_info_dict),
23  
-    (r'^jsi18n_test/$', views.jsi18n_test),
24 34
     
  35
+    # Static views
25 36
     (r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': media_dir}),
  37
+    
  38
+	# Date-based generic views
  39
+    (r'^date_based/object_detail/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/$', 
  40
+        'django.views.generic.date_based.object_detail', 
  41
+        dict(slug_field='slug', **date_based_info_dict)), 
  42
+    (r'^date_based/object_detail/(?P<year>\d{4})/(?P<month>\d{1,2})/(?P<day>\d{1,2})/(?P<slug>[-\w]+)/allow_future/$', 
  43
+        'django.views.generic.date_based.object_detail', 
  44
+        dict(allow_future=True, slug_field='slug', **date_based_info_dict)), 
  45
+    (r'^date_based/archive_month/(?P<year>\d{4})/(?P<month>\d{1,2})/$', 
  46
+        'django.views.generic.date_based.archive_month', 
  47
+        date_based_info_dict),     
26 48
 )
7  tests/regressiontests/views/views.py
... ...
@@ -1,12 +1,7 @@
1 1
 from django.http import HttpResponse
2 2
 from django.template import RequestContext
3  
-from django.shortcuts import render_to_response
4 3
 
5 4
 def index_page(request):
6  
-    """ Dummy index page """
  5
+    """Dummy index page"""
7 6
     return HttpResponse('<html><body>Dummy page</body></html>')
8 7
 
9  
-
10  
-def jsi18n_test(request):
11  
-    """ View for testing javascript message files """
12  
-    return render_to_response('js_i18n.html', {})
1  tests/templates/views/article_archive_month.html
... ...
@@ -0,0 +1 @@
  1
+This template intentionally left blank
1  tests/templates/views/article_detail.html
... ...
@@ -0,0 +1 @@
  1
+This template intentionally left blank

0 notes on commit 466871e

Please sign in to comment.
Something went wrong with that request. Please try again.