Skip to content
This repository has been archived by the owner on Feb 12, 2020. It is now read-only.

Commit

Permalink
Starting with a basic test for the DeserializationError
Browse files Browse the repository at this point in the history
  • Loading branch information
goschtl committed Oct 9, 2009
1 parent 52eaf67 commit d342fa9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions buildout.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ versions = versions
[versions]
lxml = 2.0.9
zope.testing = 3.6.0
zope.intid = 3.7.0
zope.container = 3.8.1

[test]
recipe = zc.recipe.testrunner
Expand Down
57 changes: 57 additions & 0 deletions src/z3c/schema2xml/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,60 @@ Set fields are very similar to List fields::
>>> deserialize(xml, IWithSet, new_set)
>>> new_set.set
set(['alpha', 'beta'])


Deserialization with errors
===========================

We define the IName interface again but with constraints:

>>> from zope.schema.fieldproperty import FieldProperty
>>> from z3c.schema2xml import DeserializationError

>>> class INameConstraint(interface.Interface):
... first_name = schema.TextLine(title=u'First name', max_length=3)
... last_name = schema.TextLine(title=u'Last name')

Our change the implementation of our content class. We use now
Field properties to reflect the changes form our interface.

>>> class NameConstraint(object):
... implements(IName)
... first_name = FieldProperty(INameConstraint['first_name'])
... last_name = FieldProperty(INameConstraint['last_name'])
... def __init__(self, first_name, last_name):
... self.first_name = first_name
... self.last_name = last_name


>>> xml = '''
... <container>
... <first_name>Karel</first_name>
... <last_name>Titulaer</last_name>
... </container>
... '''
>>> name = NameConstraint(u'', u'')

We try to deserialize against the constraint and
we get an DeserializationError:

>>> deserialize(xml, INameConstraint, name)
Traceback (most recent call last):
...
DeserializationError

Let's catch the error.

>>> try:
... deserialize(xml, INameConstraint, name)
... except DeserializationError, e:
... pass

We get detailed information which value has an error.

>>> key, value = e.field_errors.values()[0]
>>> key
Karel 3

>>> value
<Element first_name at ...>

0 comments on commit d342fa9

Please sign in to comment.