parsedatetime.tests.TestPhrases

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

# Source Code for Module parsedatetime.tests.TestPhrases

      1  #!/usr/bin/env python
      2
      3  """
      4  Test parsing of strings that are phrases
      5  """
      6
      7  import unittest, time, datetime
      8  import parsedatetime.parsedatetime as pt
      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, debug=False):
     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      if debug:
     20          print target, t_flag
     21          print t_yr, t_mth, t_dy, t_hr, t_min, t_sec, t_wd, t_yd, t_isdst, t_flag
     22          print value, v_flag
     23          print v_yr, v_mth, v_dy, v_hr, v_min, v_sec, v_wd, v_yd, v_isdst, v_flag
     24
     25      return ((t_yr == v_yr) and (t_mth == v_mth) and (t_dy == v_dy) and
     26              (t_hr == v_hr) and (t_min == v_min)) and (t_flag == v_flag)
     27
     28 -class test(unittest.TestCase):
     29 -    def setUp(self):
     30          self.cal = pt.Calendar()
     31          self.yr, self.mth, self.dy, self.hr, self.mn, self.sec, self.wd, self.yd, self.isdst = time.localtime()
     32
     33 -    def testPhrases(self):
     34          start  = datetime.datetime(self.yr, self.mth, self.dy, self.hr, self.mn, self.sec).timetuple()
     35          target = datetime.datetime(self.yr, self.mth, self.dy, 16, 0, 0).timetuple()
     36
     37          self.assertTrue(_compareResults(self.cal.parse('flight from SFO at 4pm', start), (target, 2)))
     38
     39          target = datetime.datetime(self.yr, self.mth, self.dy, 17, 0, 0).timetuple()
     40
     41          self.assertTrue(_compareResults(self.cal.parse('eod',         start), (target, 2)))
     42          self.assertTrue(_compareResults(self.cal.parse('meeting eod', start), (target, 2)))
     43          self.assertTrue(_compareResults(self.cal.parse('eod meeting', start), (target, 2)))
     44
     45          target = datetime.datetime(self.yr, self.mth, self.dy, 17, 0, 0) + datetime.timedelta(days=1)
     46          target = target.timetuple()
     47
     48          self.assertTrue(_compareResults(self.cal.parse('tomorrow eod', start), (target, 3)))
     49          self.assertTrue(_compareResults(self.cal.parse('eod tomorrow', start), (target, 3)))
     50
     51
     52 -    def testPhraseWithDays_DOWStyle_1_False(self):
     53          s = datetime.datetime.now()
     54
     55            # find out what day we are currently on
     56            # and determine what the next day of week is
     57          t      = s + datetime.timedelta(days=1)
     58          start  = s.timetuple()
     59
     60          (yr, mth, dy, _, _, _, wd, yd, isdst) = t.timetuple()
     61
     62          target = (yr, mth, dy, 17, 0, 0, wd, yd, isdst)
     63
     64          d = self.wd + 1
     65          if d > 6:
     66              d = 0
     67
     68          day = self.cal.ptc.Weekdays[d]
     69
     70          self.assertTrue(_compareResults(self.cal.parse('eod %s' % day, start), (target, 3)))
     71
     72            # find out what day we are currently on
     73            # and determine what the previous day of week is
     74          t = s + datetime.timedelta(days=6)
     75
     76          (yr, mth, dy, hr, mn, sec, wd, yd, isdst) = t.timetuple()
     77
     78          target = (yr, mth, dy, 17, 0, 0, wd, yd, isdst)
     79
     80          d = self.wd - 1
     81          if d < 0:
     82              d = 6
     83
     84          day = self.cal.ptc.Weekdays[d]
     85
     86          self.assertTrue(_compareResults(self.cal.parse('eod %s' % day, start), (target, 3)))
     87
     88
     89 -    def testEndOfPhrases(self):
     90          s = datetime.datetime.now()
     91
     92            # find out what month we are currently on
     93            # set the day to 1 and then go back a day
     94            # to get the end of the current month
     95          (yr, mth, _, hr, mn, sec, _, _, _) = s.timetuple()
     96
     97          mth += 1
     98          if mth > 12:
     99              mth = 1
    100              yr += 1
    101
    102          t = datetime.datetime(yr, mth, 1, 9, 0, 0) + datetime.timedelta(days=-1)
    103
    104          start  = s.timetuple()
    105          target = t.timetuple()
    106
    107          self.assertTrue(_compareResults(self.cal.parse('eom',         start), (target, 1)))
    108          self.assertTrue(_compareResults(self.cal.parse('meeting eom', start), (target, 1)))
    109
    110          s = datetime.datetime.now()
    111
    112          (yr, mth, dy, hr, mn, sec, wd, yd, isdst) = s.timetuple()
    113
    114          t = datetime.datetime(yr, 12, 31, hr, mn, sec)
    115
    116          start  = s.timetuple()
    117          target = t.timetuple()
    118
    119          self.assertTrue(_compareResults(self.cal.parse('eoy',         start), (target, 1)))
    120          self.assertTrue(_compareResults(self.cal.parse('meeting eoy', start), (target, 1)))
    121