parsedatetime.tests.TestUnits

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

# Source Code for Module parsedatetime.tests.TestUnits

      1  #!/usr/bin/env python
      2
      3  """
      4  Test parsing of units
      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 testMinutes(self):
     29          s = datetime.datetime.now()
     30          t = s + datetime.timedelta(minutes=1)
     31
     32          start  = s.timetuple()
     33          target = t.timetuple()
     34
     35          self.assertTrue(_compareResults(self.cal.parse('1 minute',  start), (target, 2)))
     36          self.assertTrue(_compareResults(self.cal.parse('1 minutes', start), (target, 2)))
     37          self.assertTrue(_compareResults(self.cal.parse('1 min',     start), (target, 2)))
     38          self.assertTrue(_compareResults(self.cal.parse('1min',      start), (target, 2)))
     39          self.assertTrue(_compareResults(self.cal.parse('1 m',       start), (target, 2)))
     40          self.assertTrue(_compareResults(self.cal.parse('1m',        start), (target, 2)))
     41
     42 -    def testHours(self):
     43          s = datetime.datetime.now()
     44          t = s + datetime.timedelta(hours=1)
     45
     46          start  = s.timetuple()
     47          target = t.timetuple()
     48
     49          self.assertTrue(_compareResults(self.cal.parse('1 hour',  start), (target, 2)))
     50          self.assertTrue(_compareResults(self.cal.parse('1 hours', start), (target, 2)))
     51          self.assertTrue(_compareResults(self.cal.parse('1 hr',    start), (target, 2)))
     52
     53
     54 -    def testDays(self):
     55          s = datetime.datetime.now()
     56          t = s + datetime.timedelta(days=1)
     57
     58          start  = s.timetuple()
     59          target = t.timetuple()
     60
     61          self.assertTrue(_compareResults(self.cal.parse('1 day',  start), (target, 1)))
     62          self.assertTrue(_compareResults(self.cal.parse('1 days', start), (target, 1)))
     63          self.assertTrue(_compareResults(self.cal.parse('1days',  start), (target, 1)))
     64          self.assertTrue(_compareResults(self.cal.parse('1 dy',   start), (target, 1)))
     65          self.assertTrue(_compareResults(self.cal.parse('1 d',    start), (target, 1)))
     66
     67
     68 -    def testNegativeDays(self):
     69          s = datetime.datetime.now()
     70          t = s + datetime.timedelta(days=-1)
     71
     72          start  = s.timetuple()
     73          target = t.timetuple()
     74
     75          self.assertTrue(_compareResults(self.cal.parse('-1 day',  start), (target, 1)))
     76          self.assertTrue(_compareResults(self.cal.parse('-1 days', start), (target, 1)))
     77          self.assertTrue(_compareResults(self.cal.parse('-1days',  start), (target, 1)))
     78          self.assertTrue(_compareResults(self.cal.parse('-1 dy',   start), (target, 1)))
     79          self.assertTrue(_compareResults(self.cal.parse('-1 d',    start), (target, 1)))
     80
     81          self.assertTrue(_compareResults(self.cal.parse('- 1 day',  start), (target, 1)))
     82          self.assertTrue(_compareResults(self.cal.parse('- 1 days', start), (target, 1)))
     83          self.assertTrue(_compareResults(self.cal.parse('- 1days',  start), (target, 1)))
     84          self.assertTrue(_compareResults(self.cal.parse('- 1 dy',   start), (target, 1)))
     85          self.assertTrue(_compareResults(self.cal.parse('- 1 d',    start), (target, 1)))
     86
     87
     88 -    def testWeeks(self):
     89          s = datetime.datetime.now()
     90          t = s + datetime.timedelta(weeks=1)
     91
     92          start  = s.timetuple()
     93          target = t.timetuple()
     94
     95          self.assertTrue(_compareResults(self.cal.parse('1 week',  start), (target, 1)))
     96          self.assertTrue(_compareResults(self.cal.parse('1week',   start), (target, 1)))
     97          self.assertTrue(_compareResults(self.cal.parse('1 weeks', start), (target, 1)))
     98          self.assertTrue(_compareResults(self.cal.parse('1 wk',    start), (target, 1)))
     99          self.assertTrue(_compareResults(self.cal.parse('1 w',     start), (target, 1)))
    100          self.assertTrue(_compareResults(self.cal.parse('1w',      start), (target, 1)))
    101
    102 -    def testMonths(self):
    103          s = datetime.datetime.now()
    104          t = self.cal.inc(s, month=1)
    105
    106          start  = s.timetuple()
    107          target = t.timetuple()
    108
    109          self.assertTrue(_compareResults(self.cal.parse('1 month',  start), (target, 1)))
    110          self.assertTrue(_compareResults(self.cal.parse('1 months', start), (target, 1)))
    111          self.assertTrue(_compareResults(self.cal.parse('1month',   start), (target, 1)))
    112
    113
    114 -    def testYears(self):
    115          s = datetime.datetime.now()
    116          t = self.cal.inc(s, year=1)
    117
    118          start  = s.timetuple()
    119          target = t.timetuple()
    120
    121          self.assertTrue(_compareResults(self.cal.parse('1 year',  start), (target, 1)))
    122          self.assertTrue(_compareResults(self.cal.parse('1 years', start), (target, 1)))
    123          self.assertTrue(_compareResults(self.cal.parse('1 yr',    start), (target, 1)))
    124          self.assertTrue(_compareResults(self.cal.parse('1 y',     start), (target, 1)))
    125          self.assertTrue(_compareResults(self.cal.parse('1y',      start), (target, 1)))
    126
    127
    128  if __name__ == "__main__":
    129      unittest.main()
    130