[Zope3-checkins] CVS: Zope3/src/zope/schema/tests -
test_objectfield.py:1.1
Dominik Huber
cvs-admin at zope.org
Mon Nov 24 12:03:05 EST 2003
Update of /cvs-repository/Zope3/src/zope/schema/tests
In directory cvs.zope.org:/tmp/cvs-serv9649
Added Files:
test_objectfield.py
Log Message:
_field.py
======
update class Object(Field):
-----------------------------------------
__init__: first set self.schema and then call the super class
_validate: add schema check
update function _validate_fields:
---------------------------------------------------
- iterate through schema.fields instead of iterating through the value.__dict__
tests/test_objectfield.py
===================
added unittest
errornames.py
===========
added two new Errors: SchemaNotFullyImplemented, SchemaNotProvided
=== Added File Zope3/src/zope/schema/tests/test_objectfield.py ===
##############################################################################
#
# 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.0 (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.
#
##############################################################################
"""
This set of tests exercises Objects.
$Id: test_objectfield.py,v 1.1 2003/11/24 17:03:05 dominikhuber Exp $
"""
from unittest import TestSuite, main, makeSuite
from zope.i18n import MessageIDFactory
from zope.interface import Interface, implements
from zope.schema import Object, TextLine
from zope.schema import errornames
from zope.schema.fieldproperty import FieldProperty
from zope.schema.interfaces import ValidationError
from zope.schema.tests.test_field import FieldTestBase
_ = MessageIDFactory('zope')
class ITestSchema(Interface):
"""A test schema"""
foo = TextLine(
title=_(u"Foo"),
description=_(u"Foo description"),
default=u"",
required=True)
bar = TextLine(
title=_(u"Bar"),
description=_(u"Bar description"),
default=u"",
required=False)
class TestClass(object):
implements(ITestSchema)
_foo = u''
_bar = u''
def getfoo(self):
return self._foo
def setfoo(self, value):
self._foo = value
foo = property(getfoo, setfoo, None, u'foo')
def getbar(self):
return self._bar
def setbar(self, value):
self._bar = value
bar = property(getbar, setbar, None, u'foo')
class FieldPropertyTestClass(object):
implements(ITestSchema)
foo = FieldProperty(ITestSchema['foo'])
bar = FieldProperty(ITestSchema['bar'])
class NotFullyImplementedTestClass(object):
implements(ITestSchema)
foo = FieldProperty(ITestSchema['foo'])
# bar = FieldProperty(ITestSchema['bar']): bar is not implemented
class ObjectTest(FieldTestBase):
"""Test the Object Field."""
def getErrors(self, error_name, f, *args, **kw):
try:
f(*args, **kw)
except ValidationError, e:
try:
return e[1]
except:
return []
self.fail('Expected ValidationError')
def makeTestObject(self, **kw):
kw['schema'] = kw.get('schema', Interface)
return Object(**kw)
_Field_Factory = makeTestObject
def makeTestData(self):
return TestClass()
def makeFieldPropertyTestClass(self):
return FieldPropertyTestClass()
def makeNotFullyImplementedTestData(self):
return NotFullyImplementedTestClass()
def invalidSchemas(self):
return ['foo', 1, 0, {}, [], None]
def validSchemas(self):
return [Interface, ITestSchema]
def test_init(self):
for schema in self.validSchemas():
field = Object(schema=schema)
for schema in self.invalidSchemas():
self.assertRaises(ValidationError, Object, schema=schema)
self.assertRaisesErrorNames(errornames.WrongType, Object, schema=schema)
def testValidate(self):
# this test of the base class is not applicable
pass
def testValidateRequired(self):
# this test of the base class is not applicable
pass
def test_validate_required(self):
field = self._Field_Factory(
title=u'Required field', description=u'',
readonly=False, required=True)
self.assertRaisesErrorNames(errornames.RequiredMissing,
field.validate, None)
def test_validate_TestData(self):
field = self.makeTestObject(schema=ITestSchema, required=False)
data = self.makeTestData()
field.validate(data)
field = self.makeTestObject(schema=ITestSchema)
field.validate(data)
data.foo = None
self.assertRaises(ValidationError, field.validate, data)
self.assertRaisesErrorNames(errornames.WrongContainedType, field.validate, data)
errors = self.getErrors(errornames.WrongContainedType, field.validate, data)
self.assertEqual(str(errors[0]), errornames.RequiredMissing)
def test_validate_FieldPropertyTestData(self):
field = self.makeTestObject(schema=ITestSchema, required=False)
data = self.makeFieldPropertyTestClass()
field.validate(data)
field = self.makeTestObject(schema=ITestSchema)
field.validate(data)
self.assertRaises(ValidationError, setattr, data, 'foo', None)
self.assertRaisesErrorNames(errornames.RequiredMissing, setattr, data, 'foo', None)
def test_validate_NotFullyImplementedTestData(self):
field = self.makeTestObject(schema=ITestSchema, required=False)
data = self.makeNotFullyImplementedTestData()
self.assertRaises(ValidationError, field.validate, data)
self.assertRaisesErrorNames(errornames.WrongContainedType, field.validate, data)
errors = self.getErrors(errornames.WrongContainedType, field.validate, data)
self.assertEqual(errors[0], errornames.SchemaNotFullyImplemented)
def test_suite():
suite = TestSuite()
suite.addTest(makeSuite(ObjectTest))
return suite
if __name__ == '__main__':
main(defaultTest='test_suite')
More information about the Zope3-Checkins
mailing list