mirror of https://github.com/django/django.git
Migrated timesince utils doctests. Thanks to Stephan Jaekel.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13890 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
ba53cd30d1
commit
9abfd55a29
|
@ -1,98 +1,108 @@
|
||||||
"""
|
from django.test import TestCase
|
||||||
>>> import datetime
|
|
||||||
>>> from django.utils.timesince import timesince, timeuntil
|
|
||||||
>>> from django.utils.tzinfo import LocalTimezone, FixedOffset
|
|
||||||
|
|
||||||
>>> t = datetime.datetime(2007, 8, 14, 13, 46, 0)
|
import datetime
|
||||||
|
|
||||||
>>> onemicrosecond = datetime.timedelta(microseconds=1)
|
from django.utils.timesince import timesince, timeuntil
|
||||||
>>> onesecond = datetime.timedelta(seconds=1)
|
from django.utils.tzinfo import LocalTimezone, FixedOffset
|
||||||
>>> oneminute = datetime.timedelta(minutes=1)
|
|
||||||
>>> onehour = datetime.timedelta(hours=1)
|
|
||||||
>>> oneday = datetime.timedelta(days=1)
|
|
||||||
>>> oneweek = datetime.timedelta(days=7)
|
|
||||||
>>> onemonth = datetime.timedelta(days=30)
|
|
||||||
>>> oneyear = datetime.timedelta(days=365)
|
|
||||||
|
|
||||||
# equal datetimes.
|
class TimesinceTests(TestCase):
|
||||||
>>> timesince(t, t)
|
|
||||||
u'0 minutes'
|
|
||||||
|
|
||||||
# Microseconds and seconds are ignored.
|
def setUp(self):
|
||||||
>>> timesince(t, t+onemicrosecond)
|
self.t = datetime.datetime(2007, 8, 14, 13, 46, 0)
|
||||||
u'0 minutes'
|
self.onemicrosecond = datetime.timedelta(microseconds=1)
|
||||||
>>> timesince(t, t+onesecond)
|
self.onesecond = datetime.timedelta(seconds=1)
|
||||||
u'0 minutes'
|
self.oneminute = datetime.timedelta(minutes=1)
|
||||||
|
self.onehour = datetime.timedelta(hours=1)
|
||||||
|
self.oneday = datetime.timedelta(days=1)
|
||||||
|
self.oneweek = datetime.timedelta(days=7)
|
||||||
|
self.onemonth = datetime.timedelta(days=30)
|
||||||
|
self.oneyear = datetime.timedelta(days=365)
|
||||||
|
|
||||||
# Test other units.
|
def test_equal_datetimes(self):
|
||||||
>>> timesince(t, t+oneminute)
|
""" equal datetimes. """
|
||||||
u'1 minute'
|
self.assertEquals(timesince(self.t, self.t), u'0 minutes')
|
||||||
>>> timesince(t, t+onehour)
|
|
||||||
u'1 hour'
|
|
||||||
>>> timesince(t, t+oneday)
|
|
||||||
u'1 day'
|
|
||||||
>>> timesince(t, t+oneweek)
|
|
||||||
u'1 week'
|
|
||||||
>>> timesince(t, t+onemonth)
|
|
||||||
u'1 month'
|
|
||||||
>>> timesince(t, t+oneyear)
|
|
||||||
u'1 year'
|
|
||||||
|
|
||||||
# Test multiple units.
|
def test_ignore_microseconds_and_seconds(self):
|
||||||
>>> timesince(t, t+2*oneday+6*onehour)
|
""" Microseconds and seconds are ignored. """
|
||||||
u'2 days, 6 hours'
|
self.assertEquals(timesince(self.t, self.t+self.onemicrosecond),
|
||||||
>>> timesince(t, t+2*oneweek+2*oneday)
|
u'0 minutes')
|
||||||
u'2 weeks, 2 days'
|
self.assertEquals(timesince(self.t, self.t+self.onesecond),
|
||||||
|
u'0 minutes')
|
||||||
|
|
||||||
# If the two differing units aren't adjacent, only the first unit is displayed.
|
def test_other_units(self):
|
||||||
>>> timesince(t, t+2*oneweek+3*onehour+4*oneminute)
|
""" Test other units. """
|
||||||
u'2 weeks'
|
self.assertEquals(timesince(self.t, self.t+self.oneminute),
|
||||||
>>> timesince(t, t+4*oneday+5*oneminute)
|
u'1 minute')
|
||||||
u'4 days'
|
self.assertEquals(timesince(self.t, self.t+self.onehour), u'1 hour')
|
||||||
|
self.assertEquals(timesince(self.t, self.t+self.oneday), u'1 day')
|
||||||
|
self.assertEquals(timesince(self.t, self.t+self.oneweek), u'1 week')
|
||||||
|
self.assertEquals(timesince(self.t, self.t+self.onemonth),
|
||||||
|
u'1 month')
|
||||||
|
self.assertEquals(timesince(self.t, self.t+self.oneyear), u'1 year')
|
||||||
|
|
||||||
# When the second date occurs before the first, we should always get 0 minutes.
|
def test_multiple_units(self):
|
||||||
>>> timesince(t, t-onemicrosecond)
|
""" Test multiple units. """
|
||||||
u'0 minutes'
|
self.assertEquals(timesince(self.t,
|
||||||
>>> timesince(t, t-onesecond)
|
self.t+2*self.oneday+6*self.onehour), u'2 days, 6 hours')
|
||||||
u'0 minutes'
|
self.assertEquals(timesince(self.t,
|
||||||
>>> timesince(t, t-oneminute)
|
self.t+2*self.oneweek+2*self.oneday), u'2 weeks, 2 days')
|
||||||
u'0 minutes'
|
|
||||||
>>> timesince(t, t-onehour)
|
|
||||||
u'0 minutes'
|
|
||||||
>>> timesince(t, t-oneday)
|
|
||||||
u'0 minutes'
|
|
||||||
>>> timesince(t, t-oneweek)
|
|
||||||
u'0 minutes'
|
|
||||||
>>> timesince(t, t-onemonth)
|
|
||||||
u'0 minutes'
|
|
||||||
>>> timesince(t, t-oneyear)
|
|
||||||
u'0 minutes'
|
|
||||||
>>> timesince(t, t-2*oneday-6*onehour)
|
|
||||||
u'0 minutes'
|
|
||||||
>>> timesince(t, t-2*oneweek-2*oneday)
|
|
||||||
u'0 minutes'
|
|
||||||
>>> timesince(t, t-2*oneweek-3*onehour-4*oneminute)
|
|
||||||
u'0 minutes'
|
|
||||||
>>> timesince(t, t-4*oneday-5*oneminute)
|
|
||||||
u'0 minutes'
|
|
||||||
|
|
||||||
# When using two different timezones.
|
def test_display_first_unit(self):
|
||||||
>>> now = datetime.datetime.now()
|
"""
|
||||||
>>> now_tz = datetime.datetime.now(LocalTimezone(now))
|
If the two differing units aren't adjacent, only the first unit is
|
||||||
>>> now_tz_i = datetime.datetime.now(FixedOffset((3 * 60) + 15))
|
displayed.
|
||||||
>>> timesince(now)
|
"""
|
||||||
u'0 minutes'
|
self.assertEquals(timesince(self.t,
|
||||||
>>> timesince(now_tz)
|
self.t+2*self.oneweek+3*self.onehour+4*self.oneminute),
|
||||||
u'0 minutes'
|
u'2 weeks')
|
||||||
>>> timeuntil(now_tz, now_tz_i)
|
|
||||||
u'0 minutes'
|
|
||||||
|
|
||||||
# Timesince should work with both date objects (#9672)
|
self.assertEquals(timesince(self.t,
|
||||||
>>> today = datetime.date.today()
|
self.t+4*self.oneday+5*self.oneminute), u'4 days')
|
||||||
>>> timeuntil(today+oneday, today)
|
|
||||||
u'1 day'
|
def test_display_second_before_first(self):
|
||||||
>>> timeuntil(today-oneday, today)
|
"""
|
||||||
u'0 minutes'
|
When the second date occurs before the first, we should always
|
||||||
>>> timeuntil(today+oneweek, today)
|
get 0 minutes.
|
||||||
u'1 week'
|
"""
|
||||||
"""
|
self.assertEquals(timesince(self.t, self.t-self.onemicrosecond),
|
||||||
|
u'0 minutes')
|
||||||
|
self.assertEquals(timesince(self.t, self.t-self.onesecond),
|
||||||
|
u'0 minutes')
|
||||||
|
self.assertEquals(timesince(self.t, self.t-self.oneminute),
|
||||||
|
u'0 minutes')
|
||||||
|
self.assertEquals(timesince(self.t, self.t-self.onehour),
|
||||||
|
u'0 minutes')
|
||||||
|
self.assertEquals(timesince(self.t, self.t-self.oneday),
|
||||||
|
u'0 minutes')
|
||||||
|
self.assertEquals(timesince(self.t, self.t-self.oneweek),
|
||||||
|
u'0 minutes')
|
||||||
|
self.assertEquals(timesince(self.t, self.t-self.onemonth),
|
||||||
|
u'0 minutes')
|
||||||
|
self.assertEquals(timesince(self.t, self.t-self.oneyear),
|
||||||
|
u'0 minutes')
|
||||||
|
self.assertEquals(timesince(self.t,
|
||||||
|
self.t-2*self.oneday-6*self.onehour), u'0 minutes')
|
||||||
|
self.assertEquals(timesince(self.t,
|
||||||
|
self.t-2*self.oneweek-2*self.oneday), u'0 minutes')
|
||||||
|
self.assertEquals(timesince(self.t,
|
||||||
|
self.t-2*self.oneweek-3*self.onehour-4*self.oneminute),
|
||||||
|
u'0 minutes')
|
||||||
|
self.assertEquals(timesince(self.t,
|
||||||
|
self.t-4*self.oneday-5*self.oneminute), u'0 minutes')
|
||||||
|
|
||||||
|
def test_different_timezones(self):
|
||||||
|
""" When using two different timezones. """
|
||||||
|
now = datetime.datetime.now()
|
||||||
|
now_tz = datetime.datetime.now(LocalTimezone(now))
|
||||||
|
now_tz_i = datetime.datetime.now(FixedOffset((3 * 60) + 15))
|
||||||
|
|
||||||
|
self.assertEquals(timesince(now), u'0 minutes')
|
||||||
|
self.assertEquals(timesince(now_tz), u'0 minutes')
|
||||||
|
self.assertEquals(timeuntil(now_tz, now_tz_i), u'0 minutes')
|
||||||
|
|
||||||
|
def test_both_date_objects(self):
|
||||||
|
""" Timesince should work with both date objects (#9672) """
|
||||||
|
today = datetime.date.today()
|
||||||
|
self.assertEquals(timeuntil(today+self.oneday, today), u'1 day')
|
||||||
|
self.assertEquals(timeuntil(today-self.oneday, today), u'0 minutes')
|
||||||
|
self.assertEquals(timeuntil(today+self.oneweek, today), u'1 week')
|
||||||
|
|
Loading…
Reference in New Issue