From 9abfd55a29d00397e6c5d14cc86fccc342715a09 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Mon, 27 Sep 2010 15:15:33 +0000 Subject: [PATCH] Migrated timesince utils doctests. Thanks to Stephan Jaekel. git-svn-id: http://code.djangoproject.com/svn/django/trunk@13890 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/regressiontests/utils/timesince.py | 186 ++++++++++++----------- 1 file changed, 98 insertions(+), 88 deletions(-) diff --git a/tests/regressiontests/utils/timesince.py b/tests/regressiontests/utils/timesince.py index 5a54bf4c8c..d751121730 100644 --- a/tests/regressiontests/utils/timesince.py +++ b/tests/regressiontests/utils/timesince.py @@ -1,98 +1,108 @@ -""" ->>> import datetime ->>> from django.utils.timesince import timesince, timeuntil ->>> from django.utils.tzinfo import LocalTimezone, FixedOffset +from django.test import TestCase ->>> t = datetime.datetime(2007, 8, 14, 13, 46, 0) +import datetime ->>> onemicrosecond = datetime.timedelta(microseconds=1) ->>> onesecond = datetime.timedelta(seconds=1) ->>> 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) +from django.utils.timesince import timesince, timeuntil +from django.utils.tzinfo import LocalTimezone, FixedOffset -# equal datetimes. ->>> timesince(t, t) -u'0 minutes' +class TimesinceTests(TestCase): -# Microseconds and seconds are ignored. ->>> timesince(t, t+onemicrosecond) -u'0 minutes' ->>> timesince(t, t+onesecond) -u'0 minutes' + def setUp(self): + self.t = datetime.datetime(2007, 8, 14, 13, 46, 0) + self.onemicrosecond = datetime.timedelta(microseconds=1) + self.onesecond = datetime.timedelta(seconds=1) + 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. ->>> timesince(t, t+oneminute) -u'1 minute' ->>> 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' + def test_equal_datetimes(self): + """ equal datetimes. """ + self.assertEquals(timesince(self.t, self.t), u'0 minutes') -# Test multiple units. ->>> timesince(t, t+2*oneday+6*onehour) -u'2 days, 6 hours' ->>> timesince(t, t+2*oneweek+2*oneday) -u'2 weeks, 2 days' + def test_ignore_microseconds_and_seconds(self): + """ Microseconds and seconds are ignored. """ + self.assertEquals(timesince(self.t, self.t+self.onemicrosecond), + u'0 minutes') + 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. ->>> timesince(t, t+2*oneweek+3*onehour+4*oneminute) -u'2 weeks' ->>> timesince(t, t+4*oneday+5*oneminute) -u'4 days' + def test_other_units(self): + """ Test other units. """ + self.assertEquals(timesince(self.t, self.t+self.oneminute), + u'1 minute') + 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. ->>> timesince(t, t-onemicrosecond) -u'0 minutes' ->>> timesince(t, t-onesecond) -u'0 minutes' ->>> timesince(t, t-oneminute) -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' + def test_multiple_units(self): + """ Test multiple units. """ + self.assertEquals(timesince(self.t, + self.t+2*self.oneday+6*self.onehour), u'2 days, 6 hours') + self.assertEquals(timesince(self.t, + self.t+2*self.oneweek+2*self.oneday), u'2 weeks, 2 days') -# 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)) ->>> timesince(now) -u'0 minutes' ->>> timesince(now_tz) -u'0 minutes' ->>> timeuntil(now_tz, now_tz_i) -u'0 minutes' + def test_display_first_unit(self): + """ + If the two differing units aren't adjacent, only the first unit is + displayed. + """ + self.assertEquals(timesince(self.t, + self.t+2*self.oneweek+3*self.onehour+4*self.oneminute), + u'2 weeks') -# Timesince should work with both date objects (#9672) ->>> today = datetime.date.today() ->>> timeuntil(today+oneday, today) -u'1 day' ->>> timeuntil(today-oneday, today) -u'0 minutes' ->>> timeuntil(today+oneweek, today) -u'1 week' -""" + self.assertEquals(timesince(self.t, + self.t+4*self.oneday+5*self.oneminute), u'4 days') + + def test_display_second_before_first(self): + """ + When the second date occurs before the first, we should always + get 0 minutes. + """ + 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')