parsedatetime.tests.TestSimpleOffsets

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

# Source Code for Module parsedatetime.tests.TestSimpleOffsets

      1  #!/usr/bin/env python
      2
      3  """
      4  Test parsing of 'simple' offsets
      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      target, t_flag = result
     15      value,  v_flag = check
     16
     17      t_yr, t_mth, t_dy, t_hr, t_min, _, _, _, _ = target
     18      v_yr, v_mth, v_dy, v_hr, v_min, _, _, _, _ = value
     19
     20      return ((t_yr == v_yr) and (t_mth == v_mth) and (t_dy == v_dy) and
     21              (t_hr == v_hr) and (t_min == v_min)) and (t_flag == v_flag)
     22
     23 -class test(unittest.TestCase):
     24 -    def setUp(self):
     25          self.cal = pt.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 testMinutesFromNow(self):
     29          s = datetime.datetime.now()
     30          t = s + datetime.timedelta(minutes=5)
     31
     32          start  = s.timetuple()
     33          target = t.timetuple()
     34
     35          self.assertTrue(_compareResults(self.cal.parse('5 minutes from now', start), (target, 2)))
     36          self.assertTrue(_compareResults(self.cal.parse('5 min from now',     start), (target, 2)))
     37          self.assertTrue(_compareResults(self.cal.parse('5m from now',        start), (target, 2)))
     38          self.assertTrue(_compareResults(self.cal.parse('in 5 minutes',       start), (target, 2)))
     39          self.assertTrue(_compareResults(self.cal.parse('in 5 min',           start), (target, 2)))
     40          self.assertTrue(_compareResults(self.cal.parse('5 min from now',     start), (target, 2)))
     41          self.assertTrue(_compareResults(self.cal.parse('5 minutes',          start), (target, 2)))
     42          self.assertTrue(_compareResults(self.cal.parse('5 min',              start), (target, 2)))
     43          self.assertTrue(_compareResults(self.cal.parse('5m',                 start), (target, 2)))
     44
     45
     46 -    def testMinutesBeforeNow(self):
     47          s = datetime.datetime.now()
     48          t = s + datetime.timedelta(minutes=-5)
     49
     50          start  = s.timetuple()
     51          target = t.timetuple()
     52
     53          self.assertTrue(_compareResults(self.cal.parse('5 minutes before now', start), (target, 2)))
     54          self.assertTrue(_compareResults(self.cal.parse('5 min before now',     start), (target, 2)))
     55          self.assertTrue(_compareResults(self.cal.parse('5m before now',        start), (target, 2)))
     56
     57
     58 -    def testOffsetAfterNoon(self):
     59          s = datetime.datetime.now()
     60          t = datetime.datetime(self.yr, self.mth, self.dy, 12, 0, 0) + datetime.timedelta(hours=5)
     61
     62          start  = s.timetuple()
     63          target = t.timetuple()
     64
     65          self.assertTrue(_compareResults(self.cal.parse('5 hours from noon',      start), (target, 2)))
     66          self.assertTrue(_compareResults(self.cal.parse('5 hours after noon',     start), (target, 2)))
     67          self.assertTrue(_compareResults(self.cal.parse('5 hours after 12pm',     start), (target, 2)))
     68          self.assertTrue(_compareResults(self.cal.parse('5 hours after 12 pm',    start), (target, 2)))
     69          self.assertTrue(_compareResults(self.cal.parse('5 hours after 12:00pm',  start), (target, 2)))
     70          self.assertTrue(_compareResults(self.cal.parse('5 hours after 12:00 pm', start), (target, 2)))
     71
     72
     73 -    def testOffsetBeforeNoon(self):
     74          s = datetime.datetime.now()
     75          t = datetime.datetime(self.yr, self.mth, self.dy, 12, 0, 0) + datetime.timedelta(hours=-5)
     76
     77          start  = s.timetuple()
     78          target = t.timetuple()
     79
     80          self.assertTrue(_compareResults(self.cal.parse('5 hours before noon',     start), (target, 2)))
     81          self.assertTrue(_compareResults(self.cal.parse('5 hours before 12pm',     start), (target, 2)))
     82          self.assertTrue(_compareResults(self.cal.parse('5 hours before 12 pm',    start), (target, 2)))
     83          self.assertTrue(_compareResults(self.cal.parse('5 hours before 12:00pm',  start), (target, 2)))
     84          self.assertTrue(_compareResults(self.cal.parse('5 hours before 12:00 pm', start), (target, 2)))
     85
     86
     87 -    def testWeekFromNow(self):
     88          s = datetime.datetime.now()
     89          t = s + datetime.timedelta(weeks=1)
     90
     91          start  = s.timetuple()
     92          target = t.timetuple()
     93
     94          self.assertTrue(_compareResults(self.cal.parse('in 1 week',       start), (target, 1)))
     95          self.assertTrue(_compareResults(self.cal.parse('1 week from now', start), (target, 1)))
     96          self.assertTrue(_compareResults(self.cal.parse('in 7 days',       start), (target, 1)))
     97          self.assertTrue(_compareResults(self.cal.parse('7 days from now', start), (target, 1)))
     98          #self.assertTrue(_compareResults(self.cal.parse('next week',       start), (target, 1)))
     99
    100
    101 -    def testWeekBeforeNow(self):
    102          s = datetime.datetime.now()
    103          t = s + datetime.timedelta(weeks=-1)
    104
    105          start  = s.timetuple()
    106          target = t.timetuple()
    107
    108          self.assertTrue(_compareResults(self.cal.parse('1 week before now', start), (target, 1)))
    109          self.assertTrue(_compareResults(self.cal.parse('7 days before now', start), (target, 1)))
    110          #self.assertTrue(_compareResults(self.cal.parse('last week',         start), (target, 1)))
    111
    112
    113 -    def testSpecials(self):
    114          s = datetime.datetime.now()
    115          t = datetime.datetime(self.yr, self.mth, self.dy, 9, 0, 0) + datetime.timedelta(days=1)
    116
    117          start  = s.timetuple()
    118          target = t.timetuple()
    119
    120          self.assertTrue(_compareResults(self.cal.parse('tomorrow', start), (target, 1)))
    121          self.assertTrue(_compareResults(self.cal.parse('next day', start), (target, 1)))
    122
    123          t      = datetime.datetime(self.yr, self.mth, self.dy, 9, 0, 0) + datetime.timedelta(days=-1)
    124          target = t.timetuple()
    125
    126          self.assertTrue(_compareResults(self.cal.parse('yesterday', start), (target, 1)))
    127
    128          t      = datetime.datetime(self.yr, self.mth, self.dy, 9, 0, 0)
    129          target = t.timetuple()
    130
    131          self.assertTrue(_compareResults(self.cal.parse('today', start), (target, 1)))
    132
    133
    134  if __name__ == "__main__":
    135      unittest.main()
    136