Back to index

plone3  3.1.7
testDateComponentsSupport.py
Go to the documentation of this file.
00001 #
00002 # Tests for date_components_support.py
00003 #
00004 
00005 from Products.CMFPlone.tests import PloneTestCase
00006 
00007 from DateTime import DateTime
00008 
00009 def sortTuple(t):
00010     l = list(t)
00011     l.sort()
00012     return tuple(l)
00013 
00014 
00015 class TestDateComponentsSupport(PloneTestCase.PloneTestCase):
00016 
00017     def afterSetUp(self):
00018         date = DateTime(2002, 8, 1, 17, 42, 0)
00019         self.d = self.portal.date_components_support(date)
00020 
00021     def testElements(self):
00022         self.assertEqual(sortTuple(self.d.keys()), 
00023                 ('ampm', 'days', 'hours', 'minutes', 'months', 'years'))
00024 
00025     def testYears(self):
00026         this_year = DateTime().year()
00027         site_properties = self.portal.portal_properties.site_properties
00028         min_year = site_properties.getProperty('calendar_starting_year', 1999)
00029         max_year = site_properties.getProperty('calendar_future_years_available', 5) + this_year
00030 
00031         data = [
00032             {'selected': None, 'id': '--', 'value': '0000'}
00033         ]
00034 
00035         for y in range(min_year, max_year+1):
00036             d = {'selected': None, 'id': y, 'value': y}
00037             if y == 2002:
00038                 d['selected'] = 1
00039             data.append(d)
00040 
00041         years = self.d.get('years')
00042         for i in range(max_year - min_year + 1):
00043             self.assertEqual(years[i], data[i])
00044 
00045     def testMonths(self):
00046         data = [
00047             {'selected': None, 'id': '--',      'value': '00'},
00048             {'selected': None, 'id': 'January',   'value': '01'},
00049             {'selected': None, 'id': 'February',  'value': '02'},
00050             {'selected': None, 'id': 'March',     'value': '03'},
00051             {'selected': None, 'id': 'April',     'value': '04'},
00052             {'selected': None, 'id': 'May',       'value': '05'},
00053             {'selected': None, 'id': 'June',      'value': '06'},
00054             {'selected': None, 'id': 'July',      'value': '07'},
00055             {'selected': 1,    'id': 'August',    'value': '08'},
00056             {'selected': None, 'id': 'September', 'value': '09'},
00057             {'selected': None, 'id': 'October',   'value': '10'},
00058             {'selected': None, 'id': 'November',  'value': '11'},
00059             {'selected': None, 'id': 'December',  'value': '12'},
00060         ]
00061 
00062         months = self.d.get('months')
00063         for i in range(13):
00064             self.assertEqual(months[i], data[i])
00065 
00066     def testDays(self):
00067         data = [
00068             {'selected': None, 'id': '--', 'value': '00'},
00069             {'selected': 1,    'id': 1,      'value': '01'},
00070             {'selected': None, 'id': 2,      'value': '02'},
00071             {'selected': None, 'id': 3,      'value': '03'},
00072             {'selected': None, 'id': 4,      'value': '04'},
00073             {'selected': None, 'id': 5,      'value': '05'},
00074             {'selected': None, 'id': 6,      'value': '06'},
00075             {'selected': None, 'id': 7,      'value': '07'},
00076             {'selected': None, 'id': 8,      'value': '08'},
00077             {'selected': None, 'id': 9,      'value': '09'},
00078             {'selected': None, 'id': 10,     'value': '10'},
00079             {'selected': None, 'id': 11,     'value': '11'},
00080             {'selected': None, 'id': 12,     'value': '12'},
00081             {'selected': None, 'id': 13,     'value': '13'},
00082             {'selected': None, 'id': 14,     'value': '14'},
00083             {'selected': None, 'id': 15,     'value': '15'},
00084             {'selected': None, 'id': 16,     'value': '16'},
00085             {'selected': None, 'id': 17,     'value': '17'},
00086             {'selected': None, 'id': 18,     'value': '18'},
00087             {'selected': None, 'id': 19,     'value': '19'},
00088             {'selected': None, 'id': 20,     'value': '20'},
00089             {'selected': None, 'id': 21,     'value': '21'},
00090             {'selected': None, 'id': 22,     'value': '22'},
00091             {'selected': None, 'id': 23,     'value': '23'},
00092             {'selected': None, 'id': 24,     'value': '24'},
00093             {'selected': None, 'id': 25,     'value': '25'},
00094             {'selected': None, 'id': 26,     'value': '26'},
00095             {'selected': None, 'id': 27,     'value': '27'},
00096             {'selected': None, 'id': 28,     'value': '28'},
00097             {'selected': None, 'id': 29,     'value': '29'},
00098             {'selected': None, 'id': 30,     'value': '30'},
00099             {'selected': None, 'id': 31,     'value': '31'},
00100         ]
00101     
00102         days = self.d.get('days')
00103         for i in range(32):
00104             self.assertEqual(days[i], data[i])
00105 
00106     def testHours(self):
00107         data = [
00108             {'selected': None, 'id': '--', 'value': '00'},
00109             {'selected': None, 'id': '00',   'value': '00'},
00110             {'selected': None, 'id': '01',   'value': '01'},
00111             {'selected': None, 'id': '02',   'value': '02'},
00112             {'selected': None, 'id': '03',   'value': '03'},
00113             {'selected': None, 'id': '04',   'value': '04'},
00114             {'selected': None, 'id': '05',   'value': '05'},
00115             {'selected': None, 'id': '06',   'value': '06'},
00116             {'selected': None, 'id': '07',   'value': '07'},
00117             {'selected': None, 'id': '08',   'value': '08'},
00118             {'selected': None, 'id': '09',   'value': '09'},
00119             {'selected': None, 'id': '10',   'value': '10'},
00120             {'selected': None, 'id': '11',   'value': '11'},
00121             {'selected': None, 'id': '12',   'value': '12'},
00122             {'selected': None, 'id': '13',   'value': '13'},
00123             {'selected': None, 'id': '14',   'value': '14'},
00124             {'selected': None, 'id': '15',   'value': '15'},
00125             {'selected': None, 'id': '16',   'value': '16'},
00126             {'selected': 1,    'id': '17',   'value': '17'},
00127             {'selected': None, 'id': '18',   'value': '18'},
00128             {'selected': None, 'id': '19',   'value': '19'},
00129             {'selected': None, 'id': '20',   'value': '20'},
00130             {'selected': None, 'id': '21',   'value': '21'},
00131             {'selected': None, 'id': '22',   'value': '22'},
00132             {'selected': None, 'id': '23',   'value': '23'},
00133         ]
00134 
00135         hours = self.d.get('hours')
00136         for i in range(25):
00137             self.assertEqual(hours[i], data[i])
00138 
00139     def testMinutes(self):
00140         data = [
00141             {'selected': None, 'id': '--', 'value': '00'},
00142             {'selected': None, 'id': '00',   'value': '00'},
00143             {'selected': None, 'id': '05',   'value': '05'},
00144             {'selected': None, 'id': '10',   'value': '10'},
00145             {'selected': None, 'id': '15',   'value': '15'},
00146             {'selected': None, 'id': '20',   'value': '20'},
00147             {'selected': None, 'id': '25',   'value': '25'},
00148             {'selected': None, 'id': '30',   'value': '30'},
00149             {'selected': None, 'id': '35',   'value': '35'},
00150             {'selected': None, 'id': '40',   'value': '40'},
00151             {'selected': 1,    'id': '45',   'value': '45'},
00152             {'selected': None, 'id': '50',   'value': '50'},
00153             {'selected': None, 'id': '55',   'value': '55'},
00154         ]
00155 
00156         minutes = self.d.get('minutes')
00157         for i in range(13):
00158             self.assertEqual(minutes[i], data[i])
00159 
00160     def testAM(self):
00161         d = DateTime(2002, 8, 1, 3, 0, 0)
00162         d = self.portal.date_components_support(d)
00163         self.assertEqual(d.get('ampm'), [])
00164 
00165     def testPM(self):
00166         ampm = self.d.get('ampm')
00167         self.assertEqual(ampm, [])
00168 
00169 
00170 class TestDateComponentsSupportDefault(PloneTestCase.PloneTestCase):
00171 
00172     def afterSetUp(self):
00173         self.d = self.portal.date_components_support(None)
00174 
00175     def testElements(self):
00176         self.assertEqual(sortTuple(self.d.keys()), 
00177                 ('ampm', 'days', 'hours', 'minutes', 'months', 'years'))
00178 
00179     def testYears(self):
00180         this_year = DateTime().year()
00181         site_properties = self.portal.portal_properties.site_properties
00182         min_year = site_properties.getProperty('calendar_starting_year', 1999)
00183         max_year = site_properties.getProperty('calendar_future_years_available', 5) + this_year
00184 
00185         data = [
00186             {'selected': None, 'id': '--', 'value': '0000'}
00187         ]
00188 
00189         for y in range(min_year, max_year+1):
00190             d = {'selected': None, 'id': y, 'value': y}
00191             if y == this_year:
00192                 d['selected'] = 1
00193             data.append(d)
00194 
00195         years = self.d.get('years')
00196         for i in range(max_year - min_year + 1):
00197             self.assertEqual(years[i], data[i])
00198 
00199     def testMonths(self):
00200         data = [
00201             {'selected': 1,    'id': '--',      'value': '00'},
00202             {'selected': None, 'id': 'January',   'value': '01'},
00203             {'selected': None, 'id': 'February',  'value': '02'},
00204             {'selected': None, 'id': 'March',     'value': '03'},
00205             {'selected': None, 'id': 'April',     'value': '04'},
00206             {'selected': None, 'id': 'May',       'value': '05'},
00207             {'selected': None, 'id': 'June',      'value': '06'},
00208             {'selected': None, 'id': 'July',      'value': '07'},
00209             {'selected': None, 'id': 'August',    'value': '08'},
00210             {'selected': None, 'id': 'September', 'value': '09'},
00211             {'selected': None, 'id': 'October',   'value': '10'},
00212             {'selected': None, 'id': 'November',  'value': '11'},
00213             {'selected': None, 'id': 'December',  'value': '12'},
00214         ]
00215 
00216         months = self.d.get('months')
00217         for i in range(13):
00218             self.assertEqual(months[i], data[i])
00219 
00220     def testDays(self):
00221         data = [
00222             {'selected': 1,    'id': '--', 'value': '00'},
00223             {'selected': None, 'id': 1,      'value': '01'},
00224             {'selected': None, 'id': 2,      'value': '02'},
00225             {'selected': None, 'id': 3,      'value': '03'},
00226             {'selected': None, 'id': 4,      'value': '04'},
00227             {'selected': None, 'id': 5,      'value': '05'},
00228             {'selected': None, 'id': 6,      'value': '06'},
00229             {'selected': None, 'id': 7,      'value': '07'},
00230             {'selected': None, 'id': 8,      'value': '08'},
00231             {'selected': None, 'id': 9,      'value': '09'},
00232             {'selected': None, 'id': 10,     'value': '10'},
00233             {'selected': None, 'id': 11,     'value': '11'},
00234             {'selected': None, 'id': 12,     'value': '12'},
00235             {'selected': None, 'id': 13,     'value': '13'},
00236             {'selected': None, 'id': 14,     'value': '14'},
00237             {'selected': None, 'id': 15,     'value': '15'},
00238             {'selected': None, 'id': 16,     'value': '16'},
00239             {'selected': None, 'id': 17,     'value': '17'},
00240             {'selected': None, 'id': 18,     'value': '18'},
00241             {'selected': None, 'id': 19,     'value': '19'},
00242             {'selected': None, 'id': 20,     'value': '20'},
00243             {'selected': None, 'id': 21,     'value': '21'},
00244             {'selected': None, 'id': 22,     'value': '22'},
00245             {'selected': None, 'id': 23,     'value': '23'},
00246             {'selected': None, 'id': 24,     'value': '24'},
00247             {'selected': None, 'id': 25,     'value': '25'},
00248             {'selected': None, 'id': 26,     'value': '26'},
00249             {'selected': None, 'id': 27,     'value': '27'},
00250             {'selected': None, 'id': 28,     'value': '28'},
00251             {'selected': None, 'id': 29,     'value': '29'},
00252             {'selected': None, 'id': 30,     'value': '30'},
00253             {'selected': None, 'id': 31,     'value': '31'},
00254         ]
00255     
00256         days = self.d.get('days')
00257         for i in range(32):
00258             self.assertEqual(days[i], data[i])
00259 
00260     def testHours(self):
00261         data = [
00262             {'selected': 1,    'id': '--', 'value': '00'},
00263             {'selected': None, 'id': '00',   'value': '00'},
00264             {'selected': None, 'id': '01',   'value': '01'},
00265             {'selected': None, 'id': '02',   'value': '02'},
00266             {'selected': None, 'id': '03',   'value': '03'},
00267             {'selected': None, 'id': '04',   'value': '04'},
00268             {'selected': None, 'id': '05',   'value': '05'},
00269             {'selected': None, 'id': '06',   'value': '06'},
00270             {'selected': None, 'id': '07',   'value': '07'},
00271             {'selected': None, 'id': '08',   'value': '08'},
00272             {'selected': None, 'id': '09',   'value': '09'},
00273             {'selected': None, 'id': '10',   'value': '10'},
00274             {'selected': None, 'id': '11',   'value': '11'},
00275             {'selected': None, 'id': '12',   'value': '12'},
00276             {'selected': None, 'id': '13',   'value': '13'},
00277             {'selected': None, 'id': '14',   'value': '14'},
00278             {'selected': None, 'id': '15',   'value': '15'},
00279             {'selected': None, 'id': '16',   'value': '16'},
00280             {'selected': None, 'id': '17',   'value': '17'},
00281             {'selected': None, 'id': '18',   'value': '18'},
00282             {'selected': None, 'id': '19',   'value': '19'},
00283             {'selected': None, 'id': '20',   'value': '20'},
00284             {'selected': None, 'id': '21',   'value': '21'},
00285             {'selected': None, 'id': '22',   'value': '22'},
00286             {'selected': None, 'id': '23',   'value': '23'},
00287         ]
00288 
00289         hours = self.d.get('hours')
00290         for i in range(25):
00291             self.assertEqual(hours[i], data[i])
00292 
00293     def testMinutes(self):
00294         data = [
00295             {'selected': 1,    'id': '--', 'value': '00'},
00296             {'selected': None, 'id': '00',   'value': '00'},
00297             {'selected': None, 'id': '05',   'value': '05'},
00298             {'selected': None, 'id': '10',   'value': '10'},
00299             {'selected': None, 'id': '15',   'value': '15'},
00300             {'selected': None, 'id': '20',   'value': '20'},
00301             {'selected': None, 'id': '25',   'value': '25'},
00302             {'selected': None, 'id': '30',   'value': '30'},
00303             {'selected': None, 'id': '35',   'value': '35'},
00304             {'selected': None, 'id': '40',   'value': '40'},
00305             {'selected': None, 'id': '45',   'value': '45'},
00306             {'selected': None, 'id': '50',   'value': '50'},
00307             {'selected': None, 'id': '55',   'value': '55'},
00308         ]
00309 
00310         minutes = self.d.get('minutes')
00311         for i in range(13):
00312             self.assertEqual(minutes[i], data[i])
00313 
00314     def testAMPM(self):
00315         ampm = self.d.get('ampm')
00316         self.assertEqual(ampm, [])
00317 
00318 
00319 class TestDateComponentsSupportAMPM(PloneTestCase.PloneTestCase):
00320 
00321     def afterSetUp(self):
00322         date = DateTime(2002, 8, 1, 17, 42, 0)
00323         self.d = self.portal.date_components_support(date, use_ampm=1)
00324 
00325     def testElements(self):
00326         self.assertEqual(sortTuple(self.d.keys()),
00327                 ('ampm', 'days', 'hours', 'minutes', 'months', 'years'))
00328 
00329     def testHours(self):
00330         data = [
00331             {'selected': None, 'id': '--', 'value': '12'},
00332             {'selected': None, 'id': '12',   'value': '12'},
00333             {'selected': None, 'id': '01',   'value': '01'},
00334             {'selected': None, 'id': '02',   'value': '02'},
00335             {'selected': None, 'id': '03',   'value': '03'},
00336             {'selected': None, 'id': '04',   'value': '04'},
00337             {'selected': 1,    'id': '05',   'value': '05'},
00338             {'selected': None, 'id': '06',   'value': '06'},
00339             {'selected': None, 'id': '07',   'value': '07'},
00340             {'selected': None, 'id': '08',   'value': '08'},
00341             {'selected': None, 'id': '09',   'value': '09'},
00342             {'selected': None, 'id': '10',   'value': '10'},
00343             {'selected': None, 'id': '11',   'value': '11'},
00344         ]
00345 
00346         hours = self.d.get('hours')
00347         for i in range(13):
00348             self.assertEqual(hours[i], data[i])
00349 
00350     def testAM(self):
00351         d = DateTime(2002, 8, 1, 3, 0, 0)
00352         d = self.portal.date_components_support(d, use_ampm=1)
00353 
00354         data = [
00355             {'selected': None, 'id': '--', 'value': 'AM'},
00356             {'selected': 1,    'id': 'AM',   'value': 'AM'},
00357             {'selected': None, 'id': 'PM',   'value': 'PM'},
00358         ]
00359 
00360         ampm = d.get('ampm')
00361         for i in range(3):
00362             self.assertEqual(ampm[i], data[i])
00363 
00364     def testPM(self):
00365         data = [
00366             {'selected': None, 'id': '--', 'value': 'AM'},
00367             {'selected': None, 'id': 'AM',   'value': 'AM'},
00368             {'selected': 1,    'id': 'PM',   'value': 'PM'},
00369         ]
00370 
00371         ampm = self.d.get('ampm')
00372         for i in range(3):
00373             self.assertEqual(ampm[i], data[i])
00374 
00375 
00376 class TestDateComponentsSupportAMPMDefault(PloneTestCase.PloneTestCase):
00377 
00378     def afterSetUp(self):
00379         self.d = self.portal.date_components_support(None, use_ampm=1)
00380 
00381     def testElements(self):
00382         self.assertEqual(sortTuple(self.d.keys()),
00383                 ('ampm', 'days', 'hours', 'minutes', 'months', 'years'))
00384 
00385     def testHours(self):
00386         data = [
00387             {'selected': 1,    'id': '--', 'value': '12'},
00388             {'selected': None, 'id': '12',   'value': '12'},
00389             {'selected': None, 'id': '01',   'value': '01'},
00390             {'selected': None, 'id': '02',   'value': '02'},
00391             {'selected': None, 'id': '03',   'value': '03'},
00392             {'selected': None, 'id': '04',   'value': '04'},
00393             {'selected': None, 'id': '05',   'value': '05'},
00394             {'selected': None, 'id': '06',   'value': '06'},
00395             {'selected': None, 'id': '07',   'value': '07'},
00396             {'selected': None, 'id': '08',   'value': '08'},
00397             {'selected': None, 'id': '09',   'value': '09'},
00398             {'selected': None, 'id': '10',   'value': '10'},
00399             {'selected': None, 'id': '11',   'value': '11'},
00400         ]
00401 
00402         hours = self.d.get('hours')
00403         for i in range(13):
00404             self.assertEqual(hours[i], data[i])
00405 
00406     def testAMPM(self):
00407         data = [
00408             {'selected': 1,    'id': '--', 'value': 'AM'},
00409             {'selected': None, 'id': 'AM',   'value': 'AM'},
00410             {'selected': None, 'id': 'PM',   'value': 'PM'},
00411         ]
00412 
00413         ampm = self.d.get('ampm')
00414         for i in range(2):
00415             self.assertEqual(ampm[i], data[i])
00416 
00417 
00418 class TestDateComponentsSupportMinuteStepDefault(PloneTestCase.PloneTestCase):
00419 
00420     def testMinutesStep1(self):
00421         data = [
00422             {'selected': 1,    'id': '--', 'value': '00'},
00423         ]
00424         for x in range(0, 60, 1):
00425             d={'id': '%02d' % x, 'value': '%02d' % x, 'selected': None}
00426             data.append(d)
00427 
00428         d = self.portal.date_components_support(None, minute_step=1)
00429         minutes = d.get('minutes')
00430         for i in range(61):
00431             self.assertEqual(minutes[i], data[i])
00432 
00433     def testMinutesStep10(self):
00434         data = [
00435             {'selected': 1,    'id': '--', 'value': '00'},
00436         ]
00437         for x in range(0, 60, 10):
00438             d={'id': '%02d' % x, 'value': '%02d' % x, 'selected': None}
00439             data.append(d)
00440 
00441         d = self.portal.date_components_support(None, minute_step=10)
00442         minutes = d.get('minutes')
00443         for i in range(7):
00444             self.assertEqual(minutes[i], data[i])
00445 
00446 
00447 class TestSpecialCases(PloneTestCase.PloneTestCase):
00448 
00449     def testNoneUsesDefault(self):
00450         d = self.portal.date_components_support(None)
00451         hours = d.get('hours')
00452         # default == 1
00453         self.failUnless(hours[0]['selected'])
00454 
00455     def testEmptyStringUsesDefault(self):
00456         d = self.portal.date_components_support('')
00457         hours = d.get('hours')
00458         # default == 1
00459         self.failUnless(hours[0]['selected'])
00460 
00461     def testDateWithGMT(self):
00462         # Any GMT suffix gets truncated
00463         d = self.portal.date_components_support('2004/08/31 04:30:00 GMT+2')
00464         hours = d.get('hours')
00465         # default == 0
00466         self.failUnless(hours[5]['selected'])   # 4th hour
00467 
00468     def testDateOnly(self):
00469         d = self.portal.date_components_support('2004/08/31')
00470         hours = d.get('hours')
00471         # default == 0
00472         self.failUnless(hours[1]['selected'])   # 0th hour
00473 
00474     def testInvalidDateWithGMT(self):
00475         # Any GMT suffix gets truncated
00476         d = self.portal.date_components_support('2004/02/31 00:30:00 GMT+2')
00477         hours = d.get('hours')
00478         # default == 1
00479         self.failUnless(hours[0]['selected'])
00480 
00481     def testInvalidDateOnly(self):
00482         d = self.portal.date_components_support('2004/02/31')
00483         hours = d.get('hours')
00484         # default == 1
00485         self.failUnless(hours[0]['selected'])
00486 
00487 
00488 def test_suite():
00489     from unittest import TestSuite, makeSuite
00490     suite = TestSuite()
00491     suite.addTest(makeSuite(TestDateComponentsSupport))
00492     suite.addTest(makeSuite(TestDateComponentsSupportDefault))
00493     suite.addTest(makeSuite(TestDateComponentsSupportAMPM))
00494     suite.addTest(makeSuite(TestDateComponentsSupportAMPMDefault))
00495     suite.addTest(makeSuite(TestDateComponentsSupportMinuteStepDefault))
00496     suite.addTest(makeSuite(TestSpecialCases))
00497     return suite