[Zope3-checkins] SVN: Zope3/trunk/ Added zope.schema.timedelta.

Marius Gedminas marius at pov.lt
Thu Feb 10 14:08:38 EST 2005


Log message for revision 29104:
  Added zope.schema.timedelta.
  
  

Changed:
  U   Zope3/trunk/doc/CHANGES.txt
  U   Zope3/trunk/src/zope/schema/__init__.py
  U   Zope3/trunk/src/zope/schema/_field.py
  U   Zope3/trunk/src/zope/schema/interfaces.py
  A   Zope3/trunk/src/zope/schema/tests/test_timedelta.py

-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt	2005-02-10 17:48:37 UTC (rev 29103)
+++ Zope3/trunk/doc/CHANGES.txt	2005-02-10 19:08:38 UTC (rev 29104)
@@ -10,6 +10,8 @@
 
     New features
 
+      - New schema field: Timedelta.
+
       - The browser:containerViews directive provides an optional layer
         attribute.
 
@@ -386,7 +388,7 @@
 
       Jim Fulton, Fred Drake, Philipp von Weitershausen, Stephan Richter,
       Gustavo Niemeyer, Daniel Nouri, Volker Bachschneider, Roger Ineichen,
-      Shane Hathaway, Bjorn Tillenius, Garrett Smith
+      Shane Hathaway, Bjorn Tillenius, Garrett Smith, Marius Gedminas
 
       Note: If you are not listed and contributed, please add yourself. This
       note will be deleted before the release.

Modified: Zope3/trunk/src/zope/schema/__init__.py
===================================================================
--- Zope3/trunk/src/zope/schema/__init__.py	2005-02-10 17:48:37 UTC (rev 29103)
+++ Zope3/trunk/src/zope/schema/__init__.py	2005-02-10 19:08:38 UTC (rev 29104)
@@ -20,7 +20,8 @@
 from zope.schema._field import Bytes, ASCII, BytesLine
 from zope.schema._field import Text, TextLine, Bool, Int, Float
 from zope.schema._field import Tuple, List, Set
-from zope.schema._field import Password, Dict, Datetime, Date, SourceText
+from zope.schema._field import Password, Dict, Datetime, Date, Timedelta
+from zope.schema._field import SourceText
 from zope.schema._field import Object, URI, Id, DottedName
 from zope.schema._field import InterfaceField
 from zope.schema._schema import getFields, getFieldsInOrder

Modified: Zope3/trunk/src/zope/schema/_field.py
===================================================================
--- Zope3/trunk/src/zope/schema/_field.py	2005-02-10 17:48:37 UTC (rev 29103)
+++ Zope3/trunk/src/zope/schema/_field.py	2005-02-10 19:08:38 UTC (rev 29104)
@@ -1,6 +1,3 @@
-# -*- coding: ISO-8859-1 -*-
-##############################################################################
-#
 # Copyright (c) 2002 Zope Corporation and Contributors.
 # All Rights Reserved.
 #
@@ -19,7 +16,7 @@
 __docformat__ = 'restructuredtext'
 import warnings
 import re
-from datetime import datetime, date
+from datetime import datetime, date, timedelta
 from sets import Set as SetType
 
 from zope.interface import classImplements, implements, directlyProvides
@@ -32,7 +29,7 @@
 from zope.schema.interfaces import IBytes, IASCII, IBytesLine
 from zope.schema.interfaces import IBool, IInt, IFloat, IDatetime
 from zope.schema.interfaces import IChoice, ITuple, IList, ISet, IDict
-from zope.schema.interfaces import IPassword, IObject, IDate
+from zope.schema.interfaces import IPassword, IObject, IDate, ITimedelta
 from zope.schema.interfaces import IURI, IId, IFromUnicode
 from zope.schema.interfaces import ISource, IVocabulary
 
@@ -170,6 +167,11 @@
     implements(IDate)
     _type = date
 
