summaryrefslogtreecommitdiff
path: root/tests/test_dates.py
diff options
context:
space:
mode:
authorAlex Morega <alex@grep.ro>2013-07-06 12:16:33 +0200
committerAlex Morega <alex@grep.ro>2013-07-06 12:16:33 +0200
commit6d275a1c8d40dd5de597f10f820c2032add0bdf4 (patch)
treee8847e4cde58772bbd122cbce348d85121f3db7f /tests/test_dates.py
parentfe06771d8a0b9119c88bda0c1fcc58d03acc70ef (diff)
downloadbabel-6d275a1c8d40dd5de597f10f820c2032add0bdf4.tar.gz
move unit tests outside babel package
Diffstat (limited to 'tests/test_dates.py')
-rw-r--r--tests/test_dates.py347
1 files changed, 347 insertions, 0 deletions
diff --git a/tests/test_dates.py b/tests/test_dates.py
new file mode 100644
index 0000000..75e7e11
--- /dev/null
+++ b/tests/test_dates.py
@@ -0,0 +1,347 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2007-2011 Edgewall Software
+# All rights reserved.
+#
+# This software is licensed as described in the file COPYING, which
+# you should have received as part of this distribution. The terms
+# are also available at http://babel.edgewall.org/wiki/License.
+#
+# This software consists of voluntary contributions made by many
+# individuals. For the exact contribution history, see the revision
+# history and logs, available at http://babel.edgewall.org/log/.
+
+import calendar
+from datetime import date, datetime, time, timedelta
+import doctest
+import new
+import unittest
+
+from pytz import timezone
+
+from babel import dates
+from babel.util import FixedOffsetTimezone
+
+
+class DateTimeFormatTestCase(unittest.TestCase):
+
+ def test_quarter_format(self):
+ d = date(2006, 6, 8)
+ fmt = dates.DateTimeFormat(d, locale='en_US')
+ self.assertEqual('2', fmt['Q'])
+ self.assertEqual('2nd quarter', fmt['QQQQ'])
+ d = date(2006, 12, 31)
+ fmt = dates.DateTimeFormat(d, locale='en_US')
+ self.assertEqual('Q4', fmt['QQQ'])
+
+ def test_month_context(self):
+ d = date(2006, 2, 8)
+ fmt = dates.DateTimeFormat(d, locale='cs_CZ')
+ self.assertEqual(u'2', fmt['MMMMM']) # narrow format
+ fmt = dates.DateTimeFormat(d, locale='cs_CZ')
+ self.assertEqual(u'ú', fmt['LLLLL']) # narrow standalone
+
+ def test_abbreviated_month_alias(self):
+ d = date(2006, 3, 8)
+ fmt = dates.DateTimeFormat(d, locale='de_DE')
+ self.assertEqual(u'Mär', fmt['LLL'])
+
+ def test_week_of_year_first(self):
+ d = date(2006, 1, 8)
+ fmt = dates.DateTimeFormat(d, locale='de_DE')
+ self.assertEqual('1', fmt['w'])
+ fmt = dates.DateTimeFormat(d, locale='en_US')
+ self.assertEqual('02', fmt['ww'])
+
+ def test_week_of_year_first_with_year(self):
+ d = date(2006, 1, 1)
+ fmt = dates.DateTimeFormat(d, locale='de_DE')
+ self.assertEqual('52', fmt['w'])
+ self.assertEqual('2005', fmt['YYYY'])
+
+ def test_week_of_year_last(self):
+ d = date(2006, 12, 26)
+ fmt = dates.DateTimeFormat(d, locale='de_DE')
+ self.assertEqual('52', fmt['w'])
+ fmt = dates.DateTimeFormat(d, locale='en_US')
+ self.assertEqual('52', fmt['w'])
+
+ def test_week_of_year_last_us_extra_week(self):
+ d = date(2005, 12, 26)
+ fmt = dates.DateTimeFormat(d, locale='de_DE')
+ self.assertEqual('52', fmt['w'])
+ fmt = dates.DateTimeFormat(d, locale='en_US')
+ self.assertEqual('53', fmt['w'])
+
+ def test_week_of_month_first(self):
+ d = date(2006, 1, 8)
+ fmt = dates.DateTimeFormat(d, locale='de_DE')
+ self.assertEqual('1', fmt['W'])
+ fmt = dates.DateTimeFormat(d, locale='en_US')
+ self.assertEqual('2', fmt['W'])
+
+ def test_week_of_month_last(self):
+ d = date(2006, 1, 29)
+ fmt = dates.DateTimeFormat(d, locale='de_DE')
+ self.assertEqual('4', fmt['W'])
+ fmt = dates.DateTimeFormat(d, locale='en_US')
+ self.assertEqual('5', fmt['W'])
+
+ def test_day_of_year(self):
+ d = date(2007, 4, 1)
+ fmt = dates.DateTimeFormat(d, locale='en_US')
+ self.assertEqual('91', fmt['D'])
+
+ def test_day_of_year_works_with_datetime(self):
+ d = datetime(2007, 4, 1)
+ fmt = dates.DateTimeFormat(d, locale='en_US')
+ self.assertEqual('91', fmt['D'])
+
+ def test_day_of_year_first(self):
+ d = date(2007, 1, 1)
+ fmt = dates.DateTimeFormat(d, locale='en_US')
+ self.assertEqual('001', fmt['DDD'])
+
+ def test_day_of_year_last(self):
+ d = date(2007, 12, 31)
+ fmt = dates.DateTimeFormat(d, locale='en_US')
+ self.assertEqual('365', fmt['DDD'])
+
+ def test_day_of_week_in_month(self):
+ d = date(2007, 4, 15)
+ fmt = dates.DateTimeFormat(d, locale='en_US')
+ self.assertEqual('3', fmt['F'])
+
+ def test_day_of_week_in_month_first(self):
+ d = date(2007, 4, 1)
+ fmt = dates.DateTimeFormat(d, locale='en_US')
+ self.assertEqual('1', fmt['F'])
+
+ def test_day_of_week_in_month_last(self):
+ d = date(2007, 4, 29)
+ fmt = dates.DateTimeFormat(d, locale='en_US')
+ self.assertEqual('5', fmt['F'])
+
+ def test_local_day_of_week(self):
+ d = date(2007, 4, 1) # a sunday
+ fmt = dates.DateTimeFormat(d, locale='de_DE')
+ self.assertEqual('7', fmt['e']) # monday is first day of week
+ fmt = dates.DateTimeFormat(d, locale='en_US')
+ self.assertEqual('01', fmt['ee']) # sunday is first day of week
+ fmt = dates.DateTimeFormat(d, locale='bn_BD')
+ self.assertEqual('03', fmt['ee']) # friday is first day of week
+
+ d = date(2007, 4, 2) # a monday
+ fmt = dates.DateTimeFormat(d, locale='de_DE')
+ self.assertEqual('1', fmt['e']) # monday is first day of week
+ fmt = dates.DateTimeFormat(d, locale='en_US')
+ self.assertEqual('02', fmt['ee']) # sunday is first day of week
+ fmt = dates.DateTimeFormat(d, locale='bn_BD')
+ self.assertEqual('04', fmt['ee']) # friday is first day of week
+
+ def test_local_day_of_week_standalone(self):
+ d = date(2007, 4, 1) # a sunday
+ fmt = dates.DateTimeFormat(d, locale='de_DE')
+ self.assertEqual('7', fmt['c']) # monday is first day of week
+ fmt = dates.DateTimeFormat(d, locale='en_US')
+ self.assertEqual('1', fmt['c']) # sunday is first day of week
+ fmt = dates.DateTimeFormat(d, locale='bn_BD')
+ self.assertEqual('3', fmt['c']) # friday is first day of week
+
+ d = date(2007, 4, 2) # a monday
+ fmt = dates.DateTimeFormat(d, locale='de_DE')
+ self.assertEqual('1', fmt['c']) # monday is first day of week
+ fmt = dates.DateTimeFormat(d, locale='en_US')
+ self.assertEqual('2', fmt['c']) # sunday is first day of week
+ fmt = dates.DateTimeFormat(d, locale='bn_BD')
+ self.assertEqual('4', fmt['c']) # friday is first day of week
+
+ def test_fractional_seconds(self):
+ t = time(15, 30, 12, 34567)
+ fmt = dates.DateTimeFormat(t, locale='en_US')
+ self.assertEqual('3457', fmt['SSSS'])
+
+ def test_fractional_seconds_zero(self):
+ t = time(15, 30, 0)
+ fmt = dates.DateTimeFormat(t, locale='en_US')
+ self.assertEqual('0000', fmt['SSSS'])
+
+ def test_milliseconds_in_day(self):
+ t = time(15, 30, 12, 345000)
+ fmt = dates.DateTimeFormat(t, locale='en_US')
+ self.assertEqual('55812345', fmt['AAAA'])
+
+ def test_milliseconds_in_day_zero(self):
+ d = time(0, 0, 0)
+ fmt = dates.DateTimeFormat(d, locale='en_US')
+ self.assertEqual('0000', fmt['AAAA'])
+
+ def test_timezone_rfc822(self):
+ tz = timezone('Europe/Berlin')
+ t = time(15, 30, tzinfo=tz)
+ fmt = dates.DateTimeFormat(t, locale='de_DE')
+ self.assertEqual('+0100', fmt['Z'])
+
+ def test_timezone_gmt(self):
+ tz = timezone('Europe/Berlin')
+ t = time(15, 30, tzinfo=tz)
+ fmt = dates.DateTimeFormat(t, locale='de_DE')
+ self.assertEqual('GMT+01:00', fmt['ZZZZ'])
+
+ def test_timezone_name(self):
+ tz = timezone('Europe/Paris')
+ dt = datetime(2007, 4, 1, 15, 30, tzinfo=tz)
+ fmt = dates.DateTimeFormat(dt, locale='fr_FR')
+ self.assertEqual('Heure : France', fmt['v'])
+
+ def test_timezone_location_format(self):
+ tz = timezone('Europe/Paris')
+ dt = datetime(2007, 4, 1, 15, 30, tzinfo=tz)
+ fmt = dates.DateTimeFormat(dt, locale='fr_FR')
+ self.assertEqual('Heure : France', fmt['VVVV'])
+
+ def test_timezone_walltime_short(self):
+ tz = timezone('Europe/Paris')
+ t = time(15, 30, tzinfo=tz)
+ fmt = dates.DateTimeFormat(t, locale='fr_FR')
+ self.assertEqual('Heure : France', fmt['v'])
+
+ def test_timezone_walltime_long(self):
+ tz = timezone('Europe/Paris')
+ t = time(15, 30, tzinfo=tz)
+ fmt = dates.DateTimeFormat(t, locale='fr_FR')
+ self.assertEqual(u'heure de l\u2019Europe centrale', fmt['vvvv'])
+
+ def test_hour_formatting(self):
+ l = 'en_US'
+ t = time(0, 0, 0)
+ self.assertEqual(dates.format_time(t, 'h a', locale=l), '12 AM')
+ self.assertEqual(dates.format_time(t, 'H', locale=l), '0')
+ self.assertEqual(dates.format_time(t, 'k', locale=l), '24')
+ self.assertEqual(dates.format_time(t, 'K a', locale=l), '0 AM')
+ t = time(12, 0, 0)
+ self.assertEqual(dates.format_time(t, 'h a', locale=l), '12 PM')
+ self.assertEqual(dates.format_time(t, 'H', locale=l), '12')
+ self.assertEqual(dates.format_time(t, 'k', locale=l), '12')
+ self.assertEqual(dates.format_time(t, 'K a', locale=l), '0 PM')
+
+
+class FormatDateTestCase(unittest.TestCase):
+
+ def test_with_time_fields_in_pattern(self):
+ self.assertRaises(AttributeError, dates.format_date, date(2007, 04, 01),
+ "yyyy-MM-dd HH:mm", locale='en_US')
+
+ def test_with_time_fields_in_pattern_and_datetime_param(self):
+ self.assertRaises(AttributeError, dates.format_date,
+ datetime(2007, 04, 01, 15, 30),
+ "yyyy-MM-dd HH:mm", locale='en_US')
+
+ def test_with_day_of_year_in_pattern_and_datetime_param(self):
+ # format_date should work on datetimes just as well (see #282)
+ d = datetime(2007, 04, 01)
+ self.assertEqual('14', dates.format_date(d, 'w', locale='en_US'))
+
+
+class FormatDatetimeTestCase(unittest.TestCase):
+
+ def test_with_float(self):
+ d = datetime(2012, 4, 1, 15, 30, 29, tzinfo=timezone('UTC'))
+ epoch = float(calendar.timegm(d.timetuple()))
+ formatted_string = dates.format_datetime(epoch, format='long', locale='en_US')
+ self.assertEqual(u'April 1, 2012 at 3:30:29 PM +0000', formatted_string)
+
+
+class FormatTimeTestCase(unittest.TestCase):
+
+ def test_with_naive_datetime_and_tzinfo(self):
+ string = dates.format_time(datetime(2007, 4, 1, 15, 30),
+ 'long', tzinfo=timezone('US/Eastern'),
+ locale='en')
+ self.assertEqual('11:30:00 AM EDT', string)
+
+ def test_with_float(self):
+ d = datetime(2012, 4, 1, 15, 30, 29, tzinfo=timezone('UTC'))
+ epoch = float(calendar.timegm(d.timetuple()))
+ formatted_time = dates.format_time(epoch, format='long', locale='en_US')
+ self.assertEqual(u'3:30:29 PM +0000', formatted_time)
+
+
+ def test_with_date_fields_in_pattern(self):
+ self.assertRaises(AttributeError, dates.format_time, date(2007, 04, 01),
+ "yyyy-MM-dd HH:mm", locale='en_US')
+
+ def test_with_date_fields_in_pattern_and_datetime_param(self):
+ self.assertRaises(AttributeError, dates.format_time,
+ datetime(2007, 04, 01, 15, 30),
+ "yyyy-MM-dd HH:mm", locale='en_US')
+
+
+class FormatTimedeltaTestCase(unittest.TestCase):
+
+ def test_zero_seconds(self):
+ string = dates.format_timedelta(timedelta(seconds=0), locale='en')
+ self.assertEqual('0 seconds', string)
+ string = dates.format_timedelta(timedelta(seconds=0), locale='en',
+ format='short')
+ self.assertEqual('0 secs', string)
+ string = dates.format_timedelta(timedelta(seconds=0),
+ granularity='hour', locale='en')
+ self.assertEqual('0 hours', string)
+ string = dates.format_timedelta(timedelta(seconds=0),
+ granularity='hour', locale='en',
+ format='short')
+ self.assertEqual('0 hrs', string)
+
+ def test_small_value_with_granularity(self):
+ string = dates.format_timedelta(timedelta(seconds=42),
+ granularity='hour', locale='en')
+ self.assertEqual('1 hour', string)
+ string = dates.format_timedelta(timedelta(seconds=42),
+ granularity='hour', locale='en',
+ format='short')
+ self.assertEqual('1 hr', string)
+
+ def test_direction_adding(self):
+ string = dates.format_timedelta(timedelta(hours=1),
+ locale='en',
+ add_direction=True)
+ self.assertEqual('In 1 hour', string)
+ string = dates.format_timedelta(timedelta(hours=-1),
+ locale='en',
+ add_direction=True)
+ self.assertEqual('1 hour ago', string)
+
+
+class TimeZoneAdjustTestCase(unittest.TestCase):
+ def _utc(self):
+ UTC = FixedOffsetTimezone(0, 'UTC')
+ def fake_localize(self, dt, is_dst=False):
+ raise NotImplementedError()
+ UTC.localize = new.instancemethod(fake_localize, UTC, UTC.__class__)
+ # This is important to trigger the actual bug (#257)
+ self.assertEqual(False, hasattr(UTC, 'normalize'))
+ return UTC
+
+ def test_can_format_time_with_non_pytz_timezone(self):
+ # regression test for #257
+ utc = self._utc()
+ t = datetime(2007, 4, 1, 15, 30, tzinfo=utc)
+ formatted_time = dates.format_time(t, 'long', tzinfo=utc, locale='en')
+ self.assertEqual('3:30:00 PM +0000', formatted_time)
+
+
+def suite():
+ suite = unittest.TestSuite()
+ suite.addTest(doctest.DocTestSuite(dates))
+ suite.addTest(unittest.makeSuite(DateTimeFormatTestCase))
+ suite.addTest(unittest.makeSuite(FormatDateTestCase))
+ suite.addTest(unittest.makeSuite(FormatDatetimeTestCase))
+ suite.addTest(unittest.makeSuite(FormatTimeTestCase))
+ suite.addTest(unittest.makeSuite(FormatTimedeltaTestCase))
+ suite.addTest(unittest.makeSuite(TimeZoneAdjustTestCase))
+ return suite
+
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='suite')