Mercurial > repos > shellac > guppy_basecaller
comparison env/lib/python3.7/site-packages/isodate/tests/test_duration.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 isoduration module. | |
29 ''' | |
30 import unittest | |
31 import operator | |
32 from datetime import timedelta, date, datetime | |
33 | |
34 from isodate import Duration, parse_duration, ISO8601Error | |
35 from isodate import D_DEFAULT, D_WEEK, D_ALT_EXT, duration_isoformat | |
36 | |
37 # the following list contains tuples of ISO duration strings and the expected | |
38 # result from the parse_duration method. A result of None means an ISO8601Error | |
39 # is expected. | |
40 PARSE_TEST_CASES = {'P18Y9M4DT11H9M8S': (Duration(4, 8, 0, 0, 9, 11, 0, 9, 18), | |
41 D_DEFAULT, None), | |
42 'P2W': (timedelta(weeks=2), D_WEEK, None), | |
43 'P3Y6M4DT12H30M5S': (Duration(4, 5, 0, 0, 30, 12, 0, 6, 3), | |
44 D_DEFAULT, None), | |
45 'P23DT23H': (timedelta(hours=23, days=23), | |
46 D_DEFAULT, None), | |
47 'P4Y': (Duration(years=4), D_DEFAULT, None), | |
48 'P1M': (Duration(months=1), D_DEFAULT, None), | |
49 'PT1M': (timedelta(minutes=1), D_DEFAULT, None), | |
50 'P0.5Y': (Duration(years=0.5), D_DEFAULT, None), | |
51 'PT36H': (timedelta(hours=36), D_DEFAULT, 'P1DT12H'), | |
52 'P1DT12H': (timedelta(days=1, hours=12), D_DEFAULT, None), | |
53 '+P11D': (timedelta(days=11), D_DEFAULT, 'P11D'), | |
54 '-P2W': (timedelta(weeks=-2), D_WEEK, None), | |
55 '-P2.2W': (timedelta(weeks=-2.2), D_DEFAULT, | |
56 '-P15DT9H36M'), | |
57 'P1DT2H3M4S': (timedelta(days=1, hours=2, minutes=3, | |
58 seconds=4), D_DEFAULT, None), | |
59 'P1DT2H3M': (timedelta(days=1, hours=2, minutes=3), | |
60 D_DEFAULT, None), | |
61 'P1DT2H': (timedelta(days=1, hours=2), D_DEFAULT, None), | |
62 'PT2H': (timedelta(hours=2), D_DEFAULT, None), | |
63 'PT2.3H': (timedelta(hours=2.3), D_DEFAULT, 'PT2H18M'), | |
64 'PT2H3M4S': (timedelta(hours=2, minutes=3, seconds=4), | |
65 D_DEFAULT, None), | |
66 'PT3M4S': (timedelta(minutes=3, seconds=4), D_DEFAULT, | |
67 None), | |
68 'PT22S': (timedelta(seconds=22), D_DEFAULT, None), | |
69 'PT22.22S': (timedelta(seconds=22.22), 'PT%S.%fS', | |
70 'PT22.220000S'), | |
71 '-P2Y': (Duration(years=-2), D_DEFAULT, None), | |
72 '-P3Y6M4DT12H30M5S': (Duration(-4, -5, 0, 0, -30, -12, 0, | |
73 -6, -3), D_DEFAULT, None), | |
74 '-P1DT2H3M4S': (timedelta(days=-1, hours=-2, minutes=-3, | |
75 seconds=-4), D_DEFAULT, None), | |
76 # alternative format | |
77 'P0018-09-04T11:09:08': (Duration(4, 8, 0, 0, 9, 11, 0, 9, | |
78 18), D_ALT_EXT, None), | |
79 # 'PT000022.22': timedelta(seconds=22.22), | |
80 } | |
81 | |
82 # d1 d2 '+', '-', '>' | |
83 # A list of test cases to test addition and subtraction between datetime and | |
84 # Duration objects. | |
85 # each tuple contains 2 duration strings, and a result string for addition and | |
86 # one for subtraction. The last value says, if the first duration is greater | |
87 # than the second. | |
88 MATH_TEST_CASES = (('P5Y7M1DT9H45M16.72S', 'PT27M24.68S', | |
89 'P5Y7M1DT10H12M41.4S', 'P5Y7M1DT9H17M52.04S', None), | |
90 ('PT28M12.73S', 'PT56M29.92S', | |
91 'PT1H24M42.65S', '-PT28M17.19S', False), | |
92 ('P3Y7M23DT5H25M0.33S', 'PT1H1.95S', | |
93 'P3Y7M23DT6H25M2.28S', 'P3Y7M23DT4H24M58.38S', None), | |
94 ('PT1H1.95S', 'P3Y7M23DT5H25M0.33S', | |
95 'P3Y7M23DT6H25M2.28S', '-P3Y7M23DT4H24M58.38S', None), | |
96 ('P1332DT55M0.33S', 'PT1H1.95S', | |
97 'P1332DT1H55M2.28S', 'P1331DT23H54M58.38S', True), | |
98 ('PT1H1.95S', 'P1332DT55M0.33S', | |
99 'P1332DT1H55M2.28S', '-P1331DT23H54M58.38S', False)) | |
100 | |
101 | |
102 # A list of test cases to test addition and subtraction of date/datetime | |
103 # and Duration objects. They are tested against the results of an | |
104 # equal long timedelta duration. | |
105 DATE_TEST_CASES = ((date(2008, 2, 29), | |
106 timedelta(days=10, hours=12, minutes=20), | |
107 Duration(days=10, hours=12, minutes=20)), | |
108 (date(2008, 1, 31), | |
109 timedelta(days=10, hours=12, minutes=20), | |
110 Duration(days=10, hours=12, minutes=20)), | |
111 (datetime(2008, 2, 29), | |
112 timedelta(days=10, hours=12, minutes=20), | |
113 Duration(days=10, hours=12, minutes=20)), | |
114 (datetime(2008, 1, 31), | |
115 timedelta(days=10, hours=12, minutes=20), | |
116 Duration(days=10, hours=12, minutes=20)), | |
117 (datetime(2008, 4, 21), | |
118 timedelta(days=10, hours=12, minutes=20), | |
119 Duration(days=10, hours=12, minutes=20)), | |
120 (datetime(2008, 5, 5), | |
121 timedelta(days=10, hours=12, minutes=20), | |
122 Duration(days=10, hours=12, minutes=20)), | |
123 (datetime(2000, 1, 1), | |
124 timedelta(hours=-33), | |
125 Duration(hours=-33)), | |
126 (datetime(2008, 5, 5), | |
127 Duration(years=1, months=1, days=10, hours=12, | |
128 minutes=20), | |
129 Duration(months=13, days=10, hours=12, minutes=20)), | |
130 (datetime(2000, 3, 30), | |
131 Duration(years=1, months=1, days=10, hours=12, | |
132 minutes=20), | |
133 Duration(months=13, days=10, hours=12, minutes=20)), | |
134 ) | |
135 | |
136 # A list of test cases of additon of date/datetime and Duration. The results | |
137 # are compared against a given expected result. | |
138 DATE_CALC_TEST_CASES = ( | |
139 (date(2000, 2, 1), | |
140 Duration(years=1, months=1), | |
141 date(2001, 3, 1)), | |
142 (date(2000, 2, 29), | |
143 Duration(years=1, months=1), | |
144 date(2001, 3, 29)), | |
145 (date(2000, 2, 29), | |
146 Duration(years=1), | |
147 date(2001, 2, 28)), | |
148 (date(1996, 2, 29), | |
149 Duration(years=4), | |
150 date(2000, 2, 29)), | |
151 (date(2096, 2, 29), | |
152 Duration(years=4), | |
153 date(2100, 2, 28)), | |
154 (date(2000, 2, 1), | |
155 Duration(years=-1, months=-1), | |
156 date(1999, 1, 1)), | |
157 (date(2000, 2, 29), | |
158 Duration(years=-1, months=-1), | |
159 date(1999, 1, 29)), | |
160 (date(2000, 2, 1), | |
161 Duration(years=1, months=1, days=1), | |
162 date(2001, 3, 2)), | |
163 (date(2000, 2, 29), | |
164 Duration(years=1, months=1, days=1), | |
165 date(2001, 3, 30)), | |
166 (date(2000, 2, 29), | |
167 Duration(years=1, days=1), | |
168 date(2001, 3, 1)), | |
169 (date(1996, 2, 29), | |
170 Duration(years=4, days=1), | |
171 date(2000, 3, 1)), | |
172 (date(2096, 2, 29), | |
173 Duration(years=4, days=1), | |
174 date(2100, 3, 1)), | |
175 (date(2000, 2, 1), | |
176 Duration(years=-1, months=-1, days=-1), | |
177 date(1998, 12, 31)), | |
178 (date(2000, 2, 29), | |
179 Duration(years=-1, months=-1, days=-1), | |
180 date(1999, 1, 28)), | |
181 (date(2001, 4, 1), | |
182 Duration(years=-1, months=-1, days=-1), | |
183 date(2000, 2, 29)), | |
184 (date(2000, 4, 1), | |
185 Duration(years=-1, months=-1, days=-1), | |
186 date(1999, 2, 28)), | |
187 (Duration(years=1, months=2), | |
188 Duration(years=0, months=0, days=1), | |
189 Duration(years=1, months=2, days=1)), | |
190 (Duration(years=-1, months=-1, days=-1), | |
191 date(2000, 4, 1), | |
192 date(1999, 2, 28)), | |
193 (Duration(years=1, months=1, weeks=5), | |
194 date(2000, 1, 30), | |
195 date(2001, 4, 4)), | |
196 (parse_duration("P1Y1M5W"), | |
197 date(2000, 1, 30), | |
198 date(2001, 4, 4)), | |
199 (parse_duration("P0.5Y"), | |
200 date(2000, 1, 30), | |
201 None), | |
202 (Duration(years=1, months=1, hours=3), | |
203 datetime(2000, 1, 30, 12, 15, 00), | |
204 datetime(2001, 2, 28, 15, 15, 00)), | |
205 (parse_duration("P1Y1MT3H"), | |
206 datetime(2000, 1, 30, 12, 15, 00), | |
207 datetime(2001, 2, 28, 15, 15, 00)), | |
208 (Duration(years=1, months=2), | |
209 timedelta(days=1), | |
210 Duration(years=1, months=2, days=1)), | |
211 (timedelta(days=1), | |
212 Duration(years=1, months=2), | |
213 Duration(years=1, months=2, days=1)), | |
214 (datetime(2008, 1, 1, 0, 2), | |
215 Duration(months=1), | |
216 datetime(2008, 2, 1, 0, 2)), | |
217 (datetime.strptime("200802", "%Y%M"), | |
218 parse_duration("P1M"), | |
219 datetime(2008, 2, 1, 0, 2)), | |
220 (datetime(2008, 2, 1), | |
221 Duration(months=1), | |
222 datetime(2008, 3, 1)), | |
223 (datetime.strptime("200802", "%Y%m"), | |
224 parse_duration("P1M"), | |
225 datetime(2008, 3, 1)), | |
226 # (date(2000, 1, 1), | |
227 # Duration(years=1.5), | |
228 # date(2001, 6, 1)), | |
229 # (date(2000, 1, 1), | |
230 # Duration(years=1, months=1.5), | |
231 # date(2001, 2, 14)), | |
232 ) | |
233 | |
234 # A list of test cases of multiplications of durations | |
235 # are compared against a given expected result. | |
236 DATE_MUL_TEST_CASES = ( | |
237 (Duration(years=1, months=1), | |
238 3, | |
239 Duration(years=3, months=3)), | |
240 (Duration(years=1, months=1), | |
241 -3, | |
242 Duration(years=-3, months=-3)), | |
243 (3, | |
244 Duration(years=1, months=1), | |
245 Duration(years=3, months=3)), | |
246 (-3, | |
247 Duration(years=1, months=1), | |
248 Duration(years=-3, months=-3)), | |
249 (5, | |
250 Duration(years=2, minutes=40), | |
251 Duration(years=10, hours=3, minutes=20)), | |
252 (-5, | |
253 Duration(years=2, minutes=40), | |
254 Duration(years=-10, hours=-3, minutes=-20)), | |
255 (7, | |
256 Duration(years=1, months=2, weeks=40), | |
257 Duration(years=8, months=2, weeks=280))) | |
258 | |
259 | |
260 class DurationTest(unittest.TestCase): | |
261 ''' | |
262 This class tests various other aspects of the isoduration module, | |
263 which are not covered with the test cases listed above. | |
264 ''' | |
265 | |
266 def test_associative(self): | |
267 ''' | |
268 Adding 2 durations to a date is not associative. | |
269 ''' | |
270 days1 = Duration(days=1) | |
271 months1 = Duration(months=1) | |
272 start = date(2000, 3, 30) | |
273 res1 = start + days1 + months1 | |
274 res2 = start + months1 + days1 | |
275 self.assertNotEqual(res1, res2) | |
276 | |
277 def test_typeerror(self): | |
278 ''' | |
279 Test if TypError is raised with certain parameters. | |
280 ''' | |
281 self.assertRaises(TypeError, parse_duration, date(2000, 1, 1)) | |
282 self.assertRaises(TypeError, operator.sub, Duration(years=1), | |
283 date(2000, 1, 1)) | |
284 self.assertRaises(TypeError, operator.sub, 'raise exc', | |
285 Duration(years=1)) | |
286 self.assertRaises(TypeError, operator.add, | |
287 Duration(years=1, months=1, weeks=5), | |
288 'raise exception') | |
289 self.assertRaises(TypeError, operator.add, 'raise exception', | |
290 Duration(years=1, months=1, weeks=5)) | |
291 self.assertRaises(TypeError, operator.mul, | |
292 Duration(years=1, months=1, weeks=5), | |
293 'raise exception') | |
294 self.assertRaises(TypeError, operator.mul, 'raise exception', | |
295 Duration(years=1, months=1, weeks=5)) | |
296 self.assertRaises(TypeError, operator.mul, | |
297 Duration(years=1, months=1, weeks=5), | |
298 3.14) | |
299 self.assertRaises(TypeError, operator.mul, 3.14, | |
300 Duration(years=1, months=1, weeks=5)) | |
301 | |
302 def test_parseerror(self): | |
303 ''' | |
304 Test for unparseable duration string. | |
305 ''' | |
306 self.assertRaises(ISO8601Error, parse_duration, 'T10:10:10') | |
307 | |
308 def test_repr(self): | |
309 ''' | |
310 Test __repr__ and __str__ for Duration objects. | |
311 ''' | |
312 dur = Duration(10, 10, years=10, months=10) | |
313 self.assertEqual('10 years, 10 months, 10 days, 0:00:10', str(dur)) | |
314 self.assertEqual('isodate.duration.Duration(10, 10, 0,' | |
315 ' years=10, months=10)', repr(dur)) | |
316 dur = Duration(months=0) | |
317 self.assertEqual('0:00:00', str(dur)) | |
318 dur = Duration(months=1) | |
319 self.assertEqual('1 month, 0:00:00', str(dur)) | |
320 | |
321 def test_hash(self): | |
322 ''' | |
323 Test __hash__ for Duration objects. | |
324 ''' | |
325 dur1 = Duration(10, 10, years=10, months=10) | |
326 dur2 = Duration(9, 9, years=9, months=9) | |
327 dur3 = Duration(10, 10, years=10, months=10) | |
328 self.assertNotEqual(hash(dur1), hash(dur2)) | |
329 self.assertNotEqual(id(dur1), id(dur2)) | |
330 self.assertEqual(hash(dur1), hash(dur3)) | |
331 self.assertNotEqual(id(dur1), id(dur3)) | |
332 durSet = set() | |
333 durSet.add(dur1) | |
334 durSet.add(dur2) | |
335 durSet.add(dur3) | |
336 self.assertEqual(len(durSet), 2) | |
337 | |
338 def test_neg(self): | |
339 ''' | |
340 Test __neg__ for Duration objects. | |
341 ''' | |
342 self.assertEqual(-Duration(0), Duration(0)) | |
343 self.assertEqual(-Duration(years=1, months=1), | |
344 Duration(years=-1, months=-1)) | |
345 self.assertEqual(-Duration(years=1, months=1), Duration(months=-13)) | |
346 self.assertNotEqual(-Duration(years=1), timedelta(days=-365)) | |
347 self.assertNotEqual(-timedelta(days=365), Duration(years=-1)) | |
348 # FIXME: this test fails in python 3... it seems like python3 | |
349 # treats a == b the same b == a | |
350 # self.assertNotEqual(-timedelta(days=10), -Duration(days=10)) | |
351 | |
352 def test_format(self): | |
353 ''' | |
354 Test various other strftime combinations. | |
355 ''' | |
356 self.assertEqual(duration_isoformat(Duration(0)), 'P0D') | |
357 self.assertEqual(duration_isoformat(-Duration(0)), 'P0D') | |
358 self.assertEqual(duration_isoformat(Duration(seconds=10)), 'PT10S') | |
359 self.assertEqual(duration_isoformat(Duration(years=-1, months=-1)), | |
360 '-P1Y1M') | |
361 self.assertEqual(duration_isoformat(-Duration(years=1, months=1)), | |
362 '-P1Y1M') | |
363 self.assertEqual(duration_isoformat(-Duration(years=-1, months=-1)), | |
364 'P1Y1M') | |
365 self.assertEqual(duration_isoformat(-Duration(years=-1, months=-1)), | |
366 'P1Y1M') | |
367 dur = Duration(years=3, months=7, days=23, hours=5, minutes=25, | |
368 milliseconds=330) | |
369 self.assertEqual(duration_isoformat(dur), 'P3Y7M23DT5H25M0.33S') | |
370 self.assertEqual(duration_isoformat(-dur), '-P3Y7M23DT5H25M0.33S') | |
371 | |
372 def test_equal(self): | |
373 ''' | |
374 Test __eq__ and __ne__ methods. | |
375 ''' | |
376 self.assertEqual(Duration(years=1, months=1), | |
377 Duration(years=1, months=1)) | |
378 self.assertEqual(Duration(years=1, months=1), Duration(months=13)) | |
379 self.assertNotEqual(Duration(years=1, months=2), | |
380 Duration(years=1, months=1)) | |
381 self.assertNotEqual(Duration(years=1, months=1), Duration(months=14)) | |
382 self.assertNotEqual(Duration(years=1), timedelta(days=365)) | |
383 self.assertFalse(Duration(years=1, months=1) != | |
384 Duration(years=1, months=1)) | |
385 self.assertFalse(Duration(years=1, months=1) != Duration(months=13)) | |
386 self.assertTrue(Duration(years=1, months=2) != | |
387 Duration(years=1, months=1)) | |
388 self.assertTrue(Duration(years=1, months=1) != Duration(months=14)) | |
389 self.assertTrue(Duration(years=1) != timedelta(days=365)) | |
390 self.assertEqual(Duration(days=1), timedelta(days=1)) | |
391 # FIXME: this test fails in python 3... it seems like python3 | |
392 # treats a != b the same b != a | |
393 # self.assertNotEqual(timedelta(days=1), Duration(days=1)) | |
394 | |
395 def test_totimedelta(self): | |
396 ''' | |
397 Test conversion form Duration to timedelta. | |
398 ''' | |
399 dur = Duration(years=1, months=2, days=10) | |
400 self.assertEqual(dur.totimedelta(datetime(1998, 2, 25)), | |
401 timedelta(434)) | |
402 # leap year has one day more in february | |
403 self.assertEqual(dur.totimedelta(datetime(2000, 2, 25)), | |
404 timedelta(435)) | |
405 dur = Duration(months=2) | |
406 # march is longer than february, but april is shorter than | |
407 # march (cause only one day difference compared to 2) | |
408 self.assertEqual(dur.totimedelta(datetime(2000, 2, 25)), timedelta(60)) | |
409 self.assertEqual(dur.totimedelta(datetime(2001, 2, 25)), timedelta(59)) | |
410 self.assertEqual(dur.totimedelta(datetime(2001, 3, 25)), timedelta(61)) | |
411 | |
412 | |
413 def create_parsetestcase(durationstring, expectation, format, altstr): | |
414 """ | |
415 Create a TestCase class for a specific test. | |
416 | |
417 This allows having a separate TestCase for each test tuple from the | |
418 PARSE_TEST_CASES list, so that a failed test won't stop other tests. | |
419 """ | |
420 | |
421 class TestParseDuration(unittest.TestCase): | |
422 ''' | |
423 A test case template to parse an ISO duration string into a | |
424 timedelta or Duration object. | |
425 ''' | |
426 | |
427 def test_parse(self): | |
428 ''' | |
429 Parse an ISO duration string and compare it to the expected value. | |
430 ''' | |
431 result = parse_duration(durationstring) | |
432 self.assertEqual(result, expectation) | |
433 | |
434 def test_format(self): | |
435 ''' | |
436 Take duration/timedelta object and create ISO string from it. | |
437 This is the reverse test to test_parse. | |
438 ''' | |
439 if altstr: | |
440 self.assertEqual(duration_isoformat(expectation, format), | |
441 altstr) | |
442 else: | |
443 # if durationstring == '-P2W': | |
444 # import pdb; pdb.set_trace() | |
445 self.assertEqual(duration_isoformat(expectation, format), | |
446 durationstring) | |
447 | |
448 return unittest.TestLoader().loadTestsFromTestCase(TestParseDuration) | |
449 | |
450 | |
451 def create_mathtestcase(dur1, dur2, resadd, ressub, resge): | |
452 """ | |
453 Create a TestCase class for a specific test. | |
454 | |
455 This allows having a separate TestCase for each test tuple from the | |
456 MATH_TEST_CASES list, so that a failed test won't stop other tests. | |
457 """ | |
458 | |
459 dur1 = parse_duration(dur1) | |
460 dur2 = parse_duration(dur2) | |
461 resadd = parse_duration(resadd) | |
462 ressub = parse_duration(ressub) | |
463 | |
464 class TestMathDuration(unittest.TestCase): | |
465 ''' | |
466 A test case template test addition, subtraction and > | |
467 operators for Duration objects. | |
468 ''' | |
469 | |
470 def test_add(self): | |
471 ''' | |
472 Test operator + (__add__, __radd__) | |
473 ''' | |
474 self.assertEqual(dur1 + dur2, resadd) | |
475 | |
476 def test_sub(self): | |
477 ''' | |
478 Test operator - (__sub__, __rsub__) | |
479 ''' | |
480 self.assertEqual(dur1 - dur2, ressub) | |
481 | |
482 def test_ge(self): | |
483 ''' | |
484 Test operator > and < | |
485 ''' | |
486 def dogetest(): | |
487 ''' Test greater than.''' | |
488 return dur1 > dur2 | |
489 | |
490 def doletest(): | |
491 ''' Test less than.''' | |
492 return dur1 < dur2 | |
493 if resge is None: | |
494 self.assertRaises(TypeError, dogetest) | |
495 self.assertRaises(TypeError, doletest) | |
496 else: | |
497 self.assertEqual(dogetest(), resge) | |
498 self.assertEqual(doletest(), not resge) | |
499 | |
500 return unittest.TestLoader().loadTestsFromTestCase(TestMathDuration) | |
501 | |
502 | |
503 def create_datetestcase(start, tdelta, duration): | |
504 """ | |
505 Create a TestCase class for a specific test. | |
506 | |
507 This allows having a separate TestCase for each test tuple from the | |
508 DATE_TEST_CASES list, so that a failed test won't stop other tests. | |
509 """ | |
510 | |
511 class TestDateCalc(unittest.TestCase): | |
512 ''' | |
513 A test case template test addition, subtraction | |
514 operators for Duration objects. | |
515 ''' | |
516 | |
517 def test_add(self): | |
518 ''' | |
519 Test operator +. | |
520 ''' | |
521 self.assertEqual(start + tdelta, start + duration) | |
522 | |
523 def test_sub(self): | |
524 ''' | |
525 Test operator -. | |
526 ''' | |
527 self.assertEqual(start - tdelta, start - duration) | |
528 | |
529 return unittest.TestLoader().loadTestsFromTestCase(TestDateCalc) | |
530 | |
531 | |
532 def create_datecalctestcase(start, duration, expectation): | |
533 """ | |
534 Create a TestCase class for a specific test. | |
535 | |
536 This allows having a separate TestCase for each test tuple from the | |
537 DATE_CALC_TEST_CASES list, so that a failed test won't stop other tests. | |
538 """ | |
539 | |
540 class TestDateCalc(unittest.TestCase): | |
541 ''' | |
542 A test case template test addition operators for Duration objects. | |
543 ''' | |
544 | |
545 def test_calc(self): | |
546 ''' | |
547 Test operator +. | |
548 ''' | |
549 if expectation is None: | |
550 self.assertRaises(ValueError, operator.add, start, duration) | |
551 else: | |
552 self.assertEqual(start + duration, expectation) | |
553 | |
554 return unittest.TestLoader().loadTestsFromTestCase(TestDateCalc) | |
555 | |
556 | |
557 def create_datemultestcase(operand1, operand2, expectation): | |
558 """ | |
559 Create a TestCase class for a specific test. | |
560 | |
561 This allows having a separate TestCase for each test tuple from the | |
562 DATE_CALC_TEST_CASES list, so that a failed test won't stop other tests. | |
563 """ | |
564 | |
565 class TestDateMul(unittest.TestCase): | |
566 ''' | |
567 A test case template test addition operators for Duration objects. | |
568 ''' | |
569 | |
570 def test_mul(self): | |
571 ''' | |
572 Test operator *. | |
573 ''' | |
574 self.assertEqual(operand1 * operand2, expectation) | |
575 | |
576 return unittest.TestLoader().loadTestsFromTestCase(TestDateMul) | |
577 | |
578 | |
579 def test_suite(): | |
580 ''' | |
581 Return a test suite containing all test defined above. | |
582 ''' | |
583 suite = unittest.TestSuite() | |
584 for durationstring, (expectation, format, | |
585 altstr) in PARSE_TEST_CASES.items(): | |
586 suite.addTest(create_parsetestcase(durationstring, expectation, | |
587 format, altstr)) | |
588 for testdata in MATH_TEST_CASES: | |
589 suite.addTest(create_mathtestcase(*testdata)) | |
590 for testdata in DATE_TEST_CASES: | |
591 suite.addTest(create_datetestcase(*testdata)) | |
592 for testdata in DATE_CALC_TEST_CASES: | |
593 suite.addTest(create_datecalctestcase(*testdata)) | |
594 for testdata in DATE_MUL_TEST_CASES: | |
595 suite.addTest(create_datemultestcase(*testdata)) | |
596 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(DurationTest)) | |
597 return suite | |
598 | |
599 | |
600 # load_tests Protocol | |
601 def load_tests(loader, tests, pattern): | |
602 return test_suite() | |
603 | |
604 | |
605 if __name__ == '__main__': | |
606 unittest.main(defaultTest='test_suite') |