Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 5a16b64ed29301fc7eb04405dac6ca4f58f550d2 1 parent 9796f69
Carl Meyer authored October 26, 2011

Showing 1 changed file with 12 additions and 8 deletions. Show diff stats Hide diff stats

  1. 20  django/contrib/humanize/tests.py
20  django/contrib/humanize/tests.py
@@ -118,7 +118,8 @@ def test_naturalday_tz(self):
118 118
         self.assertNotEqual(naturalday_one, naturalday_two)
119 119
 
120 120
     def test_naturaltime(self):
121  
-        now = datetime.now()
  121
+        # we're going to mock datetime.datetime, so use a fixed datetime
  122
+        now = datetime(2011, 8, 15)
122 123
         test_list = [
123 124
             now,
124 125
             now - timedelta(seconds=1),
@@ -160,18 +161,21 @@ def test_naturaltime(self):
160 161
 
161 162
         # mock out datetime so these tests don't fail occasionally when the
162 163
         # test runs too slow
163  
-        class MockDateTime(object):
  164
+        class MockDateTime(datetime):
  165
+            @classmethod
164 166
             def now(self):
165 167
                 return now
166 168
 
167  
-            def __call__(self, *args, **kwargs):
168  
-                return datetime(*args, **kwargs)
169  
-
  169
+        # naturaltime also calls timesince/timeuntil
170 170
         from django.contrib.humanize.templatetags import humanize
171  
-        orig_datetime = humanize.datetime
172  
-        humanize.datetime = MockDateTime()
  171
+        from django.utils import timesince
  172
+        orig_humanize_datetime = humanize.datetime
  173
+        orig_timesince_datetime = timesince.datetime.datetime
  174
+        humanize.datetime = MockDateTime
  175
+        timesince.datetime.datetime = MockDateTime
173 176
 
174 177
         try:
175 178
             self.humanize_tester(test_list, result_list, 'naturaltime')
176 179
         finally:
177  
-            humanize.datetime = orig_datetime
  180
+            humanize.datetime = orig_humanize_datetime
  181
+            timesince.datetime.datetime = orig_timesince_datetime

0 notes on commit 5a16b64

Please sign in to comment.
Something went wrong with that request. Please try again.