PageRenderTime 26ms CodeModel.GetById 11ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/utils/timesince.py

https://code.google.com/p/mango-py/
Python | 107 lines | 91 code | 8 blank | 8 comment | 0 complexity | 1ef701d49b8e82332225b7317af99e6f MD5 | raw file
  1import datetime
  2import unittest
  3
  4from django.utils.timesince import timesince, timeuntil
  5from django.utils.tzinfo import LocalTimezone, FixedOffset
  6
  7class TimesinceTests(unittest.TestCase):
  8
  9    def setUp(self):
 10        self.t = datetime.datetime(2007, 8, 14, 13, 46, 0)
 11        self.onemicrosecond = datetime.timedelta(microseconds=1)
 12        self.onesecond = datetime.timedelta(seconds=1)
 13        self.oneminute = datetime.timedelta(minutes=1)
 14        self.onehour = datetime.timedelta(hours=1)
 15        self.oneday = datetime.timedelta(days=1)
 16        self.oneweek = datetime.timedelta(days=7)
 17        self.onemonth = datetime.timedelta(days=30)
 18        self.oneyear = datetime.timedelta(days=365)
 19
 20    def test_equal_datetimes(self):
 21        """ equal datetimes. """
 22        self.assertEqual(timesince(self.t, self.t), u'0 minutes')
 23
 24    def test_ignore_microseconds_and_seconds(self):
 25        """ Microseconds and seconds are ignored. """
 26        self.assertEqual(timesince(self.t, self.t+self.onemicrosecond),
 27            u'0 minutes')
 28        self.assertEqual(timesince(self.t, self.t+self.onesecond),
 29            u'0 minutes')
 30
 31    def test_other_units(self):
 32        """ Test other units. """
 33        self.assertEqual(timesince(self.t, self.t+self.oneminute),
 34            u'1 minute')
 35        self.assertEqual(timesince(self.t, self.t+self.onehour), u'1 hour')
 36        self.assertEqual(timesince(self.t, self.t+self.oneday), u'1 day')
 37        self.assertEqual(timesince(self.t, self.t+self.oneweek), u'1 week')
 38        self.assertEqual(timesince(self.t, self.t+self.onemonth),
 39            u'1 month')
 40        self.assertEqual(timesince(self.t, self.t+self.oneyear), u'1 year')
 41
 42    def test_multiple_units(self):
 43        """ Test multiple units. """
 44        self.assertEqual(timesince(self.t,
 45            self.t+2*self.oneday+6*self.onehour), u'2 days, 6 hours')
 46        self.assertEqual(timesince(self.t,
 47            self.t+2*self.oneweek+2*self.oneday), u'2 weeks, 2 days')
 48
 49    def test_display_first_unit(self):
 50        """
 51        If the two differing units aren't adjacent, only the first unit is
 52        displayed.
 53        """
 54        self.assertEqual(timesince(self.t,
 55            self.t+2*self.oneweek+3*self.onehour+4*self.oneminute),
 56            u'2 weeks')
 57
 58        self.assertEqual(timesince(self.t,
 59            self.t+4*self.oneday+5*self.oneminute), u'4 days')
 60
 61    def test_display_second_before_first(self):
 62        """
 63        When the second date occurs before the first, we should always
 64        get 0 minutes.
 65        """
 66        self.assertEqual(timesince(self.t, self.t-self.onemicrosecond),
 67            u'0 minutes')
 68        self.assertEqual(timesince(self.t, self.t-self.onesecond),
 69            u'0 minutes')
 70        self.assertEqual(timesince(self.t, self.t-self.oneminute),
 71            u'0 minutes')
 72        self.assertEqual(timesince(self.t, self.t-self.onehour),
 73            u'0 minutes')
 74        self.assertEqual(timesince(self.t, self.t-self.oneday),
 75            u'0 minutes')
 76        self.assertEqual(timesince(self.t, self.t-self.oneweek),
 77            u'0 minutes')
 78        self.assertEqual(timesince(self.t, self.t-self.onemonth),
 79            u'0 minutes')
 80        self.assertEqual(timesince(self.t, self.t-self.oneyear),
 81            u'0 minutes')
 82        self.assertEqual(timesince(self.t,
 83            self.t-2*self.oneday-6*self.onehour), u'0 minutes')
 84        self.assertEqual(timesince(self.t,
 85            self.t-2*self.oneweek-2*self.oneday), u'0 minutes')
 86        self.assertEqual(timesince(self.t,
 87            self.t-2*self.oneweek-3*self.onehour-4*self.oneminute),
 88            u'0 minutes')
 89        self.assertEqual(timesince(self.t,
 90            self.t-4*self.oneday-5*self.oneminute), u'0 minutes')
 91
 92    def test_different_timezones(self):
 93        """ When using two different timezones. """
 94        now = datetime.datetime.now()
 95        now_tz = datetime.datetime.now(LocalTimezone(now))
 96        now_tz_i = datetime.datetime.now(FixedOffset((3 * 60) + 15))
 97
 98        self.assertEqual(timesince(now), u'0 minutes')
 99        self.assertEqual(timesince(now_tz), u'0 minutes')
100        self.assertEqual(timeuntil(now_tz, now_tz_i), u'0 minutes')
101
102    def test_both_date_objects(self):
103        """ Timesince should work with both date objects (#9672) """
104        today = datetime.date.today()
105        self.assertEqual(timeuntil(today+self.oneday, today), u'1 day')
106        self.assertEqual(timeuntil(today-self.oneday, today), u'0 minutes')
107        self.assertEqual(timeuntil(today+self.oneweek, today), u'1 week')