diff options
| author | Carl Meyer <carl@oddbird.net> | 2011-10-26 15:17:43 +0000 |
|---|---|---|
| committer | Carl Meyer <carl@oddbird.net> | 2011-10-26 15:17:43 +0000 |
| commit | 5a16b64ed29301fc7eb04405dac6ca4f58f550d2 (patch) | |
| tree | 12a361728c1f4ddb348e6b66c66f2014179960e6 | |
| parent | 9796f69533320171bf22d769d6e4ab3f5a45aac1 (diff) | |
Avoid spurious failures in naturaltime tests.
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
| -rw-r--r-- | django/contrib/humanize/tests.py | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/django/contrib/humanize/tests.py b/django/contrib/humanize/tests.py index d46cdb05e4..c59885a0dc 100644 --- a/django/contrib/humanize/tests.py +++ b/django/contrib/humanize/tests.py @@ -118,7 +118,8 @@ class HumanizeTests(TestCase): 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), @@ -160,18 +161,21 @@ class HumanizeTests(TestCase): # 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 |
