PageRenderTime 268ms CodeModel.GetById 70ms app.highlight 97ms RepoModel.GetById 75ms app.codeStats 0ms

/src/googlecl/calendar/date_test.py

http://googlecl.googlecode.com/
Python | 233 lines | 167 code | 48 blank | 18 comment | 6 complexity | 0daf20b10cc2d32d2a51cb43e560a6e6 MD5 | raw file
  1#!/usr/bin/python
  2#
  3# Copyright (C) 2010 Google Inc.
  4#
  5# Licensed under the Apache License, Version 2.0 (the "License");
  6# you may not use this file except in compliance with the License.
  7# You may obtain a copy of the License at
  8#
  9#      http://www.apache.org/licenses/LICENSE-2.0
 10#
 11# Unless required by applicable law or agreed to in writing, software
 12# distributed under the License is distributed on an "AS IS" BASIS,
 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14# See the License for the specific language governing permissions and
 15# limitations under the License.
 16
 17"""Tests for calendar dates."""
 18
 19__author__ = 'thmiller@google.com (Tom Miller)'
 20
 21import date
 22import unittest
 23from datetime import datetime
 24from datetime import timedelta
 25
 26
 27# If "now" changes, the ONE_DAY, edge, and trick tests
 28# below must be changed carefully!!
 29def static_now():
 30  return datetime(year=2010,
 31                  month=10,
 32                  day=22,
 33                  hour=14,
 34                  minute=5)
 35
 36
 37def static_today():
 38  return static_now().replace(hour=0,
 39                              minute=0,
 40                              second=0,
 41                              microsecond=0)
 42
 43NOW = static_now()
 44YEAR = NOW.year
 45ONE_DAY = timedelta(hours=24)
 46
 47
 48MONTH_DAY_TESTS = {'1/1': datetime(year=YEAR+1, month=1, day=1),
 49                   '12/31': datetime(year=YEAR, month=12, day=31),
 50                   '10/30': datetime(year=YEAR, month=10, day=30),
 51                   '2/28': datetime(year=YEAR+1, month=2, day=28),
 52                   '11/1': datetime(year=YEAR, month=11, day=1)}
 53
 54TIME_TESTS = {'7pm': NOW.replace(hour=19, minute=0),
 55              '8am': NOW.replace(hour=8, minute=0) + ONE_DAY,
 56              '01:00': NOW.replace(hour=1, minute=0) + ONE_DAY,
 57              '14:00': NOW.replace(hour=14, minute=0) + ONE_DAY}
 58
 59AMBIGUOUS_TESTS = {'6': NOW.replace(hour=18, minute=0),
 60                   '7:01': NOW.replace(hour=7, minute=1) + ONE_DAY,
 61                   '4:59': NOW.replace(hour=16, minute=59)}
 62
 63FULL_DATE_TESTS = {'7/15/2011': datetime(year=2011, month=7, day=15),
 64                   '4/30/2000': datetime(year=2000, month=4, day=30),
 65                   '2010-06-02': datetime(year=2010, month=6, day=2),
 66                   '2010-9-2': datetime(year=2010, month=9, day=2),
 67                   'March 30 2010': datetime(year=2010, month=3, day=30),
 68                   'Jan 1 1970': datetime(year=1970, month=1, day=1)}
 69
 70JOINED_TESTS = {'06/15/11 at 6': datetime(year=2011, day=15, month=6, hour=18),
 71                '3/4@19:00': datetime(year=YEAR+1, day=4, month=3, hour=19),
 72                '10/22/2012 @ 12:53pm': datetime(year=2012, month=10, day=22,
 73                                                 hour=12, minute=53),
 74                'Aug 23 2020 at 5pm': datetime(year=2020, month=8, day=23,
 75                                               hour=17)}
 76
 77EDGE_TESTS = {'10/22/2010 @ 14:05': NOW,
 78              '10/22 @ 14:04': NOW.replace(minute=4),
 79              '2:05pm': NOW,
 80              'tomorrow at 2:04': NOW.replace(minute=4) + ONE_DAY,
 81              '10/21 at 00:00': datetime(year=YEAR+1, month=10, day=21)}
 82
 83TRICK_TESTS = {'today at 2': NOW.replace(minute=0),
 84               '10/22 at 2': NOW.replace(minute=0)}
 85
 86DURATION_TESTS = {'+3': NOW + timedelta(hours=3),
 87                  '+1:20': NOW + timedelta(hours=1, minutes=20),
 88                  '+:45': NOW + timedelta(minutes=45)}
 89
 90FAIL_TESTS = [',', '@', '115', 'notadate', '1-4']
 91
 92RANGE_BASE_TEXT = '11/3 @ 9pm'
 93RANGE_EXPECTED_START = datetime(year=YEAR, month=11, day=3, hour=21)
 94
 95
 96class ParsingDatesTest(unittest.TestCase):
 97  def assertConvertedEqual(self, text, expected, actual):
 98    if expected != actual:
 99      self.fail('%s was not parsed correctly (%s != %s)'
100                % (text, expected, actual))
101
102
103class ParseSingleDateTest(ParsingDatesTest):
104
105  def setUp(self):
106    self.parser = date.DateParser(static_today, static_now)
107
108  def runTestSet(self, test_dict):
109    for text, expected_date in test_dict.items():
110      parsed_date = self.parser.parse(text)
111      self.assertConvertedEqual(text, expected_date, parsed_date.local)
112
113  def testDatetimeParse(self):
114    parsed_date = self.parser.parse('2010-01-20T13:45:00')
115    self.assertEqual(datetime(year=2010, month=1, day=20, hour=13, minute=45),
116                     parsed_date.local)
117
118  def testMonthDayParsing(self):
119    self.runTestSet(MONTH_DAY_TESTS)
120
121  def testTimeParsing(self):
122    self.runTestSet(TIME_TESTS)
123
124  def testAmbiguous(self):
125    self.runTestSet(AMBIGUOUS_TESTS)
126
127  def testFullDateParsing(self):
128    self.runTestSet(FULL_DATE_TESTS)
129
130  def testJoined(self):
131    self.runTestSet(JOINED_TESTS)
132
133  def testEdge(self):
134    self.runTestSet(EDGE_TESTS)
135
136  def testTrick(self):
137    self.runTestSet(TRICK_TESTS)
138
139  def testDuration(self):
140    self.runTestSet(DURATION_TESTS)
141
142  def testFailures(self):
143    for text in FAIL_TESTS:
144      try:
145        self.parser.parse(text)
146      except date.ParsingError, err:
147        pass
148      else:
149        self.fail('Expected ParsingError for %s' % text)
150
151
152class ParseDateRange(ParsingDatesTest):
153
154  def setUp(self):
155    self.parser = date.DateRangeParser(static_today, static_now)
156
157  def testSingleDate(self):
158    date_range = self.parser.parse(RANGE_BASE_TEXT)
159    self.assertConvertedEqual(RANGE_BASE_TEXT,
160                              RANGE_EXPECTED_START,
161                              date_range.start.local)
162    self.assertEqual(date_range.end.local, RANGE_EXPECTED_START)
163    self.assertFalse(date_range.specified_as_range)
164
165  def testStartRange(self):
166    text = RANGE_BASE_TEXT + ','
167    date_range = self.parser.parse(text)
168    self.assertConvertedEqual(text,
169                              RANGE_EXPECTED_START,
170                              date_range.start.local)
171    self.assertEqual(date_range.end, None)
172    self.assertTrue(date_range.specified_as_range)
173
174  def testStartAndDurationRange(self):
175    text = RANGE_BASE_TEXT + ',+5'
176    date_range = self.parser.parse(text)
177    self.assertConvertedEqual(RANGE_BASE_TEXT,
178                              RANGE_EXPECTED_START,
179                              date_range.start.local)
180    self.assertEqual(date_range.end.local,
181                     RANGE_EXPECTED_START + timedelta(hours=5))
182    self.assertTrue(date_range.specified_as_range)
183
184  def testAllDurationRange(self):
185    text = '+2,+3'
186    date_range = self.parser.parse(text)
187    self.assertEqual(date_range.start.local,
188                     NOW + timedelta(hours=2))
189    self.assertEqual(date_range.end.local,
190                     NOW + timedelta(hours=5))
191    self.assertTrue(date_range.specified_as_range)
192
193  def testEndDurationRange(self):
194    text = ',+3'
195    date_range = self.parser.parse(text)
196    self.assertEqual(date_range.start, None)
197    self.assertEqual(date_range.end.local,
198                     NOW + timedelta(hours=3))
199    self.assertTrue(date_range.specified_as_range)
200
201  def testDurationStart(self):
202    text = '+1' + ',' + RANGE_BASE_TEXT
203    date_range = self.parser.parse(text)
204    self.assertEqual(date_range.start.local, NOW + timedelta(hours=1))
205    self.assertConvertedEqual(RANGE_BASE_TEXT,
206                              RANGE_EXPECTED_START,
207                              date_range.end.local)
208    self.assertTrue(date_range.specified_as_range)
209
210  def testStartEndRange(self):
211    end_text = '11/29'
212    text = RANGE_BASE_TEXT + ',' + end_text
213    date_range = self.parser.parse(text)
214    self.assertConvertedEqual(RANGE_BASE_TEXT,
215                              RANGE_EXPECTED_START,
216                              date_range.start.local)
217    self.assertConvertedEqual(end_text,
218                              datetime(year=YEAR, month=11, day=29),
219                              date_range.end.local)
220    self.assertTrue(date_range.specified_as_range)
221
222  def testEndRange(self):
223    text = ',' + RANGE_BASE_TEXT
224    date_range = self.parser.parse(text)
225    self.assertEqual(date_range.start, None)
226    self.assertConvertedEqual(text,
227                              RANGE_EXPECTED_START,
228                              date_range.end.local)
229    self.assertTrue(date_range.specified_as_range)
230
231
232if __name__ == '__main__':
233  unittest.main()