From d342fa9cf90322657ab0d05b4026ddcf34ab2e6e Mon Sep 17 00:00:00 2001 From: Christian Klinger Date: Fri, 9 Oct 2009 13:11:25 +0000 Subject: [PATCH] Starting with a basic test for the DeserializationError --- buildout.cfg | 2 ++ src/z3c/schema2xml/README.txt | 57 +++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/buildout.cfg b/buildout.cfg index 0a55540..217b73c 100644 --- a/buildout.cfg +++ b/buildout.cfg @@ -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 diff --git a/src/z3c/schema2xml/README.txt b/src/z3c/schema2xml/README.txt index fcc9f66..6e9eb51 100644 --- a/src/z3c/schema2xml/README.txt +++ b/src/z3c/schema2xml/README.txt @@ -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 = ''' + ... + ... Karel + ... Titulaer + ... + ... ''' + >>> 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 +