Ticket #7602: 7602.patch

File 7602.patch, 10.4 KB (added by cgrady, 7 years ago)

added tests, verified failure->success change

  • django/views/generic/date_based.py

     
    129129        last_day = first_day.replace(year=first_day.year + 1, month=1)
    130130    else:
    131131        last_day = first_day.replace(month=first_day.month + 1)
    132     lookup_kwargs = {'%s__range' % date_field: (first_day, last_day)}
     132    lookup_kwargs = {
     133        '%s__gte' % date_field: first_day,
     134        '%s__lt' % date_field: last_day,
     135    }
    133136
    134137    # Only bother to check current date if the month isn't in the past and future objects are requested.
    135138    if last_day >= now.date() and not allow_future:
     
    188191    # Calculate first and last day of week, for use in a date-range lookup.
    189192    first_day = date
    190193    last_day = date + datetime.timedelta(days=7)
    191     lookup_kwargs = {'%s__range' % date_field: (first_day, last_day)}
     194    lookup_kwargs = {
     195        '%s__gte' % date_field: first_day,
     196        '%s__lt' % date_field: last_day,
     197    }
    192198
    193199    # Only bother to check current date if the week isn't in the past and future objects aren't requested.
    194200    if last_day >= now.date() and not allow_future:
  • tests/regressiontests/views/tests/generic/date_based.py

     
    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
     1# coding: utf-8
     2from django.test import TestCase
     3from datetime import datetime
     4from datetime import timedelta
     5from regressiontests.views.models import Article, Author, DateArticle
    66
    77class ObjectDetailTest(TestCase):
    8     fixtures = ['testdata.json'] 
    9     def setUp(self): 
     8    fixtures = ['testdata.json']
     9    def setUp(self):
    1010        # Correct the date for the current article
    1111        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): 
     12        current_article.date_created = datetime.now()
     13        current_article.save()
     14
     15    def test_finds_past(self):
    1616        "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): 
     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):
    2222        "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): 
     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):
    2929        "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) 
     30        response = self.client.get('/views/date_based/object_detail/3000/01/01/future_article/')
     31        self.assertEqual(response.status_code, 404)
    3232
    33     def test_object_detail_allowed_future_if_enabled(self): 
     33    def test_object_detail_allowed_future_if_enabled(self):
    3434        "date_based.object_detail can view a page from the future if explicitly allowed."
    3535        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): 
     36        self.assertEqual(response.status_code, 200)
     37        self.assertEqual(response.context['object'].title, "Future Article")
     38
     39class MonthArchiveTest(TestCase):
     40    def test_archive_month_includes_only_month(self):
    4141        "Regression for #3031: Archives around Feburary include only one month"
    4242        author = Author(name="John Smith")
    4343        author.save()
    4444
    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, 1) 
    47         first_second_of_mar = datetime(2004, 3, 1, 0, 0, 1) 
    48         two_seconds = timedelta(0, 2, 0) 
    49         article = Article(title="example", author=author) 
     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, 1)
     47        first_second_of_mar = datetime(2004, 3, 1, 0, 0, 1)
     48        two_seconds = timedelta(0, 2, 0)
     49        article = Article(title="example", author=author)
    5050
    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-two_seconds
    57         article.save()
    58         response = self.client.get('/views/date_based/archive_month/2004/02/')
    59         self.assertEqual(response.status_code, 404)
     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)
    6055
    61         article.date_created = first_second_of_mar-two_seconds
    62         article.save() 
    63         response = self.client.get('/views/date_based/archive_month/2004/02/') 
    64         self.assertEqual(response.status_code, 200)
     56        article.date_created = first_second_of_feb-two_seconds
     57        article.save()
     58        response = self.client.get('/views/date_based/archive_month/2004/02/')
     59        self.assertEqual(response.status_code, 404)
    6560
    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          
    72  No newline at end of file
     61        article.date_created = first_second_of_mar-two_seconds
     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        article2 = DateArticle(title="example", author=author)
     72
     73        article2.date_created = first_second_of_feb.date()
     74        article2.save()
     75        response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
     76        self.assertEqual(response.status_code, 200)
     77
     78        article2.date_created = (first_second_of_feb-two_seconds).date()
     79        article2.save()
     80        response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
     81        self.assertEqual(response.status_code, 404)
     82
     83        article2.date_created = (first_second_of_mar-two_seconds).date()
     84        article2.save()
     85        response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
     86        self.assertEqual(response.status_code, 200)
     87
     88        article2.date_created = first_second_of_mar.date()
     89        article2.save()
     90        response = self.client.get('/views/date_based/datefield/archive_month/2004/02/')
     91        self.assertEqual(response.status_code, 404)
     92
  • tests/regressiontests/views/models.py

     
    2121    title = models.CharField(max_length=100)
    2222    slug = models.SlugField()
    2323    author = models.ForeignKey(Author)
    24     date_created = models.DateTimeField()
    2524
    2625    class Meta:
    2726        abstract = True
     
    3029        return self.title
    3130
    3231class Article(BaseArticle):
    33     pass
     32    date_created = models.DateTimeField()
    3433
    3534class UrlArticle(BaseArticle):
    3635    """
    3736    An Article class with a get_absolute_url defined.
    3837    """
     38    date_created = models.DateTimeField()
     39
    3940    def get_absolute_url(self):
    4041        return '/urlarticles/%s/' % self.slug
     42
     43class DateArticle(BaseArticle):
     44    """
     45    An article Model with a DateField instead of DateTimeField,
     46    for testing #7602
     47    """
     48    date_created = models.DateField()
  • tests/regressiontests/views/urls.py

     
    2020    'date_field': 'date_created',
    2121    'month_format': '%m',
    2222}
     23date_based_datefield_info_dict = dict(date_based_info_dict, queryset=DateArticle.objects.all())
    2324
    2425urlpatterns = patterns('',
    2526    (r'^$', views.index_page),
     
    4849    (r'^date_based/archive_month/(?P<year>\d{4})/(?P<month>\d{1,2})/$',
    4950        'archive_month',
    5051        date_based_info_dict),
     52    (r'^date_based/datefield/archive_month/(?P<year>\d{4})/(?P<month>\d{1,2})/$',
     53        'archive_month',
     54        date_based_datefield_info_dict),
    5155)
    5256
    5357# crud generic views.
  • tests/templates/views/datearticle_archive_month.html

     
     1This template intentionally left blank
     2 No newline at end of file
Back to Top