Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit acfa9332de6c7501110b196fb3117e1e65aaf779 1 parent 08d521e
Russell Keith-Magee authored November 01, 2009

Showing 1 changed file with 77 additions and 84 deletions. Show diff stats Hide diff stats

  1. 161  tests/regressiontests/dateformat/tests.py
161  tests/regressiontests/dateformat/tests.py
... ...
@@ -1,90 +1,83 @@
1  
-r"""
2  
->>> format(my_birthday, '')
3  
-u''
4  
->>> format(my_birthday, 'a')
5  
-u'p.m.'
6  
->>> format(my_birthday, 'A')
7  
-u'PM'
8  
->>> format(my_birthday, 'd')
9  
-u'08'
10  
->>> format(my_birthday, 'j')
11  
-u'8'
12  
->>> format(my_birthday, 'l')
13  
-u'Sunday'
14  
->>> format(my_birthday, 'L')
15  
-u'False'
16  
->>> format(my_birthday, 'm')
17  
-u'07'
18  
->>> format(my_birthday, 'M')
19  
-u'Jul'
20  
->>> format(my_birthday, 'b')
21  
-u'jul'
22  
->>> format(my_birthday, 'n')
23  
-u'7'
24  
->>> format(my_birthday, 'N')
25  
-u'July'
26  
->>> no_tz or format(my_birthday, 'O') == '+0100'
27  
-True
28  
->>> format(my_birthday, 'P')
29  
-u'10 p.m.'
30  
->>> no_tz or format(my_birthday, 'r') == 'Sun, 8 Jul 1979 22:00:00 +0100'
31  
-True
32  
->>> format(my_birthday, 's')
33  
-u'00'
34  
->>> format(my_birthday, 'S')
35  
-u'th'
36  
->>> format(my_birthday, 't')
37  
-u'31'
38  
->>> no_tz or format(my_birthday, 'T') == 'CET'
39  
-True
40  
->>> no_tz or format(my_birthday, 'U') == '300315600'
41  
-True
42  
->>> format(my_birthday, 'w')
43  
-u'0'
44  
->>> format(my_birthday, 'W')
45  
-u'27'
46  
->>> format(my_birthday, 'y')
47  
-u'79'
48  
->>> format(my_birthday, 'Y')
49  
-u'1979'
50  
->>> format(my_birthday, 'z')
51  
-u'189'
52  
->>> no_tz or format(my_birthday, 'Z') == '3600'
53  
-True
54  
-
55  
->>> no_tz or format(summertime, 'I') == '1'
56  
-True
57  
->>> no_tz or format(summertime, 'O') == '+0200'
58  
-True
59  
->>> no_tz or format(wintertime, 'I') == '0'
60  
-True
61  
->>> no_tz or format(wintertime, 'O') == '+0100'
62  
-True
63  
-
64  
->>> format(my_birthday, r'Y z \C\E\T')
65  
-u'1979 189 CET'
66  
-
67  
->>> format(my_birthday, r'jS o\f F')
68  
-u'8th of July'
69  
-
70  
->>> format(the_future, r'Y')
71  
-u'2100'
72  
-"""
73 1
 
74 2
 from django.utils import dateformat, translation
  3
+from unittest import TestCase
75 4
 import datetime, os, time
76 5
 
77  
-format = dateformat.format
78  
-os.environ['TZ'] = 'Europe/Copenhagen'
79  
-translation.activate('en-us')
  6
+class DateFormatTests(TestCase):
  7
+    def setUp(self):
  8
+        self.old_TZ = os.environ['TZ']
  9
+        os.environ['TZ'] = 'Europe/Copenhagen'
  10
+        translation.activate('en-us')
80 11
 
81  
-try:
82  
-    time.tzset()
83  
-    no_tz = False
84  
-except AttributeError:
85  
-    no_tz = True
  12
+    def tearDown(self):
  13
+        os.environ['TZ'] = self.old_TZ
86 14
 
87  
-my_birthday = datetime.datetime(1979, 7, 8, 22, 00)
88  
-summertime = datetime.datetime(2005, 10, 30, 1, 00)
89  
-wintertime = datetime.datetime(2005, 10, 30, 4, 00)
90  
-the_future = datetime.datetime(2100, 10, 25, 0, 00)
  15
+    def test_empty_format(self):
  16
+        my_birthday = datetime.datetime(1979, 7, 8, 22, 00)
  17
+
  18
+        self.assertEquals(dateformat.format(my_birthday, ''), u'')
  19
