Skip to content

Commit

Permalink
Avoid spurious failures in naturaltime tests.
Browse files Browse the repository at this point in the history
Previous fix didn't cover timesince module, which naturaltime delegates to
under certain conditions. Extended mock of datetime to timesince module, and
switched to a fixed datetime rather than now() for testing, for more
reproducible tests (failures in mock will fail fast, rather than sporadically).

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17038 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
carljm committed Oct 26, 2011
1 parent 9796f69 commit 5a16b64
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions django/contrib/humanize/tests.py
Expand Up @@ -118,7 +118,8 @@ def test_naturalday_tz(self):
self.assertNotEqual(naturalday_one, naturalday_two)

def test_naturaltime(self):
now = datetime.now()
# we're going to mock datetime.datetime, so use a fixed datetime
now = datetime(2011, 8, 15)
test_list = [
now,
now - timedelta(seconds=1),
Expand Down Expand Up @@ -160,18 +161,21 @@ def test_naturaltime(self):

# mock out datetime so these tests don't fail occasionally when the
# test runs too slow
class MockDateTime(object):
class MockDateTime(datetime):
@classmethod
def now(self):
return now

def __call__(self, *args, **kwargs):
return datetime(*args, **kwargs)

# naturaltime also calls timesince/timeuntil
from django.contrib.humanize.templatetags import humanize
orig_datetime = humanize.datetime
humanize.datetime = MockDateTime()
from django.utils import timesince
orig_humanize_datetime = humanize.datetime
orig_timesince_datetime = timesince.datetime.datetime
humanize.datetime = MockDateTime
timesince.datetime.datetime = MockDateTime

try:
self.humanize_tester(test_list, result_list, 'naturaltime')
finally:
humanize.datetime = orig_datetime
humanize.datetime = orig_humanize_datetime
timesince.datetime.datetime = orig_timesince_datetime

0 comments on commit 5a16b64

Please sign in to comment.