comparison env/lib/python3.9/site-packages/isodate/tests/test_time.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 isotime module.
29 '''
30 import unittest
31 from datetime import time
32
33 from isodate import parse_time, UTC, FixedOffset, ISO8601Error, time_isoformat
34 from isodate import TIME_BAS_COMPLETE, TIME_BAS_MINUTE
35 from isodate import TIME_EXT_COMPLETE, TIME_EXT_MINUTE
36 from isodate import TIME_HOUR
37 from isodate import TZ_BAS, TZ_EXT, TZ_HOUR
38
39 # the following list contains tuples of ISO time strings and the expected
40 # result from the parse_time method. A result of None means an ISO8601Error
41 # is expected.
42 TEST_CASES = [('232050', time(23, 20, 50), TIME_BAS_COMPLETE + TZ_BAS),
43 ('23:20:50', time(23, 20, 50), TIME_EXT_COMPLETE + TZ_EXT),
44 ('2320', time(23, 20), TIME_BAS_MINUTE),
45 ('23:20', time(23, 20), TIME_EXT_MINUTE),
46 ('23', time(23), TIME_HOUR),
47 ('232050,5', time(23, 20, 50, 500000), None),
48 ('23:20:50.5', time(23, 20, 50, 500000), None),
49 # test precision
50 ('15:33:42.123456', time(15, 33, 42, 123456), None),
51 ('15:33:42.1234564', time(15, 33, 42, 123456), None),
52 ('15:33:42.1234557', time(15, 33, 42, 123456), None),
53 ('2320,8', time(23, 20, 48), None),
54 ('23:20,8', time(23, 20, 48), None),
55 ('23,3', time(23, 18), None),
56 ('232030Z', time(23, 20, 30, tzinfo=UTC),
57 TIME_BAS_COMPLETE + TZ_BAS),
58 ('2320Z', time(23, 20, tzinfo=UTC), TIME_BAS_MINUTE + TZ_BAS),
59 ('23Z', time(23, tzinfo=UTC), TIME_HOUR + TZ_BAS),
60 ('23:20:30Z', time(23, 20, 30, tzinfo=UTC),
61 TIME_EXT_COMPLETE + TZ_EXT),
62 ('23:20Z', time(23, 20, tzinfo=UTC), TIME_EXT_MINUTE + TZ_EXT),
63 ('152746+0100', time(15, 27, 46,
64 tzinfo=FixedOffset(1, 0, '+0100')), TIME_BAS_COMPLETE + TZ_BAS),
65 ('152746-0500', time(15, 27, 46,
66 tzinfo=FixedOffset(-5, 0, '-0500')),
67 TIME_BAS_COMPLETE + TZ_BAS),
68 ('152746+01', time(15, 27, 46,
69 tzinfo=FixedOffset(1, 0, '+01:00')),
70 TIME_BAS_COMPLETE + TZ_HOUR),
71 ('152746-05', time(15, 27, 46,
72 tzinfo=FixedOffset(-5, -0, '-05:00')),
73 TIME_BAS_COMPLETE + TZ_HOUR),
74 ('15:27:46+01:00', time(15, 27, 46,
75 tzinfo=FixedOffset(1, 0, '+01:00')),
76 TIME_EXT_COMPLETE + TZ_EXT),
77 ('15:27:46-05:00', time(15, 27, 46,
78 tzinfo=FixedOffset(-5, -0, '-05:00')),
79 TIME_EXT_COMPLETE + TZ_EXT),
80 ('15:27:46+01', time(15, 27, 46,
81 tzinfo=FixedOffset(1, 0, '+01:00')),
82 TIME_EXT_COMPLETE + TZ_HOUR),
83 ('15:27:46-05', time(15, 27, 46,
84 tzinfo=FixedOffset(-5, -0, '-05:00')),
85 TIME_EXT_COMPLETE + TZ_HOUR),
86 ('15:27:46-05:30', time(15, 27, 46,
87 tzinfo=FixedOffset(-5, -30, '-05:30')),
88 TIME_EXT_COMPLETE + TZ_EXT),
89 ('15:27:46-0545', time(15, 27, 46,
90 tzinfo=FixedOffset(-5, -45, '-0545')),
91 TIME_EXT_COMPLETE + TZ_BAS),
92 ('1:17:30', None, TIME_EXT_COMPLETE)]
93
94
95 def create_testcase(timestring, expectation, format):
96 """
97 Create a TestCase class for a specific test.
98
99 This allows having a separate TestCase for each test tuple from the
100 TEST_CASES list, so that a failed test won't stop other tests.
101 """
102
103 class TestTime(unittest.TestCase):
104 '''
105 A test case template to parse an ISO time string into a time
106 object.
107 '''
108
109 def test_parse(self):
110 '''
111 Parse an ISO time string and compare it to the expected value.
112 '''
113 if expectation is None:
114 self.assertRaises(ISO8601Error, parse_time, timestring)
115 else:
116 result = parse_time(timestring)
117 self.assertEqual(result, expectation)
118
119 def test_format(self):
120 '''
121 Take time object and create ISO string from it.
122 This is the reverse test to test_parse.
123 '''
124 if expectation is None:
125 self.assertRaises(AttributeError,
126 time_isoformat, expectation, format)
127 elif format is not None:
128 self.assertEqual(time_isoformat(expectation, format),
129 timestring)
130
131 return unittest.TestLoader().loadTestsFromTestCase(TestTime)
132
133
134 def test_suite():
135 '''
136 Construct a TestSuite instance for all test cases.
137 '''
138 suite = unittest.TestSuite()
139 for timestring, expectation, format in TEST_CASES:
140 suite.addTest(create_testcase(timestring, expectation, format))
141 return suite
142
143
144 # load_tests Protocol
145 def load_tests(loader, tests, pattern):
146 return test_suite()
147
148
149 if __name__ == '__main__':
150 unittest.main(defaultTest='test_suite')