Changeset 5314


Ignore:
Timestamp:
26 Jul 2010, 12:33:05 (14 years ago)
Author:
uli
Message:

Add tests for date converter.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/branches/ulif-fasttables/src/waeup/sirp/utils/converters.txt

    r5140 r5314  
    267267       validation checks are disabled with this converter. This speeds
    268268       up things as expensive lookups are avoided.
     269
     270
     271:class:`DateConverter`
     272----------------------
     273
     274.. autoclass:: waeup.sirp.utils.converters.DateConverter
     275   :members:
     276   :inherited-members:
    269277
    270278
     
    682690
    683691A more complex (but still realistic example) for conversion of
    684 :mode:`zope.schema.Choice` fields follows. Here we have a special kind
     692:mod:`zope.schema.Choice` fields follows. Here we have a special kind
    685693of object, the `Cave` class, and get their `name` attribute as token.
    686694
     
    739747    >>> converter.toString(obj2)
    740748    'Fred'
     749
     750Date field converters
     751---------------------
     752
     753We create a field that also implements ``IDate``, a regular
     754``Date`` field:
     755
     756    >>> from zope.schema import Date
     757    >>> from datetime import date
     758    >>> field = Date(title=u'Some date',
     759    ...             default=date(2010, 4, 1),
     760    ...             required=True)
     761
     762Now we can get a converter for this field by explicitly creating a
     763:class:`IntConverter` instance:
     764
     765    >>> from waeup.sirp.utils.converters import DateConverter
     766    >>> converter = DateConverter(field)
     767
     768Or we can just grab a registered adapter:
     769
     770    >>> from waeup.sirp.interfaces import ISchemaTypeConverter
     771    >>> converter = ISchemaTypeConverter(field)
     772
     773This will select the correct converter for us automatically.
     774
     775    >>> converter
     776    <waeup.sirp.utils.converters.DateConverter object at 0x...>
     777
     778Now we can convert strings to this type:
     779
     780    >>> converter.fromString('2007-04-23')
     781    datetime.date(2007, 4, 23)
     782
     783And back:
     784
     785    >>> converter.toString(date(2007,4,23))
     786    '2007-04-23'
     787
     788As string input we also accept 'DD/MM/YYYY':
     789
     790    >>> converter.fromString('13/05/1945')
     791    datetime.date(1945, 5, 13)
     792
     793and some more bizarre formats:
     794
     795    >>> converter.fromString('1945/05/13')
     796    datetime.date(1945, 5, 13)
     797
     798    >>> converter.fromString('1945/05/13 12:12:34 GMT+1')
     799    datetime.date(1945, 5, 13)
     800
     801    >>> converter.fromString('1945/5/6')
     802    datetime.date(1945, 5, 6)
     803
     804    >>> converter.fromString('06.05.1945')
     805    datetime.date(1945, 5, 6)
     806
     807    >>> converter.fromString('6.5.1945')
     808    datetime.date(1945, 5, 6)
     809
     810while other date formats will mean trouble:
     811
     812    >>> converter.fromString('first of July 1960')
     813    Traceback (most recent call last):
     814    ...
     815    ValueError: Cannot convert to date: first of July 1960. Use YYYY-MM-DD.
     816
     817Okay, not very surprising. But the field definitions can help also
     818deliver values, if the given value is missing:
     819
     820    >>> converter.fromString(None)
     821    datetime.date(2010, 4, 1)
     822
     823``None`` is not an acceptable value for fields which are required but
     824provide no default:
     825
     826    >>> field = Date(title=u'Some Title',
     827    ...             required=True)
     828    >>> converter = ISchemaTypeConverter(field)
     829    >>> converter.fromString(None)
     830    Traceback (most recent call last):
     831    ...
     832    RequiredMissing
     833
     834If we want to avoid this type of exception (and risk non-applicable
     835data to be stored), we can use the ``strict`` parameter of
     836``fromString()``:
     837
     838    >>> converter.fromString(None, strict=False) is None
     839    True
     840
     841The same for the inverse operation:
     842
     843    >>> converter.toString(None)
     844    Traceback (most recent call last):
     845    ...
     846    RequiredMissing
     847    >>> converter.toString(None, strict=False) is None
     848    True
     849
     850
     851If a field is not required, we will get the ``missing_value`` type in
     852same case:
     853
     854    >>> field = Date(title=u'Some Title',
     855    ...             required=False)
     856    >>> converter = ISchemaTypeConverter(field)
     857    >>> converter.fromString(None) is None
     858    True
     859
     860    >>> converter.toString(None) is None
     861    True
Note: See TracChangeset for help on using the changeset viewer.