comparison env/lib/python3.7/site-packages/isodate/tests/test_strf.py @ 2:6af9afd405e9 draft

"planemo upload commit 0a63dd5f4d38a1f6944587f52a8cd79874177fc1"
author shellac
date Thu, 14 May 2020 14:56:58 -0400
parents 26e78fe6e8c4
children
comparison
equal deleted inserted replaced
1:75ca89e9b81c 2:6af9afd405e9
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 isodate module.
29 '''
30 import unittest
31 import time
32 from datetime import datetime, timedelta
33 from isodate import strftime
34 from isodate import LOCAL
35 from isodate import DT_EXT_COMPLETE
36 from isodate import tzinfo
37
38
39 TEST_CASES = ((datetime(2012, 12, 25, 13, 30, 0, 0, LOCAL), DT_EXT_COMPLETE,
40 "2012-12-25T13:30:00+10:00"),
41 # DST ON
42 (datetime(1999, 12, 25, 13, 30, 0, 0, LOCAL), DT_EXT_COMPLETE,
43 "1999-12-25T13:30:00+11:00"),
44 # microseconds
45 (datetime(2012, 10, 12, 8, 29, 46, 69178),
46 "%Y-%m-%dT%H:%M:%S.%f",
47 "2012-10-12T08:29:46.069178"),
48 (datetime(2012, 10, 12, 8, 29, 46, 691780),
49 "%Y-%m-%dT%H:%M:%S.%f",
50 "2012-10-12T08:29:46.691780"),
51 )
52
53
54 def create_testcase(dt, format, expectation):
55 """
56 Create a TestCase class for a specific test.
57
58 This allows having a separate TestCase for each test tuple from the
59 TEST_CASES list, so that a failed test won't stop other tests.
60 """
61
62 class TestDate(unittest.TestCase):
63 '''
64 A test case template to test ISO date formatting.
65 '''
66
67 # local time zone mock function
68 def localtime_mock(self, secs):
69 """
70 mock time.localtime so that it always returns a time_struct with
71 tm_idst=1
72 """
73 tt = self.ORIG['localtime'](secs)
74 # befor 2000 everything is dst, after 2000 no dst.
75 if tt.tm_year < 2000:
76 dst = 1
77 else:
78 dst = 0
79 tt = (tt.tm_year, tt.tm_mon, tt.tm_mday,
80 tt.tm_hour, tt.tm_min, tt.tm_sec,
81 tt.tm_wday, tt.tm_yday, dst)
82 return time.struct_time(tt)
83
84 def setUp(self):
85 self.ORIG = {}
86 self.ORIG['STDOFFSET'] = tzinfo.STDOFFSET
87 self.ORIG['DSTOFFSET'] = tzinfo.DSTOFFSET
88 self.ORIG['DSTDIFF'] = tzinfo.DSTDIFF
89 self.ORIG['localtime'] = time.localtime
90 # ovveride all saved values with fixtures.
91 # calculate LOCAL TZ offset, so that this test runs in
92 # every time zone
93 tzinfo.STDOFFSET = timedelta(seconds=36000) # assume LOC = +10:00
94 tzinfo.DSTOFFSET = timedelta(seconds=39600) # assume DST = +11:00
95 tzinfo.DSTDIFF = tzinfo.DSTOFFSET - tzinfo.STDOFFSET
96 time.localtime = self.localtime_mock
97
98 def tearDown(self):
99 # restore test fixtures
100 tzinfo.STDOFFSET = self.ORIG['STDOFFSET']
101 tzinfo.DSTOFFSET = self.ORIG['DSTOFFSET']
102 tzinfo.DSTDIFF = self.ORIG['DSTDIFF']
103 time.localtime = self.ORIG['localtime']
104
105 def test_format(self):
106 '''
107 Take date object and create ISO string from it.
108 This is the reverse test to test_parse.
109 '''
110 if expectation is None:
111 self.assertRaises(AttributeError,
112 strftime(dt, format))
113 else:
114 self.assertEqual(strftime(dt, format),
115 expectation)
116
117 return unittest.TestLoader().loadTestsFromTestCase(TestDate)
118
119
120 def test_suite():
121 '''
122 Construct a TestSuite instance for all test cases.
123 '''
124 suite = unittest.TestSuite()
125 for dt, format, expectation in TEST_CASES:
126 suite.addTest(create_testcase(dt, format, expectation))
127 return suite
128
129
130 # load_tests Protocol
131 def load_tests(loader, tests, pattern):
132 return test_suite()
133
134
135 if __name__ == '__main__':
136 unittest.main(defaultTest='test_suite')