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

Source Code for Module parsedatetime.tests.TestSimpleOffsetsHours

 1  """ 
 2  Test parsing of 'simple' offsets 
 3  """ 
 4   
 5  import unittest, time, datetime 
 6  import parsedatetime as pdt 
 7   
 8   
 9    # a special compare function is used to allow us to ignore the seconds as 
10    # the running of the test could cross a minute boundary 
11 -def _compareResults(result, check):
12 target, t_flag = result 13 value, v_flag = check 14 15 t_yr, t_mth, t_dy, t_hr, t_min, _, _, _, _ = target 16 v_yr, v_mth, v_dy, v_hr, v_min, _, _, _, _ = value 17 18 return ((t_yr == v_yr) and (t_mth == v_mth) and (t_dy == v_dy) and 19 (t_hr == v_hr) and (t_min == v_min)) and (t_flag == v_flag)
20 21
22 -class test(unittest.TestCase):
23
24 - def setUp(self):
25 self.cal = pdt.Calendar() 26 self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime()
27
28 - def testHoursFromNow(self):
29 s = datetime.datetime.now() 30 t = s + datetime.timedelta(hours=5) 31 32 start = s.timetuple() 33 target = t.timetuple() 34 35 self.assertTrue(_compareResults(self.cal.parse('5 hours from now', start), (target, 2))) 36 self.assertTrue(_compareResults(self.cal.parse('5 hour from now', start), (target, 2))) 37 self.assertTrue(_compareResults(self.cal.parse('5 hr from now', start), (target, 2))) 38 self.assertTrue(_compareResults(self.cal.parse('in 5 hours', start), (target, 2))) 39 self.assertTrue(_compareResults(self.cal.parse('in 5 hour', start), (target, 2))) 40 self.assertTrue(_compareResults(self.cal.parse('5 hours', start), (target, 2))) 41 self.assertTrue(_compareResults(self.cal.parse('5 hr', start), (target, 2))) 42 self.assertTrue(_compareResults(self.cal.parse('5h', start), (target, 2))) 43 44 self.assertTrue(_compareResults(self.cal.parse('five hours from now', start), (target, 2))) 45 self.assertTrue(_compareResults(self.cal.parse('five hour from now', start), (target, 2))) 46 self.assertTrue(_compareResults(self.cal.parse('five hr from now', start), (target, 2))) 47 self.assertTrue(_compareResults(self.cal.parse('in five hours', start), (target, 2))) 48 self.assertTrue(_compareResults(self.cal.parse('in five hour', start), (target, 2))) 49 self.assertTrue(_compareResults(self.cal.parse('five hours', start), (target, 2))) 50 self.assertTrue(_compareResults(self.cal.parse('five hr', start), (target, 2)))
51
52 - def testHoursBeforeNow(self):
53 s = datetime.datetime.now() 54 t = s + datetime.timedelta(hours=-5) 55 56 start = s.timetuple() 57 target = t.timetuple() 58 59 self.assertTrue(_compareResults(self.cal.parse('5 hours before now', start), (target, 2))) 60 self.assertTrue(_compareResults(self.cal.parse('5 hr before now', start), (target, 2))) 61 self.assertTrue(_compareResults(self.cal.parse('5h before now', start), (target, 2))) 62 63 self.assertTrue(_compareResults(self.cal.parse('five hours before now', start), (target, 2))) 64 self.assertTrue(_compareResults(self.cal.parse('five hr before now', start), (target, 2)))
65 66 67 if __name__ == "__main__": 68 unittest.main() 69