+
  20
+    def test_am_pm(self):
  21
+        my_birthday = datetime.datetime(1979, 7, 8, 22, 00)
  22
+
  23
+        self.assertEquals(dateformat.format(my_birthday, 'a'), u'p.m.')
  24
+
  25
+    def test_date_formats(self):
  26
+        my_birthday = datetime.datetime(1979, 7, 8, 22, 00)
  27
+
  28
+        self.assertEquals(dateformat.format(my_birthday, 'A'), u'PM')
  29
+        self.assertEquals(dateformat.format(my_birthday, 'd'), u'08')
  30
+        self.assertEquals(dateformat.format(my_birthday, 'j'), u'8')
  31
+        self.assertEquals(dateformat.format(my_birthday, 'l'), u'Sunday')
  32
+        self.assertEquals(dateformat.format(my_birthday, 'L'), u'False')
  33
+        self.assertEquals(dateformat.format(my_birthday, 'm'), u'07')
  34
+        self.assertEquals(dateformat.format(my_birthday, 'M'), u'Jul')
  35
+        self.assertEquals(dateformat.format(my_birthday, 'b'), u'jul')
  36
+        self.assertEquals(dateformat.format(my_birthday, 'n'), u'7')
  37
+        self.assertEquals(dateformat.format(my_birthday, 'N'), u'July')
  38
+
  39
+    def test_time_formats(self):
  40
+        my_birthday = datetime.datetime(1979, 7, 8, 22, 00)
  41
+
  42
+        self.assertEquals(dateformat.format(my_birthday, 'P'), u'10 p.m.')
  43
+        self.assertEquals(dateformat.format(my_birthday, 's'), u'00')
  44
+        self.assertEquals(dateformat.format(my_birthday, 'S'), u'th')
  45
+        self.assertEquals(dateformat.format(my_birthday, 't'), u'31')
  46
+        self.assertEquals(dateformat.format(my_birthday, 'w'), u'0')
  47
+        self.assertEquals(dateformat.format(my_birthday, 'W'), u'27')
  48
+        self.assertEquals(dateformat.format(my_birthday, 'y'), u'79')
  49
+        self.assertEquals(dateformat.format(my_birthday, 'Y'), u'1979')
  50
+        self.assertEquals(dateformat.format(my_birthday, 'z'), u'189')
  51
+
  52
+    def test_dateformat(self):
  53
+        my_birthday = datetime.datetime(1979, 7, 8, 22, 00)
  54
+
  55
+        self.assertEquals(dateformat.format(my_birthday, r'Y z \C\E\T'), u'1979 189 CET')
  56
+
  57
+        self.assertEquals(dateformat.format(my_birthday, r'jS o\f F'), u'8th of July')
  58
+
  59
+    def test_futuredates(self):
  60
+        the_future = datetime.datetime(2100, 10, 25, 0, 00)
  61
+        self.assertEquals(dateformat.format(the_future, r'Y'), u'2100')
  62
+
  63
+    def test_timezones(self):
  64
+        my_birthday = datetime.datetime(1979, 7, 8, 22, 00)
  65
+        summertime = datetime.datetime(2005, 10, 30, 1, 00)
  66
+        wintertime = datetime.datetime(2005, 10, 30, 4, 00)
  67
+
  68
+        try:
  69
+            # Check if a timezone has been set
  70
+            time.tzset()
  71
+
  72
+            self.assertEquals(dateformat.format(my_birthday, 'O'), u'+0100')
  73
+            self.assertEquals(dateformat.format(my_birthday, 'r'), u'Sun, 8 Jul 1979 22:00:00 +0100')
  74
+            self.assertEquals(dateformat.format(my_birthday, 'T'), u'CET')
  75
+            self.assertEquals(dateformat.format(my_birthday, 'U'), u'300315600')
  76
+            self.assertEquals(dateformat.format(my_birthday, 'Z'), u'3600')
  77
+            self.assertEquals(dateformat.format(summertime, 'I'), u'1')
  78
+            self.assertEquals(dateformat.format(summertime, 'O'), u'+0200')
  79
+            self.assertEquals(dateformat.format(wintertime, 'I'), u'0')
  80
+            self.assertEquals(dateformat.format(wintertime, 'O'), u'+0100')
  81
+        except AttributeError:
  82
+            # No timezone available. Don't run the tests
  83
+            pass

0 notes on commit acfa933

Please sign in to comment.
Something went wrong with that request. Please try again.