comparison env/lib/python3.7/site-packages/isodate/tests/test_date.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
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 from datetime import date
32 from isodate import parse_date, ISO8601Error, date_isoformat
33 from isodate import DATE_CENTURY, DATE_YEAR
34 from isodate import DATE_BAS_MONTH, DATE_EXT_MONTH
35 from isodate import DATE_EXT_COMPLETE, DATE_BAS_COMPLETE
36 from isodate import DATE_BAS_ORD_COMPLETE, DATE_EXT_ORD_COMPLETE
37 from isodate import DATE_BAS_WEEK, DATE_BAS_WEEK_COMPLETE
38 from isodate import DATE_EXT_WEEK, DATE_EXT_WEEK_COMPLETE
39
40 # the following list contains tuples of ISO date strings and the expected
41 # result from the parse_date method. A result of None means an ISO8601Error
42 # is expected. The test cases are grouped into dates with 4 digit years
43 # and 6 digit years.
44 TEST_CASES = {4: [('19', date(1901, 1, 1), DATE_CENTURY),
45 ('1985', date(1985, 1, 1), DATE_YEAR),
46 ('1985-04', date(1985, 4, 1), DATE_EXT_MONTH),
47 ('198504', date(1985, 4, 1), DATE_BAS_MONTH),
48 ('1985-04-12', date(1985, 4, 12), DATE_EXT_COMPLETE),
49 ('19850412', date(1985, 4, 12), DATE_BAS_COMPLETE),
50 ('1985102', date(1985, 4, 12), DATE_BAS_ORD_COMPLETE),
51 ('1985-102', date(1985, 4, 12), DATE_EXT_ORD_COMPLETE),
52 ('1985W155', date(1985, 4, 12), DATE_BAS_WEEK_COMPLETE),
53 ('1985-W15-5', date(1985, 4, 12), DATE_EXT_WEEK_COMPLETE),
54 ('1985W15', date(1985, 4, 8), DATE_BAS_WEEK),
55 ('1985-W15', date(1985, 4, 8), DATE_EXT_WEEK),
56 ('1989-W15', date(1989, 4, 10), DATE_EXT_WEEK),
57 ('1989-W15-5', date(1989, 4, 14), DATE_EXT_WEEK_COMPLETE),
58 ('1-W1-1', None, DATE_BAS_WEEK_COMPLETE)],
59 6: [('+0019', date(1901, 1, 1), DATE_CENTURY),
60 ('+001985', date(1985, 1, 1), DATE_YEAR),
61 ('+001985-04', date(1985, 4, 1), DATE_EXT_MONTH),
62 ('+001985-04-12', date(1985, 4, 12), DATE_EXT_COMPLETE),
63 ('+0019850412', date(1985, 4, 12), DATE_BAS_COMPLETE),
64 ('+001985102', date(1985, 4, 12), DATE_BAS_ORD_COMPLETE),
65 ('+001985-102', date(1985, 4, 12), DATE_EXT_ORD_COMPLETE),
66 ('+001985W155', date(1985, 4, 12), DATE_BAS_WEEK_COMPLETE),
67 ('+001985-W15-5', date(1985, 4, 12), DATE_EXT_WEEK_COMPLETE),
68 ('+001985W15', date(1985, 4, 8), DATE_BAS_WEEK),
69 ('+001985-W15', date(1985, 4, 8), DATE_EXT_WEEK)]}
70
71
72 def create_testcase(yeardigits, datestring, expectation, format):
73 '''
74 Create a TestCase class for a specific test.
75
76 This allows having a separate TestCase for each test tuple from the
77 TEST_CASES list, so that a failed test won't stop other tests.
78 '''
79
80 class TestDate(unittest.TestCase):
81 '''
82 A test case template to parse an ISO date string into a date
83 object.
84 '''
85
86 def test_parse(self):
87 '''
88 Parse an ISO date string and compare it to the expected value.
89 '''
90 if expectation is None:
91 self.assertRaises(ISO8601Error, parse_date, datestring,
92 yeardigits)
93 else:
94 result = parse_date(datestring, yeardigits)
95 self.assertEqual(result, expectation)
96
97 def test_format(self):
98 '''
99 Take date object and create ISO string from it.
100 This is the reverse test to test_parse.
101 '''
102 if expectation is None:
103 self.assertRaises(AttributeError,
104 date_isoformat, expectation, format,
105 yeardigits)
106 else:
107 self.assertEqual(date_isoformat(expectation, format,
108 yeardigits),
109 datestring)
110
111 return unittest.TestLoader().loadTestsFromTestCase(TestDate)
112
113
114 def test_suite():
115 '''
116 Construct a TestSuite instance for all test cases.
117 '''
118 suite = unittest.TestSuite()
119 for yeardigits, tests in TEST_CASES.items():
120 for datestring, expectation, format in tests:
121 suite.addTest(create_testcase(yeardigits, datestring,
122 expectation, format))
123 return suite
124
125
126 # load_tests Protocol
127 def load_tests(loader, tests, pattern):
128 return test_suite()
129
130
131 if __name__ == '__main__':
132 unittest.main(defaultTest='test_suite')