Ticket #14752: tests.diff

File tests.diff, 4.7 KB (added by Mark Sundstrom, 14 years ago)
  • tests/regressiontests/generic_views/dates.py

     
    11import datetime
    22import random
     3import itertools
    34
    45from django.core.exceptions import ImproperlyConfigured
    56from django.test import TestCase
     
    350351    def test_invalid_url(self):
    351352        self.assertRaises(AttributeError, self.client.get, "/dates/books/2008/oct/01/nopk/")
    352353
     354from views import BookWeekArchive
     355
     356class WeekArchiveTest(TestCase):
     357    """
     358    Test that WeekArchive gets the correct queryset for each week in a year. As of
     359    changeset 14656, it does work when the week_format is '%U', i.e., each week begins
     360    on a Sunday, which is the default case. However, it did not work correctly when
     361    using '%W', i.e., each week begins on a Monday.
     362   
     363    This test creates a Book object for each day of a year and then tests that the
     364    correct queryset is returned for all possible cases involving a week.
     365   
     366    BaseWeekArchiveView.get_dated_items calls a helper function, _date_from_string,
     367    to get the date for a given week number. It does this correctly for weeks
     368    beginning on Sunday, for example, for the 10th week in 2009, with:
     369        time.strptime('2009__0__10', '%Y__%w__%U')
     370    However, if we are requesting a week beginning on Monday, it should use:
     371        time.strptime('2009__1__10', '%Y__%w__%W')
     372    but instead it used:
     373        time.strptime('2009__0__10', '%Y__%w__%W')
     374    """
     375    test_year = 2009 # if you use current year, you'll have to deal with allow_future issues
     376   
     377    def every_day_in_year(self, year=test_year):
     378        "Generate a date for every day in a year"
     379        date = datetime.date(self.test_year, 1, 1)
     380        last_date = date.replace(year=date.year+1)
     381        while date < last_date:
     382            yield date
     383            date += datetime.timedelta(days=1)
     384
     385    def dates_grouped_by_week(self, week_format):
     386        "The dates for an entire year, grouped by week, specified by week_format (%U or %W)"
     387        group_key = lambda date:date.strftime(week_format)
     388        return itertools.groupby(self.every_day_in_year(), group_key)
     389   
     390    def setUp(self):
     391        "Create a Book for every day of the year"
     392        for pubdate in self.every_day_in_year():
     393            day_of_year = pubdate.strftime('%j')
     394            Book.objects.create(name=day_of_year, slug=day_of_year, pages=1, pubdate=pubdate)
     395   
     396    def test_simple(self):
     397        "See that each Book object exists and is what we expect"
     398        for date in self.every_day_in_year():
     399            obj = Book.objects.get(pubdate=date)
     400            self.assertEqual(obj.name, obj.slug)
     401            self.assertEqual(obj.slug, date.strftime('%j'))
     402   
     403    def test_week_start_Sunday(self):
     404        week_format = '%U'
     405        self.common_week_start(week_format)
     406   
     407    def test_week_start_Monday(self):
     408        week_format = '%W'
     409        self.common_week_start(week_format)
     410
     411    def common_week_start(self, week_format):
     412        """
     413        We get a list of dates belonging to each week of the year,
     414        as determined by the week_format (either dates starting on Sunday,
     415        or dates starting on Monday). Because we have a Book for each day
     416        of the year, the list of dates should match the dates from the Books
     417        retrieved for that week.
     418        """
     419        for key,group in self.dates_grouped_by_week(week_format):
     420            dates_in_week = list(group)
     421            view_obj = BookWeekArchive(week_format = week_format, year = str(self.test_year), week = key)
     422            date_list, qs, extra_context = view_obj.get_dated_items()
     423            self.assertEqual(date_list, None)
     424            qs_dates = sorted([obj.pubdate for obj in qs])
     425            self.assertEqual( dates_in_week, qs_dates )
  • tests/regressiontests/generic_views/tests.py

     
    22from regressiontests.generic_views.dates import ArchiveIndexViewTests, YearArchiveViewTests, MonthArchiveViewTests, WeekArchiveViewTests, DayArchiveViewTests, DateDetailViewTests
    33from regressiontests.generic_views.detail import DetailViewTest
    44from regressiontests.generic_views.edit import CreateViewTests, UpdateViewTests, DeleteViewTests
    5 from regressiontests.generic_views.list import ListViewTests
    6  No newline at end of file
     5from regressiontests.generic_views.list import ListViewTests
     6from regressiontests.generic_views.dates import WeekArchiveTest
     7 No newline at end of file
Back to Top