Code

Optimize mailgw and cgi tests by creating an empty instance before the tests
[roundup.git] / test / test_dates.py
1 #
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
10 # POSSIBILITY OF SUCH DAMAGE.
11 #
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE.  THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17
18 # $Id: test_dates.py,v 1.24 2003-04-22 20:53:54 kedder Exp $ 
20 import unittest, time
22 from roundup.date import Date, Interval, Range, fixTimeOverflow
24 class DateTestCase(unittest.TestCase):
25     def testDateInterval(self):
26         ae = self.assertEqual
27         date = Date("2000-06-26.00:34:02 + 2d")
28         ae(str(date), '2000-06-28.00:34:02')
29         date = Date("2000-02-27 + 2d")
30         ae(str(date), '2000-02-29.00:00:00')
31         date = Date("2001-02-27 + 2d")
32         ae(str(date), '2001-03-01.00:00:00')
34     def testDate(self):
35         ae = self.assertEqual
36         date = Date("2000-04-17")
37         ae(str(date), '2000-04-17.00:00:00')
38         date = Date("2000/04/17")
39         ae(str(date), '2000-04-17.00:00:00')
40         date = Date("2000-4-7")
41         ae(str(date), '2000-04-07.00:00:00')
42         date = Date("2000-4-17")
43         ae(str(date), '2000-04-17.00:00:00')
44         date = Date("01-25")
45         y, m, d, x, x, x, x, x, x = time.gmtime(time.time())
46         ae(str(date), '%s-01-25.00:00:00'%y)
47         date = Date("2000-04-17.03:45")
48         ae(str(date), '2000-04-17.03:45:00')
49         date = Date("2000/04/17.03:45")
50         ae(str(date), '2000-04-17.03:45:00')
51         date = Date("08-13.22:13")
52         ae(str(date), '%s-08-13.22:13:00'%y)
53         date = Date("11-07.09:32:43")
54         ae(str(date), '%s-11-07.09:32:43'%y)
55         date = Date("14:25")
56         ae(str(date), '%s-%02d-%02d.14:25:00'%(y, m, d))
57         date = Date("8:47:11")
58         ae(str(date), '%s-%02d-%02d.08:47:11'%(y, m, d))
59         ae(str(Date('2003')), '2003-01-01.00:00:00')
60         ae(str(Date('2004-06')), '2004-06-01.00:00:00')
62     def testDateError(self):
63         self.assertRaises(ValueError, Date, "12")
65     def testOffset(self):
66         ae = self.assertEqual
67         date = Date("2000-04-17", -5)
68         ae(str(date), '2000-04-17.05:00:00')
69         date = Date("01-25", -5)
70         y, m, d, x, x, x, x, x, x = time.gmtime(time.time())
71         ae(str(date), '%s-01-25.05:00:00'%y)
72         date = Date("2000-04-17.03:45", -5)
73         ae(str(date), '2000-04-17.08:45:00')
74         date = Date("08-13.22:13", -5)
75         ae(str(date), '%s-08-14.03:13:00'%y)
76         date = Date("11-07.09:32:43", -5)
77         ae(str(date), '%s-11-07.14:32:43'%y)
78         date = Date("14:25", -5)
79         ae(str(date), '%s-%02d-%02d.19:25:00'%(y, m, d))
80         date = Date("8:47:11", -5)
81         ae(str(date), '%s-%02d-%02d.13:47:11'%(y, m, d))
83     def testOffsetRandom(self):
84         ae = self.assertEqual
85         # XXX unsure of the usefulness of these, they're pretty random
86         date = Date('2000-01-01') + Interval('- 2y 2m')
87         ae(str(date), '1997-11-01.00:00:00')
88         date = Date('2000-01-01 - 2y 2m')
89         ae(str(date), '1997-11-01.00:00:00')
90         date = Date('2000-01-01') + Interval('2m')
91         ae(str(date), '2000-03-01.00:00:00')
92         date = Date('2000-01-01 + 2m')
93         ae(str(date), '2000-03-01.00:00:00')
95         date = Date('2000-01-01') + Interval('60d')
96         ae(str(date), '2000-03-01.00:00:00')
97         date = Date('2001-01-01') + Interval('60d')
98         ae(str(date), '2001-03-02.00:00:00')
100     def testOffsetAdd(self):
101         ae = self.assertEqual
102         date = Date('2000-02-28.23:59:59') + Interval('00:00:01')
103         ae(str(date), '2000-02-29.00:00:00')
104         date = Date('2001-02-28.23:59:59') + Interval('00:00:01')
105         ae(str(date), '2001-03-01.00:00:00')
107         date = Date('2000-02-28.23:58:59') + Interval('00:01:01')
108         ae(str(date), '2000-02-29.00:00:00')
109         date = Date('2001-02-28.23:58:59') + Interval('00:01:01')
110         ae(str(date), '2001-03-01.00:00:00')
112         date = Date('2000-02-28.22:58:59') + Interval('01:01:01')
113         ae(str(date), '2000-02-29.00:00:00')
114         date = Date('2001-02-28.22:58:59') + Interval('01:01:01')
115         ae(str(date), '2001-03-01.00:00:00')
117         date = Date('2000-02-28.22:58:59') + Interval('00:00:3661')
118         ae(str(date), '2000-02-29.00:00:00')
119         date = Date('2001-02-28.22:58:59') + Interval('00:00:3661')
120         ae(str(date), '2001-03-01.00:00:00')
122     def testOffsetSub(self):
123         ae = self.assertEqual
124         date = Date('2000-01-01') - Interval('- 2y 2m')
125         ae(str(date), '2002-03-01.00:00:00')
126         date = Date('2000-01-01') - Interval('2m')
127         ae(str(date), '1999-11-01.00:00:00')
129         date = Date('2000-03-01') - Interval('60d')
130         ae(str(date), '2000-01-01.00:00:00')
131         date = Date('2001-03-02') - Interval('60d')
132         ae(str(date), '2001-01-01.00:00:00')
134         date = Date('2000-02-29.00:00:00') - Interval('00:00:01')
135         ae(str(date), '2000-02-28.23:59:59')
136         date = Date('2001-03-01.00:00:00') - Interval('00:00:01')
137         ae(str(date), '2001-02-28.23:59:59')
139         date = Date('2000-02-29.00:00:00') - Interval('00:01:01')
140         ae(str(date), '2000-02-28.23:58:59')
141         date = Date('2001-03-01.00:00:00') - Interval('00:01:01')
142         ae(str(date), '2001-02-28.23:58:59')
144         date = Date('2000-02-29.00:00:00') - Interval('01:01:01')
145         ae(str(date), '2000-02-28.22:58:59')
146         date = Date('2001-03-01.00:00:00') - Interval('01:01:01')
147         ae(str(date), '2001-02-28.22:58:59')
149         date = Date('2000-02-29.00:00:00') - Interval('00:00:3661')
150         ae(str(date), '2000-02-28.22:58:59')
151         date = Date('2001-03-01.00:00:00') - Interval('00:00:3661')
152         ae(str(date), '2001-02-28.22:58:59')
154     def testDateLocal(self):
155         ae = self.assertEqual
156         date = Date("02:42:20")
157         date = date.local(10)
158         y, m, d, x, x, x, x, x, x = time.gmtime(time.time())
159         ae(str(date), '%s-%02d-%02d.12:42:20'%(y, m, d))
161     def testIntervalInit(self):
162         ae = self.assertEqual
163         ae(str(Interval('3y')), '+ 3y')
164         ae(str(Interval('2 y 1 m')), '+ 2y 1m')
165         ae(str(Interval('1m 25d')), '+ 1m 25d')
166         ae(str(Interval('-2w 3 d ')), '- 17d')
167         ae(str(Interval(' - 1 d 2:50 ')), '- 1d 2:50')
168         ae(str(Interval(' 14:00 ')), '+ 14:00')
169         ae(str(Interval(' 0:04:33 ')), '+ 0:04:33')
171     def testIntervalInitDate(self):
172         ae = self.assertEqual
173         now = Date('.')
174         now.hour = now.minute = now.second = 0
175         then = now + Interval('2d')
176         ae(str(Interval(str(then))), '+ 2d')
177         then = now - Interval('2d')
178         ae(str(Interval(str(then))), '- 2d')
180     def testIntervalAdd(self):
181         ae = self.assertEqual
182         ae(str(Interval('1y') + Interval('1y')), '+ 2y')
183         ae(str(Interval('1y') + Interval('1m')), '+ 1y 1m')
184         ae(str(Interval('1y') + Interval('2:40')), '+ 1y 2:40')
185         ae(str(Interval('1y') + Interval('- 1y')), '')
186         ae(str(Interval('- 1y') + Interval('1y')), '')
187         ae(str(Interval('- 1y') + Interval('- 1y')), '- 2y')
188         ae(str(Interval('1y') + Interval('- 1m')), '+ 11m')
189         ae(str(Interval('1:00') + Interval('1:00')), '+ 2:00')
190         ae(str(Interval('0:50') + Interval('0:50')), '+ 1:40')
191         ae(str(Interval('1:50') + Interval('- 1:50')), '')
192         ae(str(Interval('- 1:50') + Interval('1:50')), '')
193         ae(str(Interval('- 1:50') + Interval('- 1:50')), '- 3:40')
194         ae(str(Interval('1:59:59') + Interval('00:00:01')), '+ 2:00')
195         ae(str(Interval('2:00') + Interval('- 00:00:01')), '+ 1:59:59')
197     def testIntervalSub(self):
198         ae = self.assertEqual
199         ae(str(Interval('1y') - Interval('- 1y')), '+ 2y')
200         ae(str(Interval('1y') - Interval('- 1m')), '+ 1y 1m')
201         ae(str(Interval('1y') - Interval('- 2:40')), '+ 1y 2:40')
202         ae(str(Interval('1y') - Interval('1y')), '')
203         ae(str(Interval('1y') - Interval('1m')), '+ 11m')
204         ae(str(Interval('1:00') - Interval('- 1:00')), '+ 2:00')
205         ae(str(Interval('0:50') - Interval('- 0:50')), '+ 1:40')
206         ae(str(Interval('1:50') - Interval('1:50')), '')
207         ae(str(Interval('1:59:59') - Interval('- 00:00:01')), '+ 2:00')
208         ae(str(Interval('2:00') - Interval('00:00:01')), '+ 1:59:59')
210     def testOverflow(self):
211         ae = self.assertEqual
212         ae(fixTimeOverflow((1,0,0,0, 0, 0, 60)), (1,0,0,0, 0, 1, 0))
213         ae(fixTimeOverflow((1,0,0,0, 0, 0, 100)), (1,0,0,0, 0, 1, 40))
214         ae(fixTimeOverflow((1,0,0,0, 0, 0, 60*60)), (1,0,0,0, 1, 0, 0))
215         ae(fixTimeOverflow((1,0,0,0, 0, 0, 24*60*60)), (1,0,0,1, 0, 0, 0))
216         ae(fixTimeOverflow((1,0,0,0, 0, 0, -1)), (-1,0,0,0, 0, 0, 1))
217         ae(fixTimeOverflow((1,0,0,0, 0, 0, -100)), (-1,0,0,0, 0, 1, 40))
218         ae(fixTimeOverflow((1,0,0,0, 0, 0, -60*60)), (-1,0,0,0, 1, 0, 0))
219         ae(fixTimeOverflow((1,0,0,0, 0, 0, -24*60*60)), (-1,0,0,1, 0, 0, 0))
220         ae(fixTimeOverflow((-1,0,0,0, 0, 0, 1)), (-1,0,0,0, 0, 0, 1))
221         ae(fixTimeOverflow((-1,0,0,0, 0, 0, 100)), (-1,0,0,0, 0, 1, 40))
222         ae(fixTimeOverflow((-1,0,0,0, 0, 0, 60*60)), (-1,0,0,0, 1, 0, 0))
223         ae(fixTimeOverflow((-1,0,0,0, 0, 0, 24*60*60)), (-1,0,0,1, 0, 0, 0))
225     def testDivision(self):
226         ae = self.assertEqual
227         ae(str(Interval('1y')/2), '+ 6m')
228         ae(str(Interval('1:00')/2), '+ 0:30')
229         ae(str(Interval('00:01')/2), '+ 0:00:30')
231     def testSorting(self):
232         ae = self.assertEqual
233         i1 = Interval('1y')
234         i2 = Interval('1d')
235         l = [i1, i2]; l.sort()
236         ae(l, [i2, i1])
237         l = [i2, i1]; l.sort()
238         ae(l, [i2, i1])
239         i1 = Interval('- 2d')
240         i2 = Interval('1d')
241         l = [i1, i2]; l.sort()
242         ae(l, [i1, i2])
244         i1 = Interval("1:20")
245         i2 = Interval("2d")
246         i3 = Interval("3:30")
247         l = [i1, i2, i3]; l.sort()
248         ae(l, [i1, i3, i2])
250     def testGranularity(self):
251         ae = self.assertEqual
252         ae(str(Date('2003-2-12', add_granularity=1)), '2003-02-12.23:59:59')
253         ae(str(Date('2003-1-1.23:00', add_granularity=1)), '2003-01-01.23:00:59')
254         ae(str(Date('2003', add_granularity=1)), '2003-12-31.23:59:59')
255         ae(str(Date('2003-5', add_granularity=1)), '2003-05-31.23:59:59')
256         ae(str(Interval('+1w', add_granularity=1)), '+ 14d')
257         ae(str(Interval('-2m 3w', add_granularity=1)), '- 2m 14d')
259 def suite():
260    return unittest.makeSuite(DateTestCase, 'test')
263 # vim: set filetype=python ts=4 sw=4 et si