From acfa9332de6c7501110b196fb3117e1e65aaf779 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sun, 1 Nov 2009 04:20:17 +0000 Subject: [PATCH] Fixed #12123 -- Refactored dateformat tests to use unittest. This is needed so that we have a reliable teardown that can reset the timezone at the end of the test. git-svn-id: http://code.djangoproject.com/svn/django/trunk@11693 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/regressiontests/dateformat/tests.py | 161 +++++++++++----------- 1 file changed, 77 insertions(+), 84 deletions(-) diff --git a/tests/regressiontests/dateformat/tests.py b/tests/regressiontests/dateformat/tests.py index d649d4789c..93418d2f63 100644 --- a/tests/regressiontests/dateformat/tests.py +++ b/tests/regressiontests/dateformat/tests.py @@ -1,90 +1,83 @@ -r""" ->>> format(my_birthday, '') -u'' ->>> format(my_birthday, 'a') -u'p.m.' ->>> format(my_birthday, 'A') -u'PM' ->>> format(my_birthday, 'd') -u'08' ->>> format(my_birthday, 'j') -u'8' ->>> format(my_birthday, 'l') -u'Sunday' ->>> format(my_birthday, 'L') -u'False' ->>> format(my_birthday, 'm') -u'07' ->>> format(my_birthday, 'M') -u'Jul' ->>> format(my_birthday, 'b') -u'jul' ->>> format(my_birthday, 'n') -u'7' ->>> format(my_birthday, 'N') -u'July' ->>> no_tz or format(my_birthday, 'O') == '+0100' -True ->>> format(my_birthday, 'P') -u'10 p.m.' ->>> no_tz or format(my_birthday, 'r') == 'Sun, 8 Jul 1979 22:00:00 +0100' -True ->>> format(my_birthday, 's') -u'00' ->>> format(my_birthday, 'S') -u'th' ->>> format(my_birthday, 't') -u'31' ->>> no_tz or format(my_birthday, 'T') == 'CET' -True ->>> no_tz or format(my_birthday, 'U') == '300315600' -True ->>> format(my_birthday, 'w') -u'0' ->>> format(my_birthday, 'W') -u'27' ->>> format(my_birthday, 'y') -u'79' ->>> format(my_birthday, 'Y') -u'1979' ->>> format(my_birthday, 'z') -u'189' ->>> no_tz or format(my_birthday, 'Z') == '3600' -True - ->>> no_tz or format(summertime, 'I') == '1' -True ->>> no_tz or format(summertime, 'O') == '+0200' -True ->>> no_tz or format(wintertime, 'I') == '0' -True ->>> no_tz or format(wintertime, 'O') == '+0100' -True - ->>> format(my_birthday, r'Y z \C\E\T') -u'1979 189 CET' - ->>> format(my_birthday, r'jS o\f F') -u'8th of July' - ->>> format(the_future, r'Y') -u'2100' -""" from django.utils import dateformat, translation +from unittest import TestCase import datetime, os, time -format = dateformat.format -os.environ['TZ'] = 'Europe/Copenhagen' -translation.activate('en-us') +class DateFormatTests(TestCase): + def setUp(self): + self.old_TZ = os.environ['TZ'] + os.environ['TZ'] = 'Europe/Copenhagen' + translation.activate('en-us') -try: - time.tzset() - no_tz = False -except AttributeError: - no_tz = True + def tearDown(self): + os.environ['TZ'] = self.old_TZ -my_birthday = datetime.datetime(1979, 7, 8, 22, 00) -summertime = datetime.datetime(2005, 10, 30, 1, 00) -wintertime = datetime.datetime(2005, 10, 30, 4, 00) -the_future = datetime.datetime(2100, 10, 25, 0, 00) + def test_empty_format(self): + my_birthday = datetime.datetime(1979, 7, 8, 22, 00) + + self.assertEquals(dateformat.format(my_birthday, ''), u'') + + def test_am_pm(self): + my_birthday = datetime.datetime(1979, 7, 8, 22, 00) + + self.assertEquals(dateformat.format(my_birthday, 'a'), u'p.m.') + + def test_date_formats(self): + my_birthday = datetime.datetime(1979, 7, 8, 22, 00) + + self.assertEquals(dateformat.format(my_birthday, 'A'), u'PM') + self.assertEquals(dateformat.format(my_birthday, 'd'), u'08') + self.assertEquals(dateformat.format(my_birthday, 'j'), u'8') + self.assertEquals(dateformat.format(my_birthday, 'l'), u'Sunday') + self.assertEquals(dateformat.format(my_birthday, 'L'), u'False') + self.assertEquals(dateformat.format(my_birthday, 'm'), u'07') + self.assertEquals(dateformat.format(my_birthday, 'M'), u'Jul') + self.assertEquals(dateformat.format(my_birthday, 'b'), u'jul') + self.assertEquals(dateformat.format(my_birthday, 'n'), u'7') + self.assertEquals(dateformat.format(my_birthday, 'N'), u'July') + + def test_time_formats(self): + my_birthday = datetime.datetime(1979, 7, 8, 22, 00) + + self.assertEquals(dateformat.format(my_birthday, 'P'), u'10 p.m.') + self.assertEquals(dateformat.format(my_birthday, 's'), u'00') + self.assertEquals(dateformat.format(my_birthday, 'S'), u'th') + self.assertEquals(dateformat.format(my_birthday, 't'), u'31') + self.assertEquals(dateformat.format(my_birthday, 'w'), u'0') + self.assertEquals(dateformat.format(my_birthday, 'W'), u'27') + self.assertEquals(dateformat.format(my_birthday, 'y'), u'79') + self.assertEquals(dateformat.format(my_birthday, 'Y'), u'1979') + self.assertEquals(dateformat.format(my_birthday, 'z'), u'189') + + def test_dateformat(self): + my_birthday = datetime.datetime(1979, 7, 8, 22, 00) + + self.assertEquals(dateformat.format(my_birthday, r'Y z \C\E\T'), u'1979 189 CET') + + self.assertEquals(dateformat.format(my_birthday, r'jS o\f F'), u'8th of July') + + def test_futuredates(self): + the_future = datetime.datetime(2100, 10, 25, 0, 00) + self.assertEquals(dateformat.format(the_future, r'Y'), u'2100') + + def test_timezones(self): + my_birthday = datetime.datetime(1979, 7, 8, 22, 00) + summertime = datetime.datetime(2005, 10, 30, 1, 00) + wintertime = datetime.datetime(2005, 10, 30, 4, 00) + + try: + # Check if a timezone has been set + time.tzset() + + self.assertEquals(dateformat.format(my_birthday, 'O'), u'+0100') + self.assertEquals(dateformat.format(my_birthday, 'r'), u'Sun, 8 Jul 1979 22:00:00 +0100') + self.assertEquals(dateformat.format(my_birthday, 'T'), u'CET') + self.assertEquals(dateformat.format(my_birthday, 'U'), u'300315600') + self.assertEquals(dateformat.format(my_birthday, 'Z'), u'3600') + self.assertEquals(dateformat.format(summertime, 'I'), u'1') + self.assertEquals(dateformat.format(summertime, 'O'), u'+0200') + self.assertEquals(dateformat.format(wintertime, 'I'), u'0') + self.assertEquals(dateformat.format(wintertime, 'O'), u'+0100') + except AttributeError: + # No timezone available. Don't run the tests + pass \ No newline at end of file