From e109de3b7e7e0d7ea8f600c21811eef867a3ffd3 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sun, 1 Nov 2009 04:54:56 +0000 Subject: [PATCH] [1.1.X] Fixed #12123 -- Updated the utils.dateformat test to be a unittest too, due to a dependency on the old dateformat test. Backport of r11695 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@11696 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/regressiontests/utils/dateformat.py | 78 +++++++++++------------ tests/regressiontests/utils/tests.py | 3 +- 2 files changed, 38 insertions(+), 43 deletions(-) diff --git a/tests/regressiontests/utils/dateformat.py b/tests/regressiontests/utils/dateformat.py index 63b8201752..6afbcd61ec 100644 --- a/tests/regressiontests/utils/dateformat.py +++ b/tests/regressiontests/utils/dateformat.py @@ -1,48 +1,42 @@ -""" ->>> from datetime import datetime, date ->>> from django.utils.dateformat import format ->>> from django.utils.tzinfo import FixedOffset, LocalTimezone +import os +from unittest import TestCase +from datetime import datetime, date +from django.utils.dateformat import format +from django.utils.tzinfo import FixedOffset, LocalTimezone -# date ->>> d = date(2009, 5, 16) ->>> date.fromtimestamp(int(format(d, 'U'))) == d -True +class DateFormatTests(TestCase): + def setUp(self): + self.old_TZ = os.environ['TZ'] + os.environ['TZ'] = 'Europe/Copenhagen' -# Naive datetime ->>> dt = datetime(2009, 5, 16, 5, 30, 30) ->>> datetime.fromtimestamp(int(format(dt, 'U'))) == dt -True + def tearDown(self): + os.environ['TZ'] = self.old_TZ -# datetime with local tzinfo ->>> ltz = LocalTimezone(datetime.now()) ->>> dt = datetime(2009, 5, 16, 5, 30, 30, tzinfo=ltz) ->>> datetime.fromtimestamp(int(format(dt, 'U')), ltz) == dt -True ->>> datetime.fromtimestamp(int(format(dt, 'U'))) == dt.replace(tzinfo=None) -True + def test_date(self): + d = date(2009, 5, 16) + self.assertEquals(date.fromtimestamp(int(format(d, 'U'))), d) -# datetime with arbitrary tzinfo ->>> tz = FixedOffset(-510) ->>> ltz = LocalTimezone(datetime.now()) ->>> dt = datetime(2009, 5, 16, 5, 30, 30, tzinfo=tz) ->>> datetime.fromtimestamp(int(format(dt, 'U')), tz) == dt -True ->>> datetime.fromtimestamp(int(format(dt, 'U')), ltz) == dt -True ->>> datetime.fromtimestamp(int(format(dt, 'U'))) == dt.astimezone(ltz).replace(tzinfo=None) -True ->>> datetime.fromtimestamp(int(format(dt, 'U')), tz).utctimetuple() == dt.utctimetuple() -True ->>> datetime.fromtimestamp(int(format(dt, 'U')), ltz).utctimetuple() == dt.utctimetuple() -True + def test_naive_datetime(self): + dt = datetime(2009, 5, 16, 5, 30, 30) + self.assertEquals(datetime.fromtimestamp(int(format(dt, 'U'))), dt) -# Epoch ->>> utc = FixedOffset(0) ->>> udt = datetime(1970, 1, 1, tzinfo=utc) ->>> format(udt, 'U') -u'0' -""" + def test_datetime_with_local_tzinfo(self): + ltz = LocalTimezone(datetime.now()) + dt = datetime(2009, 5, 16, 5, 30, 30, tzinfo=ltz) + self.assertEquals(datetime.fromtimestamp(int(format(dt, 'U')), ltz), dt) + self.assertEquals(datetime.fromtimestamp(int(format(dt, 'U'))), dt.replace(tzinfo=None)) -if __name__ == "__main__": - import doctest - doctest.testmod() + def test_datetime_with_tzinfo(self): + tz = FixedOffset(-510) + ltz = LocalTimezone(datetime.now()) + dt = datetime(2009, 5, 16, 5, 30, 30, tzinfo=tz) + self.assertEquals(datetime.fromtimestamp(int(format(dt, 'U')), tz), dt) + self.assertEquals(datetime.fromtimestamp(int(format(dt, 'U')), ltz), dt) + self.assertEquals(datetime.fromtimestamp(int(format(dt, 'U'))), dt.astimezone(ltz).replace(tzinfo=None)) + self.assertEquals(datetime.fromtimestamp(int(format(dt, 'U')), tz).utctimetuple(), dt.utctimetuple()) + self.assertEquals(datetime.fromtimestamp(int(format(dt, 'U')), ltz).utctimetuple(), dt.utctimetuple()) + + def test_epoch(self): + utc = FixedOffset(0) + udt = datetime(1970, 1, 1, tzinfo=utc) + self.assertEquals(format(udt, 'U'), u'0') diff --git a/tests/regressiontests/utils/tests.py b/tests/regressiontests/utils/tests.py index c34dc96f88..a7a6e4c3a1 100644 --- a/tests/regressiontests/utils/tests.py +++ b/tests/regressiontests/utils/tests.py @@ -24,10 +24,11 @@ except NameError: __test__ = { 'timesince': timesince, 'datastructures': datastructures, - 'dateformat': dateformat, 'itercompat': itercompat, } +from dateformat import * + class TestUtilsHtml(TestCase): def check_output(self, function, value, output=None):