Debian Codegraph @

test/test_date.py

raw symbols SCIP
1 """Tests for ClientCookie._HTTPDate."""
2
3 from __future__ import print_function
4 import re
5 import time
6 from unittest import TestCase
7
8
9 class DateTimeTests(TestCase):
10
11 def test_time2isoz(self):
12 from mechanize._util import time2isoz
13
14 base = 1019227000
15 day = 24 * 3600
16 assert time2isoz(base) == "2002-04-19 14:36:40Z"
17 assert time2isoz(base + day) == "2002-04-20 14:36:40Z"
18 assert time2isoz(base + 2 * day) == "2002-04-21 14:36:40Z"
19 assert time2isoz(base + 3 * day) == "2002-04-22 14:36:40Z"
20
21 az = time2isoz()
22 bz = time2isoz(500000)
23 for text in (az, bz):
24 assert re.search(r"^\d{4}-\d\d-\d\d \d\d:\d\d:\d\dZ$", text), \
25 "bad time2isoz format: %s %s" % (az, bz)
26
27 def test_parse_date(self):
28 from mechanize._util import http2time
29
30 def parse_date(text, http2time=http2time):
31 return time.gmtime(http2time(text))[:6]
32
33 assert parse_date("01 Jan 2001") == (2001, 1, 1, 0, 0, 0.0)
34
35 # this test will break around year 2070
36 assert parse_date("03-Feb-20") == (2020, 2, 3, 0, 0, 0.0)
37
38 # this test will break around year 2048
39 assert parse_date("03-Feb-98") == (1998, 2, 3, 0, 0, 0.0)
40
41 def test_http2time_formats(self):
42 from mechanize._util import http2time, time2isoz
43
44 # test http2time for supported dates. Test cases with 2 digit year
45 # will probably break in year 2044.
46 tests = [
47 'Thu, 03 Feb 1994 00:00:00 GMT', # proposed new HTTP format
48 'Thursday, 03-Feb-94 00:00:00 GMT', # old rfc850 HTTP format
49 'Thursday, 03-Feb-1994 00:00:00 GMT', # broken rfc850 HTTP format
50
51 '03 Feb 1994 00:00:00 GMT', # HTTP format (no weekday)
52 '03-Feb-94 00:00:00 GMT', # old rfc850 (no weekday)
53 '03-Feb-1994 00:00:00 GMT', # broken rfc850 (no weekday)
54 '03-Feb-1994 00:00 GMT', # broken rfc850 (no weekday, no seconds)
55 # broken rfc850 (no weekday, no seconds, no tz)
56 '03-Feb-1994 00:00',
57
58 '03-Feb-94', # old rfc850 HTTP format (no weekday, no time)
59 '03-Feb-1994', # broken rfc850 HTTP format (no weekday, no time)
60 '03 Feb 1994', # proposed new HTTP format (no weekday, no time)
61
62 # A few tests with extra space at various places
63 ' 03 Feb 1994 0:00 ',
64 ' 03-Feb-1994 ',
65 ]
66
67 test_t = 760233600 # assume broken POSIX counting of seconds
68 result = time2isoz(test_t)
69 expected = "1994-02-03 00:00:00Z"
70 assert result == expected, \
71 "%s => '%s' (%s)" % (test_t, result, expected)
72
73 for s in tests:
74 t = http2time(s)
75 t2 = http2time(s.lower())
76 t3 = http2time(s.upper())
77
78 assert t == t2 == t3 == test_t, \
79 "'%s' => %s, %s, %s (%s)" % (s, t, t2, t3, test_t)
80
81 def test_http2time_garbage(self):
82 from mechanize._util import http2time
83
84 for test in [
85 '', 'Garbage',
86 'Mandag 16. September 1996',
87
88 '01-00-1980',
89 '01-13-1980',
90 '00-01-1980',
91 '32-01-1980',
92 '01-01-1980 25:00:00',
93 '01-01-1980 00:61:00',
94 '01-01-1980 00:00:62']:
95
96 bad = False
97
98 if http2time(test) is not None:
99 print("http2time(%s) is not None" % (test,))
100 print("http2time(test)", http2time(test))
101 bad = True
102
103 assert not bad
104
105
106 if __name__ == "__main__":
107 import unittest
108 unittest.main()