ยปCore Development>Code coverage>Lib/test/test_email/test_utils.py

Python code coverage for Lib/test/test_email/test_utils.py

#countcontent
1n/aimport datetime
2n/afrom email import utils
3n/aimport test.support
4n/aimport time
5n/aimport unittest
6n/aimport sys
7n/aimport os.path
8n/a
9n/aclass DateTimeTests(unittest.TestCase):
10n/a
11n/a datestring = 'Sun, 23 Sep 2001 20:10:55'
12n/a dateargs = (2001, 9, 23, 20, 10, 55)
13n/a offsetstring = ' -0700'
14n/a utcoffset = datetime.timedelta(hours=-7)
15n/a tz = datetime.timezone(utcoffset)
16n/a naive_dt = datetime.datetime(*dateargs)
17n/a aware_dt = datetime.datetime(*dateargs, tzinfo=tz)
18n/a
19n/a def test_naive_datetime(self):
20n/a self.assertEqual(utils.format_datetime(self.naive_dt),
21n/a self.datestring + ' -0000')
22n/a
23n/a def test_aware_datetime(self):
24n/a self.assertEqual(utils.format_datetime(self.aware_dt),
25n/a self.datestring + self.offsetstring)
26n/a
27n/a def test_usegmt(self):
28n/a utc_dt = datetime.datetime(*self.dateargs,
29n/a tzinfo=datetime.timezone.utc)
30n/a self.assertEqual(utils.format_datetime(utc_dt, usegmt=True),
31n/a self.datestring + ' GMT')
32n/a
33n/a def test_usegmt_with_naive_datetime_raises(self):
34n/a with self.assertRaises(ValueError):
35n/a utils.format_datetime(self.naive_dt, usegmt=True)
36n/a
37n/a def test_usegmt_with_non_utc_datetime_raises(self):
38n/a with self.assertRaises(ValueError):
39n/a utils.format_datetime(self.aware_dt, usegmt=True)
40n/a
41n/a def test_parsedate_to_datetime(self):
42n/a self.assertEqual(
43n/a utils.parsedate_to_datetime(self.datestring + self.offsetstring),
44n/a self.aware_dt)
45n/a
46n/a def test_parsedate_to_datetime_naive(self):
47n/a self.assertEqual(
48n/a utils.parsedate_to_datetime(self.datestring + ' -0000'),
49n/a self.naive_dt)
50n/a
51n/a
52n/aclass LocaltimeTests(unittest.TestCase):
53n/a
54n/a def test_localtime_is_tz_aware_daylight_true(self):
55n/a test.support.patch(self, time, 'daylight', True)
56n/a t = utils.localtime()
57n/a self.assertIsNotNone(t.tzinfo)
58n/a
59n/a def test_localtime_is_tz_aware_daylight_false(self):
60n/a test.support.patch(self, time, 'daylight', False)
61n/a t = utils.localtime()
62n/a self.assertIsNotNone(t.tzinfo)
63n/a
64n/a def test_localtime_daylight_true_dst_false(self):
65n/a test.support.patch(self, time, 'daylight', True)
66n/a t0 = datetime.datetime(2012, 3, 12, 1, 1)
67n/a t1 = utils.localtime(t0, isdst=-1)
68n/a t2 = utils.localtime(t1)
69n/a self.assertEqual(t1, t2)
70n/a
71n/a def test_localtime_daylight_false_dst_false(self):
72n/a test.support.patch(self, time, 'daylight', False)
73n/a t0 = datetime.datetime(2012, 3, 12, 1, 1)
74n/a t1 = utils.localtime(t0, isdst=-1)
75n/a t2 = utils.localtime(t1)
76n/a self.assertEqual(t1, t2)
77n/a
78n/a def test_localtime_daylight_true_dst_true(self):
79n/a test.support.patch(self, time, 'daylight', True)
80n/a t0 = datetime.datetime(2012, 3, 12, 1, 1)
81n/a t1 = utils.localtime(t0, isdst=1)
82n/a t2 = utils.localtime(t1)
83n/a self.assertEqual(t1, t2)
84n/a
85n/a def test_localtime_daylight_false_dst_true(self):
86n/a test.support.patch(self, time, 'daylight', False)
87n/a t0 = datetime.datetime(2012, 3, 12, 1, 1)
88n/a t1 = utils.localtime(t0, isdst=1)
89n/a t2 = utils.localtime(t1)
90n/a self.assertEqual(t1, t2)
91n/a
92n/a @test.support.run_with_tz('EST+05EDT,M3.2.0,M11.1.0')
93n/a def test_localtime_epoch_utc_daylight_true(self):
94n/a test.support.patch(self, time, 'daylight', True)
95n/a t0 = datetime.datetime(1990, 1, 1, tzinfo = datetime.timezone.utc)
96n/a t1 = utils.localtime(t0)
97n/a t2 = t0 - datetime.timedelta(hours=5)
98n/a t2 = t2.replace(tzinfo = datetime.timezone(datetime.timedelta(hours=-5)))
99n/a self.assertEqual(t1, t2)
100n/a
101n/a @test.support.run_with_tz('EST+05EDT,M3.2.0,M11.1.0')
102n/a def test_localtime_epoch_utc_daylight_false(self):
103n/a test.support.patch(self, time, 'daylight', False)
104n/a t0 = datetime.datetime(1990, 1, 1, tzinfo = datetime.timezone.utc)
105n/a t1 = utils.localtime(t0)
106n/a t2 = t0 - datetime.timedelta(hours=5)
107n/a t2 = t2.replace(tzinfo = datetime.timezone(datetime.timedelta(hours=-5)))
108n/a self.assertEqual(t1, t2)
109n/a
110n/a def test_localtime_epoch_notz_daylight_true(self):
111n/a test.support.patch(self, time, 'daylight', True)
112n/a t0 = datetime.datetime(1990, 1, 1)
113n/a t1 = utils.localtime(t0)
114n/a t2 = utils.localtime(t0.replace(tzinfo=None))
115n/a self.assertEqual(t1, t2)
116n/a
117n/a def test_localtime_epoch_notz_daylight_false(self):
118n/a test.support.patch(self, time, 'daylight', False)
119n/a t0 = datetime.datetime(1990, 1, 1)
120n/a t1 = utils.localtime(t0)
121n/a t2 = utils.localtime(t0.replace(tzinfo=None))
122n/a self.assertEqual(t1, t2)
123n/a
124n/a # XXX: Need a more robust test for Olson's tzdata
125n/a @unittest.skipIf(sys.platform.startswith('win'),
126n/a "Windows does not use Olson's TZ database")
127n/a @unittest.skipUnless(os.path.exists('/usr/share/zoneinfo') or
128n/a os.path.exists('/usr/lib/zoneinfo'),
129n/a "Can't find the Olson's TZ database")
130n/a @test.support.run_with_tz('Europe/Kiev')
131n/a def test_variable_tzname(self):
132n/a t0 = datetime.datetime(1984, 1, 1, tzinfo=datetime.timezone.utc)
133n/a t1 = utils.localtime(t0)
134n/a self.assertEqual(t1.tzname(), 'MSK')
135n/a t0 = datetime.datetime(1994, 1, 1, tzinfo=datetime.timezone.utc)
136n/a t1 = utils.localtime(t0)
137n/a self.assertEqual(t1.tzname(), 'EET')
138n/a
139n/a# Issue #24836: The timezone files are out of date (pre 2011k)
140n/a# on Mac OS X Snow Leopard.
141n/a@test.support.requires_mac_ver(10, 7)
142n/aclass FormatDateTests(unittest.TestCase):
143n/a
144n/a @test.support.run_with_tz('Europe/Minsk')
145n/a def test_formatdate(self):
146n/a timeval = time.mktime((2011, 12, 1, 18, 0, 0, 4, 335, 0))
147n/a string = utils.formatdate(timeval, localtime=False, usegmt=False)
148n/a self.assertEqual(string, 'Thu, 01 Dec 2011 15:00:00 -0000')
149n/a string = utils.formatdate(timeval, localtime=False, usegmt=True)
150n/a self.assertEqual(string, 'Thu, 01 Dec 2011 15:00:00 GMT')
151n/a
152n/a @test.support.run_with_tz('Europe/Minsk')
153n/a def test_formatdate_with_localtime(self):
154n/a timeval = time.mktime((2011, 1, 1, 18, 0, 0, 6, 1, 0))
155n/a string = utils.formatdate(timeval, localtime=True)
156n/a self.assertEqual(string, 'Sat, 01 Jan 2011 18:00:00 +0200')
157n/a # Minsk moved from +0200 (with DST) to +0300 (without DST) in 2011
158n/a timeval = time.mktime((2011, 12, 1, 18, 0, 0, 4, 335, 0))
159n/a string = utils.formatdate(timeval, localtime=True)
160n/a self.assertEqual(string, 'Thu, 01 Dec 2011 18:00:00 +0300')
161n/a
162n/aif __name__ == '__main__':
163n/a unittest.main()