Package parsedatetime :: Package tests :: Module TestErrors
[hide private]
[frames] | no frames]

Source Code for Module parsedatetime.tests.TestErrors

 1   
 2  """ 
 3  Test parsing of units 
 4  """ 
 5   
 6  import unittest, time, datetime 
 7  import parsedatetime as pdt 
 8   
 9   
10    # a special compare function is used to allow us to ignore the seconds as 
11    # the running of the test could cross a minute boundary 
12 -def _compareResults(result, check):
13 target, t_flag = result 14 value, v_flag = check 15 16 t_yr, t_mth, t_dy, t_hr, t_min, _, _, _, _ = target 17 v_yr, v_mth, v_dy, v_hr, v_min, _, _, _, _ = value 18 19 return ((t_yr == v_yr) and (t_mth == v_mth) and (t_dy == v_dy) and 20 (t_hr == v_hr) and (t_min == v_min)) and (t_flag == v_flag)
21 22
23 -def _compareResultsErrorFlag(result, check):
24 target, t_flag = result 25 value, v_flag = check 26 27 t_yr, t_mth, t_dy, _, _, _, _, _, _ = target 28 v_yr, v_mth, v_dy, _, _, _, _, _, _ = value 29 30 return (t_flag == v_flag)
31 32
33 -class test(unittest.TestCase):
34
35 - def setUp(self):
36 self.cal = pdt.Calendar() 37 self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime()
38
39 - def testErrors(self):
40 s = datetime.datetime.now() 41 start = s.timetuple() 42 43 # These tests all return current date/time as they are out of range 44 self.assertTrue(_compareResults(self.cal.parse('01/0', start), (start, 0))) 45 self.assertTrue(_compareResults(self.cal.parse('08/35', start), (start, 0))) 46 self.assertTrue(_compareResults(self.cal.parse('18/35', start), (start, 0))) 47 self.assertTrue(_compareResults(self.cal.parse('1799', start), (start, 0))) 48 self.assertTrue(_compareResults(self.cal.parse('781', start), (start, 0))) 49 self.assertTrue(_compareResults(self.cal.parse('2702', start), (start, 0))) 50 self.assertTrue(_compareResults(self.cal.parse('78', start), (start, 0))) 51 self.assertTrue(_compareResults(self.cal.parse('11', start), (start, 0))) 52 self.assertTrue(_compareResults(self.cal.parse('1', start), (start, 0))) 53 self.assertTrue(_compareResults(self.cal.parse('174565', start), (start, 0))) 54 self.assertTrue(_compareResults(self.cal.parse('177505', start), (start, 0))) 55 # ensure short month names do not cause false positives within a word - jun (june) 56 self.assertTrue(_compareResults(self.cal.parse('injunction', start), (start, 0))) 57 # ensure short month names do not cause false positives at the start of a word - jul (juuly) 58 self.assertTrue(_compareResults(self.cal.parse('julius', start), (start, 0))) 59 # ensure short month names do not cause false positives at the end of a word - mar (march) 60 self.assertTrue(_compareResults(self.cal.parse('lamar', start), (start, 0))) 61 # ensure short weekday names do not cause false positives within a word - mon (monday) 62 self.assertTrue(_compareResults(self.cal.parse('demonize', start), (start, 0))) 63 # ensure short weekday names do not cause false positives at the start of a word - mon (monday) 64 self.assertTrue(_compareResults(self.cal.parse('money', start), (start, 0))) 65 # ensure short weekday names do not cause false positives at the end of a word - th (thursday) 66 self.assertTrue(_compareResults(self.cal.parse('month', start), (start, 0))) 67 68 # This test actually parses into *something* for some locales, so need to check the error flag 69 self.assertTrue(_compareResultsErrorFlag(self.cal.parse('30/030/01/071/07', start), (start, 1)))
70 71 72 if __name__ == "__main__": 73 unittest.main() 74