Skip to content

Commit

Permalink
Updated humanize tests to use the FixedOffset timezone from the utils…
Browse files Browse the repository at this point in the history
… and removed a bit code smell while I'm here.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16232 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jezdez committed May 17, 2011
1 parent 060923d commit 19979ec
Showing 1 changed file with 8 additions and 30 deletions.
38 changes: 8 additions & 30 deletions tests/regressiontests/humanize/tests.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,25 @@
from __future__ import with_statement
from datetime import timedelta, date, datetime, tzinfo, timedelta
from datetime import timedelta, date, datetime, tzinfo

from django.conf import settings
from django.template import Template, Context, add_to_builtins
from django.template import Template, Context, add_to_builtins, defaultfilters
from django.test import TestCase
from django.utils import translation
from django.utils.dateformat import DateFormat
from django.utils import translation, tzinfo
from django.utils.translation import ugettext as _
from django.utils.html import escape
from django.conf import settings

add_to_builtins('django.contrib.humanize.templatetags.humanize')


class FixedOffset(tzinfo):
"""Fixed offset in hours east from UTC."""

def __init__(self, offset, name):
self.__offset = timedelta(hours=offset)
self.__name = name

def utcoffset(self, dt):
return self.__offset

def tzname(self, dt):
return self.__name

def dst(self, dt):
return timedelta(0)


class HumanizeTests(TestCase):

def humanize_tester(self, test_list, result_list, method):
# Using max below ensures we go through both lists
# However, if the lists are not equal length, this raises an exception
for index in xrange(max(len(test_list), len(result_list))):
test_content = test_list[index]
for test_content, result in zip(test_list, result_list):
t = Template('{{ test_content|%s }}' % method)
rendered = t.render(Context(locals())).strip()
self.assertEqual(rendered, escape(result_list[index]),
msg="%s test failed, produced %s, should've produced %s" % (method, rendered, result_list[index]))
self.assertEqual(rendered, escape(result),
msg="%s test failed, produced '%s', should've produced '%s'" % (method, rendered, result))

def test_ordinal(self):
test_list = ('1','2','3','4','11','12',
Expand Down Expand Up @@ -98,7 +77,6 @@ def test_apnumber(self):
self.humanize_tester(test_list, result_list, 'apnumber')

def test_naturalday(self):
from django.template import defaultfilters
today = date.today()
yesterday = today - timedelta(days=1)
tomorrow = today + timedelta(days=1)
Expand Down Expand Up @@ -140,8 +118,8 @@ def test_naturalday_tz(self):
from django.contrib.humanize.templatetags.humanize import naturalday

today = date.today()
tz_one = FixedOffset(-12, 'TzOne')
tz_two = FixedOffset(12, 'TzTwo')
tz_one = tzinfo.FixedOffset(timedelta(hours=-12))
tz_two = tzinfo.FixedOffset(timedelta(hours=12))

# Can be today or yesterday
date_one = datetime(today.year, today.month, today.day, tzinfo=tz_one)
Expand Down

0 comments on commit 19979ec

Please sign in to comment.