+class Timedelta(Orderable, Field):
+    __doc__ = ITimedelta.__doc__
+    implements(ITimedelta)
+    _type = timedelta
+
 class Choice(Field):
     """Choice fields can have a value found in a constant or dynamic set of
     values given by the field definition.

Modified: Zope3/trunk/src/zope/schema/interfaces.py
===================================================================
--- Zope3/trunk/src/zope/schema/interfaces.py	2005-02-10 17:48:37 UTC (rev 29103)
+++ Zope3/trunk/src/zope/schema/interfaces.py	2005-02-10 19:08:38 UTC (rev 29104)
@@ -326,6 +326,9 @@
 class IDate(IMinMax, IField):
     u"""Field containing a date."""
 
+class ITimedelta(IMinMax, IField):
+    u"""Field containing a timedelta."""
+
 def _is_field(value):
     if not IField.providedBy(value):
         return False

Added: Zope3/trunk/src/zope/schema/tests/test_timedelta.py
===================================================================
--- Zope3/trunk/src/zope/schema/tests/test_timedelta.py	2005-02-10 17:48:37 UTC (rev 29103)
+++ Zope3/trunk/src/zope/schema/tests/test_timedelta.py	2005-02-10 19:08:38 UTC (rev 29104)
@@ -0,0 +1,95 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Timedelta Field tests
+
+$Id$
+"""
+from unittest import main, makeSuite
+from zope.schema import Timedelta
+from zope.schema.interfaces import RequiredMissing, InvalidValue
+from zope.schema.interfaces import TooSmall, TooBig
+from zope.schema.tests.test_field import FieldTestBase
+from datetime import timedelta
+
+class TimedeltaTest(FieldTestBase):
+    """Test the Timedelta Field."""
+
+    _Field_Factory = Timedelta
+
+    def testInterface(self):
+        from zope.interface.verify import verifyObject
+        from zope.schema.interfaces import ITimedelta
+        verifyObject(ITimedelta, self._Field_Factory())
+
+    def testValidate(self):
+        field = self._Field_Factory(title=u'Timedelta field', description=u'',
+                                    readonly=False, required=False)
+        field.validate(None)
+        field.validate(timedelta(minutes=15))
+
+    def testValidateRequired(self):
+        field = self._Field_Factory(title=u'Timedelta field', description=u'',
+                                    readonly=False, required=True)
+        field.validate(timedelta(minutes=15))
+
+        self.assertRaises(RequiredMissing, field.validate, None)
+
+    def testValidateMin(self):
+        t1 = timedelta(hours=2)
+        t2 = timedelta(hours=3)
+        field = self._Field_Factory(title=u'Timedelta field', description=u'',
+                                    readonly=False, required=False, min=t1)
+        field.validate(None)
+        field.validate(t1)
+        field.validate(t2)
+
+        self.assertRaises(TooSmall, field.validate, timedelta(hours=1))
+
+    def testValidateMax(self):
+        t1 = timedelta(minutes=1)
+        t2 = timedelta(minutes=2)
+        t3 = timedelta(minutes=3)
+        field = self._Field_Factory(title=u'Timedelta field', description=u'',
+                                    readonly=False, required=False, max=t2)
+        field.validate(None)
+        field.validate(t1)
+        field.validate(t2)
+
+        self.assertRaises(TooBig, field.validate, t3)
+
+    def testValidateMinAndMax(self):
+        t1 = timedelta(days=1)
+        t2 = timedelta(days=2)
+        t3 = timedelta(days=3)
+        t4 = timedelta(days=4)
+        t5 = timedelta(days=5)
+
+        field = self._Field_Factory(title=u'Timedelta field', description=u'',
+                                    readonly=False, required=False,
+                                    min=t2, max=t4)
+        field.validate(None)
+        field.validate(t2)
+        field.validate(t3)
+        field.validate(t4)
+
+        self.assertRaises(TooSmall, field.validate, t1)
+        self.assertRaises(TooBig, field.validate, t5)
+
+
+def test_suite():
+    suite = makeSuite(TimedeltaTest)
+    return suite
+
+if __name__ == '__main__':
+    main(defaultTest='test_suite')


Property changes on: Zope3/trunk/src/zope/schema/tests/test_timedelta.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native



More information about the Zope3-Checkins mailing list