Ignore:
Timestamp:
15 Jan 2010, 12:02:14 (15 years ago)
Author:
uli
Message:

Add tests for BoolConverter?.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • waeup/branches/ulif-importers/src/waeup/utils/converters.txt

    r4816 r4817  
    294294    True
    295295
     296Bool field converters
     297---------------------
     298
     299We create a field that also implements ``IBool``, a regular
     300``Bool`` field:
     301
     302    >>> from zope.schema import Bool
     303    >>> field = Bool(title=u'Some truth',
     304    ...             default=True,
     305    ...             required=True)
     306
     307Now we can get a converter for this field by explicitly creating a
     308:class:`BoolConverter` instance:
     309
     310    >>> from waeup.utils.converters import BoolConverter
     311    >>> converter = BoolConverter(field)
     312
     313Or we can just grab a registered adapter:
     314
     315    >>> from waeup.interfaces import ISchemaTypeConverter
     316    >>> converter = ISchemaTypeConverter(field)
     317
     318This will select the correct converter for us automatically.
     319
     320    >>> converter
     321    <waeup.utils.converters.BoolConverter object at 0x...>
     322
     323Now we can convert strings to this type:
     324
     325    >>> converter.fromString('yes')
     326    True
     327
     328    >>> converter.fromString('Yes')
     329    True
     330
     331    >>> converter.fromString('yEs')
     332    True
     333
     334    >>> converter.fromString('1')
     335    True
     336
     337    >>> converter.fromString('True')
     338    True
     339
     340    >>> converter.fromString('0')
     341    False
     342
     343    >>> converter.fromString('no')
     344    False
     345
     346    >>> converter.fromString('false')
     347    False
     348
     349
     350And back:
     351
     352    >>> converter.toString(True)
     353    '1'
     354
     355    >>> converter.toString(False)
     356    '0'
     357
     358Okay, not very surprising. But the field definitions can help also
     359deliver values, if the given value is missing:
     360
     361    >>> converter.fromString(None)
     362    True
     363
     364``None`` is not an acceptable value for fields which are required but
     365provide no default:
     366
     367    >>> field = Bool(title=u'Some Truth',
     368    ...             required=True)
     369
     370    >>> converter = ISchemaTypeConverter(field)
     371    >>> converter.fromString(None)
     372    Traceback (most recent call last):
     373    ...
     374    RequiredMissing
     375
     376If we want to avoid this type of exception (and risk non-applicable
     377data to be stored), we can use the ``strict`` parameter of
     378``fromString()``:
     379
     380    >>> converter.fromString(None, strict=False) is None
     381    True
     382
     383The same for the inverse operation:
     384
     385    >>> converter.toString(None)
     386    Traceback (most recent call last):
     387    ...
     388    RequiredMissing
     389
     390    >>> converter.toString(None, strict=False) is None
     391    True
     392
     393
     394If a field is not required, we will get the ``missing_value`` type in
     395same case:
     396
     397    >>> field = Bool(title=u'Some Title',
     398    ...             required=False)
     399
     400    >>> converter = ISchemaTypeConverter(field)
     401    >>> converter.fromString(None) is None
     402    True
     403
     404    >>> converter.toString(None) is None
     405    True
     406
    296407
    297408Int field converters
     
    307418
    308419Now we can get a converter for this field by explicitly creating a
    309 :class:`TextConverter` instance:
     420:class:`IntConverter` instance:
    310421
    311422    >>> from waeup.utils.converters import IntConverter
Note: See TracChangeset for help on using the changeset viewer.