1
2 """
3 Unit tests for parsedatetime
4
5 The tests can be run as a C{suite} by running::
6
7 python run_tests.py parsedatetime
8
9 Requires Python 3.0 or later
10 """
11
12 __author__ = 'Mike Taylor (bear@code-bear.com)'
13 __copyright__ = 'Copyright (c) 2004 Mike Taylor'
14 __license__ = 'Apache v2.0'
15 __version__ = '1.0.0'
16 __contributors__ = [ 'Darshana Chhajed',
17 'Michael Lim (lim.ck.michael@gmail.com)',
18 'Bernd Zeimetz (bzed@debian.org)',
19 ]
20 import logging
21
22 log = logging.getLogger('parsedatetime')
23 echoHandler = logging.StreamHandler()
24 echoFormatter = logging.Formatter('%(levelname)-8s %(message)s')
25 log.addHandler(echoHandler)
26
27
28
30 """
31 Fail a little less cryptically that unittest.assertTrue when comparing a
32 result against a target value. Shows the result and the target in the
33 failure message.
34 """
35 def decoratedComparator(self, result, check, **kwargs):
36 equal = comparator(self, result, check, **kwargs)
37 failureMessage = 'Result does not match target value\n\n\tResult:\n\t%s\n\n\tExpected:\n\t%s'
38
39 if not equal:
40 self.fail(failureMessage % (result, check))
41
42 return decoratedComparator
43
45 """
46 Ensures that flags are an exact match and time tuples a close match when
47 given data in the format ((timetuple), flag)
48 """
49 return _compareTimeTuples(result[0], check[0], dateOnly) and _compareFlags(result[1], check[1])
50
52 """
53 Ensures that flags are an exact match when given data in the format
54 ((timetuple), flag)
55 """
56 return _compareFlags(result[1], check[1])
57
59 """
60 Ensures that flags are an exact match and time tuples a close match when
61 given data in the format ((timetuple), (timetuple), flag)
62 """
63 return _compareTimeTuples(result[0], check[0], dateOnly) and _compareTimeTuples(result[1], check[1], dateOnly) and _compareFlags(result[2], check[2])
64
66 """
67 Ignores minutes and seconds as running the test could cross a minute
68 boundary. Technically the year, month, day, hour, minute, and second could
69 all change if the test is run on New Year's Eve, but we won't worry about
70 less than per-hour granularity.
71 """
72 t_yr, t_mth, t_dy, t_hr, t_min, _, _, _, _ = target
73 v_yr, v_mth, v_dy, v_hr, v_min, _, _, _, _ = value
74
75 if dateOnly:
76 return ((t_yr == v_yr) and (t_mth == v_mth) and (t_dy == v_dy))
77 else:
78 return ((t_yr == v_yr) and (t_mth == v_mth) and (t_dy == v_dy) and
79 (t_hr == v_hr) and (t_min == v_min))
80
82 return (result == check)
83