parsedatetime.tests.TestRanges

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

# Source Code for Module parsedatetime.tests.TestRanges

     1  #!/usr/bin/env python
     2
     3  """
     4  Test parsing of simple date and times
     5  """
     6
     7  import unittest, time, datetime
     8  import parsedatetime.parsedatetime as pt
     9
    10
    11    # a special compare function is used to allow us to ignore the seconds as
    12    # the running of the test could cross a minute boundary
    13 -def _compareResults(result, check):
    14      targetStart, targetEnd, t_flag = result
    15      valueStart, valueEnd,  v_flag = check
    16
    17      t1_yr, t1_mth, t1_dy, t1_hr, t1_min, _, _, _, _ = targetStart
    18      v1_yr, v1_mth, v1_dy, v1_hr, v1_min, _, _, _, _ = valueStart
    19
    20      t2_yr, t2_mth, t2_dy, t2_hr, t2_min, _, _, _, _ = targetEnd
    21      v2_yr, v2_mth, v2_dy, v2_hr, v2_min, _, _, _, _ = valueEnd
    22
    23      return ((t1_yr == v1_yr) and (t1_mth == v1_mth) and (t1_dy == v1_dy) and (t1_hr == v1_hr) and
    24              (t1_min == v1_min) and (t2_yr == v2_yr) and (t2_mth == v2_mth) and (t2_dy == v2_dy) and
    25              (t2_hr == v2_hr) and (t2_min == v2_min) and (t_flag == v_flag))
    26
    27 -class test(unittest.TestCase):
    28 -    def setUp(self):
    29          self.cal = pt.Calendar()
    30          self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime()
    31
    32 -    def testTimes(self):
    33          start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple()
    34
    35          targetStart = datetime.datetime(self.yr, self.mth, self.dy, 14, 0, 0).timetuple()
    36          targetEnd   = datetime.datetime(self.yr, self.mth, self.dy, 17, 30, 0).timetuple()
    37
    38          self.assertTrue(_compareResults(self.cal.evalRanges("2 pm - 5:30 pm",          start), (targetStart, targetEnd, 2)))
    39          self.assertTrue(_compareResults(self.cal.evalRanges("2pm - 5:30pm",            start), (targetStart, targetEnd, 2)))
    40          self.assertTrue(_compareResults(self.cal.evalRanges("2:00:00 pm - 5:30:00 pm", start), (targetStart, targetEnd, 2)))
    41          self.assertTrue(_compareResults(self.cal.evalRanges("2 - 5:30pm",              start), (targetStart, targetEnd, 2)))
    42          self.assertTrue(_compareResults(self.cal.evalRanges("14:00 - 17:30",           start), (targetStart, targetEnd, 2)))
    43
    44          targetStart = datetime.datetime(self.yr, self.mth, self.dy, 10, 0, 0).timetuple()
    45          targetEnd   = datetime.datetime(self.yr, self.mth, self.dy, 13, 30, 0).timetuple()
    46
    47          self.assertTrue(_compareResults(self.cal.evalRanges("10AM - 1:30PM",            start), (targetStart, targetEnd, 2)))
    48          self.assertTrue(_compareResults(self.cal.evalRanges("10:00:00 am - 1:30:00 pm", start), (targetStart, targetEnd, 2)))
    49          self.assertTrue(_compareResults(self.cal.evalRanges("10:00 - 13:30",            start), (targetStart, targetEnd, 2)))
    50
    51
    52 -    def testDates(self):
    53          start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple()
    54
    55          targetStart = datetime.datetime(2006, 8, 29, self.hr, self.mn, self.sec).timetuple()
    56          targetEnd   = datetime.datetime(2006, 9, 2,self.hr, self.mn, self.sec).timetuple()
    57
    58          self.assertTrue(_compareResults(self.cal.evalRanges("August 29, 2006 - September 2, 2006", start), (targetStart, targetEnd, 1)))
    59          self.assertTrue(_compareResults(self.cal.evalRanges("August 29 - September 2, 2006",       start), (targetStart, targetEnd, 1)))
    60
    61          targetStart = datetime.datetime(2006, 8, 29, self.hr, self.mn, self.sec).timetuple()
    62          targetEnd   = datetime.datetime(2006, 9, 2, self.hr, self.mn, self.sec).timetuple()
    63
    64          self.assertTrue(_compareResults(self.cal.evalRanges("08/29/06 - 09/02/06", start), (targetStart, targetEnd, 1)))
    65
    66
    67      #def testSubRanges(self):
    68      #    start = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple()
    69
    70      #    targetStart = datetime.datetime(2006, 8, 1, 9, 0, 0).timetuple()
    71      #    targetEnd   = datetime.datetime(2006, 8, 15, 9, 0, 0).timetuple()
    72
    73      #    self.assertTrue(_compareResults(self.cal.evalRanges("August 1-15, 2006", start), (targetStart, targetEnd, 1)))
    74
    75
    76  if __name__ == "__main__":
    77      unittest.main()
    78