parsedatetime.tests.TestMultiple

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

# Source Code for Module parsedatetime.tests.TestMultiple

     1  #!/usr/bin/env python
     2
     3  """
     4  Test parsing of strings with multiple chunks
     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 testSimpleMultipleItems(self):
    29          s = datetime.datetime.now()
    30          t = self.cal.inc(s, year=3) + datetime.timedelta(days=5, weeks=2)
    31
    32          start  = s.timetuple()
    33          target = t.timetuple()
    34
    35          self.assertTrue(_compareResults(self.cal.parse('3 years 2 weeks 5 days', start), (target, 1)))
    36          self.assertTrue(_compareResults(self.cal.parse('3years 2weeks 5days',    start), (target, 1)))
    37
    38 -    def testMultipleItemsSingleCharUnits(self):
    39          s = datetime.datetime.now()
    40          t = self.cal.inc(s, year=3) + datetime.timedelta(days=5, weeks=2)
    41
    42          start  = s.timetuple()
    43          target = t.timetuple()
    44
    45          self.assertTrue(_compareResults(self.cal.parse('3 y 2 w 5 d', start), (target, 1)))
    46          self.assertTrue(_compareResults(self.cal.parse('3y 2w 5d',    start), (target, 1)))
    47
    48          t      = self.cal.inc(s, year=3) + datetime.timedelta(hours=5, minutes=50)
    49          target = t.timetuple()
    50
    51          self.assertTrue(_compareResults(self.cal.parse('3y 5h 50m', start), (target, 3)))
    52
    53
    54 -    def testMultipleItemsWithPunctuation(self):
    55          s = datetime.datetime.now()
    56          t = self.cal.inc(s, year=3) + datetime.timedelta(days=5, weeks=2)
    57
    58          start  = s.timetuple()
    59          target = t.timetuple()
    60
    61          self.assertTrue(_compareResults(self.cal.parse('3 years, 2 weeks, 5 days',    start), (target, 1)))
    62          self.assertTrue(_compareResults(self.cal.parse('3 years, 2 weeks and 5 days', start), (target, 1)))
    63          self.assertTrue(_compareResults(self.cal.parse('3y, 2w, 5d ',                 start), (target, 1)))
    64
    65
    66 -    def testUnixATStyle(self):
    67          s = datetime.datetime.now()
    68          t = s + datetime.timedelta(days=3)
    69
    70          t = t.replace(hour=16, minute=0, second=0)
    71
    72          start  = s.timetuple()
    73          target = t.timetuple()
    74
    75          self.assertTrue(_compareResults(self.cal.parse('4pm + 3 days', start), (target, 3)))
    76          self.assertTrue(_compareResults(self.cal.parse('4pm +3 days',  start), (target, 3)))
    77
    78
    79 -    def testUnixATStyleNegative(self):
    80          s = datetime.datetime.now()
    81          t = s + datetime.timedelta(days=-3)
    82
    83          t = t.replace(hour=16, minute=0, second=0)
    84
    85          start  = s.timetuple()
    86          target = t.timetuple()
    87
    88          self.assertTrue(_compareResults(self.cal.parse('4pm - 3 days', start), (target, 3)))
    89          self.assertTrue(_compareResults(self.cal.parse('4pm -3 days',  start), (target, 3)))
    90
    91
    92  if __name__ == "__main__":
    93      unittest.main()
    94