comparison env/lib/python3.9/site-packages/isodate/tests/test_datetime.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 ##############################################################################
2 # Copyright 2009, Gerhard Weis
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
7 #
8 # * Redistributions of source code must retain the above copyright notice,
9 # this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright notice,
11 # this list of conditions and the following disclaimer in the documentation
12 # and/or other materials provided with the distribution.
13 # * Neither the name of the authors nor the names of its contributors
14 # may be used to endorse or promote products derived from this software
15 # without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 # CONTRACT, STRICT LIABILITY, OR TORT
26 ##############################################################################
27 '''
28 Test cases for the isodatetime module.
29 '''
30 import unittest
31 from datetime import datetime
32
33 from isodate import parse_datetime, UTC, FixedOffset, datetime_isoformat
34 from isodate import ISO8601Error
35 from isodate import DATE_BAS_COMPLETE, TIME_BAS_MINUTE, TIME_BAS_COMPLETE
36 from isodate import DATE_EXT_COMPLETE, TIME_EXT_MINUTE, TIME_EXT_COMPLETE
37 from isodate import TZ_BAS, TZ_EXT, TZ_HOUR
38 from isodate import DATE_BAS_ORD_COMPLETE, DATE_EXT_ORD_COMPLETE
39 from isodate import DATE_BAS_WEEK_COMPLETE, DATE_EXT_WEEK_COMPLETE
40
41 # the following list contains tuples of ISO datetime strings and the expected
42 # result from the parse_datetime method. A result of None means an ISO8601Error
43 # is expected.
44 TEST_CASES = [('19850412T1015', datetime(1985, 4, 12, 10, 15),
45 DATE_BAS_COMPLETE + 'T' + TIME_BAS_MINUTE,
46 '19850412T1015'),
47 ('1985-04-12T10:15', datetime(1985, 4, 12, 10, 15),
48 DATE_EXT_COMPLETE + 'T' + TIME_EXT_MINUTE,
49 '1985-04-12T10:15'),
50 ('1985102T1015Z', datetime(1985, 4, 12, 10, 15, tzinfo=UTC),
51 DATE_BAS_ORD_COMPLETE + 'T' + TIME_BAS_MINUTE + TZ_BAS,
52 '1985102T1015Z'),
53 ('1985-102T10:15Z', datetime(1985, 4, 12, 10, 15, tzinfo=UTC),
54 DATE_EXT_ORD_COMPLETE + 'T' + TIME_EXT_MINUTE + TZ_EXT,
55 '1985-102T10:15Z'),
56 ('1985W155T1015+0400', datetime(1985, 4, 12, 10, 15,
57 tzinfo=FixedOffset(4, 0,
58 '+0400')),
59 DATE_BAS_WEEK_COMPLETE + 'T' + TIME_BAS_MINUTE + TZ_BAS,
60 '1985W155T1015+0400'),
61 ('1985-W15-5T10:15+04', datetime(1985, 4, 12, 10, 15,
62 tzinfo=FixedOffset(4, 0,
63 '+0400'),),
64 DATE_EXT_WEEK_COMPLETE + 'T' + TIME_EXT_MINUTE + TZ_HOUR,
65 '1985-W15-5T10:15+04'),
66 ('1985-W15-5T10:15-0430',
67 datetime(1985, 4, 12, 10, 15, tzinfo=FixedOffset(-4, -30,
68 '-0430'),),
69 DATE_EXT_WEEK_COMPLETE + 'T' + TIME_EXT_MINUTE + TZ_BAS,
70 '1985-W15-5T10:15-0430'),
71 ('1985-W15-5T10:15+04:45',
72 datetime(1985, 4, 12, 10, 15, tzinfo=FixedOffset(4, 45,
73 '+04:45'),),
74 DATE_EXT_WEEK_COMPLETE + 'T' + TIME_EXT_MINUTE + TZ_EXT,
75 '1985-W15-5T10:15+04:45'),
76 ('20110410T101225.123000Z',
77 datetime(2011, 4, 10, 10, 12, 25, 123000, tzinfo=UTC),
78 DATE_BAS_COMPLETE + 'T' + TIME_BAS_COMPLETE + ".%f" + TZ_BAS,
79 '20110410T101225.123000Z'),
80 ('2012-10-12T08:29:46.069178Z',
81 datetime(2012, 10, 12, 8, 29, 46, 69178, tzinfo=UTC),
82 DATE_EXT_COMPLETE + 'T' + TIME_EXT_COMPLETE + '.%f' + TZ_BAS,
83 '2012-10-12T08:29:46.069178Z'),
84 ('2012-10-12T08:29:46.691780Z',
85 datetime(2012, 10, 12, 8, 29, 46, 691780, tzinfo=UTC),
86 DATE_EXT_COMPLETE + 'T' + TIME_EXT_COMPLETE + '.%f' + TZ_BAS,
87 '2012-10-12T08:29:46.691780Z'),
88 ('2012-10-30T08:55:22.1234567Z',
89 datetime(2012, 10, 30, 8, 55, 22, 123457, tzinfo=UTC),
90 DATE_EXT_COMPLETE + 'T' + TIME_EXT_COMPLETE + '.%f' + TZ_BAS,
91 '2012-10-30T08:55:22.123457Z'),
92 ('2012-10-30T08:55:22.1234561Z',
93 datetime(2012, 10, 30, 8, 55, 22, 123456, tzinfo=UTC),
94 DATE_EXT_COMPLETE + 'T' + TIME_EXT_COMPLETE + '.%f' + TZ_BAS,
95 '2012-10-30T08:55:22.123456Z'),
96 ('2014-08-18 14:55:22.123456Z', None,
97 DATE_EXT_COMPLETE + 'T' + TIME_EXT_COMPLETE + '.%f' + TZ_BAS,
98 '2014-08-18T14:55:22.123456Z'),
99 ]
100
101
102 def create_testcase(datetimestring, expectation, format, output):
103 """
104 Create a TestCase class for a specific test.
105
106 This allows having a separate TestCase for each test tuple from the
107 TEST_CASES list, so that a failed test won't stop other tests.
108 """
109
110 class TestDateTime(unittest.TestCase):
111 '''
112 A test case template to parse an ISO datetime string into a
113 datetime object.
114 '''
115
116 def test_parse(self):
117 '''
118 Parse an ISO datetime string and compare it to the expected value.
119 '''
120 if expectation is None:
121 self.assertRaises(ISO8601Error, parse_datetime, datetimestring)
122 else:
123 self.assertEqual(parse_datetime(datetimestring), expectation)
124
125 def test_format(self):
126 '''
127 Take datetime object and create ISO string from it.
128 This is the reverse test to test_parse.
129 '''
130 if expectation is None:
131 self.assertRaises(AttributeError,
132 datetime_isoformat, expectation, format)
133 else:
134 self.assertEqual(datetime_isoformat(expectation, format),
135 output)
136
137 return unittest.TestLoader().loadTestsFromTestCase(TestDateTime)
138
139
140 def test_suite():
141 '''
142 Construct a TestSuite instance for all test cases.
143 '''
144 suite = unittest.TestSuite()
145 for datetimestring, expectation, format, output in TEST_CASES:
146 suite.addTest(create_testcase(datetimestring, expectation,
147 format, output))
148 return suite
149
150
151 # load_tests Protocol
152 def load_tests(loader, tests, pattern):
153 return test_suite()
154
155
156 if __name__ == '__main__':
157 unittest.main(defaultTest='test_suite')