[Zope-Checkins] CVS: Zope3/lib/python/Schema/tests - testBooleanField.py:1.1 testDictionaryField.py:1.1 testFloatField.py:1.1 testIntegerField.py:1.1 testListField.py:1.1 testStringField.py:1.1 testTupleField.py:1.1 testField.py:1.4 testSchema.py:1.4
Stephan Richter
srichter@cbu.edu
Sun, 14 Jul 2002 09:33:24 -0400
Update of /cvs-repository/Zope3/lib/python/Schema/tests
In directory cvs.zope.org:/tmp/cvs-serv5853/Schema/tests
Modified Files:
testField.py testSchema.py
Added Files:
testBooleanField.py testDictionaryField.py testFloatField.py
testIntegerField.py testListField.py testStringField.py
testTupleField.py
Log Message:
- I finished the Schema code. We now have the following Fields defined
by default: String, Boolean, Integer, Float, Tuple, List, Dictionary
- I also reworked the Validator in a major way. A Validator now does only
one thing, such as check that a value has the right type. The Validators
are then put into a ContainerValidator which runs through all the
specified Validators. BTW, I hope you like the code... I do. :)
- Rewrote and added new tests. Note that there are no explicit tests for
the Validators, since they are tested already through the Field validate
method.
- Started working on Forms. There is a whole lot of work to be done there.
More checkins on that later...
=== Added File Zope3/lib/python/Schema/tests/testBooleanField.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.
#
##############################################################################
"""
$Id: testBooleanField.py,v 1.1 2002/07/14 13:32:53 srichter Exp $
"""
from unittest import TestSuite, main, makeSuite
from Schema import Boolean, ErrorNames
from testField import FieldTest
class BooleanTest(FieldTest):
"""Test the Boolean Field."""
def testValidate(self):
field = Boolean(id="field", title='Boolean field', description='',
readonly=0, required=0)
self.assertEqual(None, field.validate(None))
self.assertEqual(1, field.validate(1))
self.assertEqual(0, field.validate(0))
self.assertEqual(10, field.validate(10))
self.assertEqual(-10, field.validate(-10))
def testValidateRequired(self):
field = Boolean(id="field", title='Boolean field', description='',
readonly=0, required=1)
self.assertEqual(1, field.validate(1))
self.assertEqual(0, field.validate(0))
self.assertRaisesErrorNames(ErrorNames.RequiredMissing,
field.validate, None)
def testAllowedValues(self):
field = Boolean(id="field", title='Boolean field', description='',
readonly=0, required=0, allowed_values=(0,))
self.assertEqual(None, field.validate(None))
self.assertEqual(0, field.validate(0))
self.assertRaisesErrorNames(ErrorNames.InvalidValue,
field.validate, 1)
def test_suite():
return TestSuite((
makeSuite(BooleanTest),
))
if __name__ == '__main__':
main(defaultTest='test_suite')
=== Added File Zope3/lib/python/Schema/tests/testDictionaryField.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.
#
##############################################################################
"""
$Id: testDictionaryField.py,v 1.1 2002/07/14 13:32:53 srichter Exp $
"""
from unittest import TestSuite, main, makeSuite
from Schema import Dictionary, Integer, Float, ErrorNames
from testField import FieldTest
class DictionaryTest(FieldTest):
"""Test the Dictionary Field."""
def testValidate(self):
field = Dictionary(id="field", title='Dictionary field',
description='', readonly=0, required=0)
self.assertEqual(None, field.validate(None))
self.assertEqual( {} , field.validate({}))
self.assertEqual( {1: 'foo'} , field.validate( {1: 'foo'} ))
self.assertEqual( {'a': 1} , field.validate( {'a': 1} ))
def testValidateRequired(self):
field = Dictionary(id="field", title='Dictionary field',
description='', readonly=0, required=1)
self.assertEqual( {} , field.validate({}))
self.assertEqual( {1: 'foo'} , field.validate( {1: 'foo'} ))
self.assertEqual( {'a': 1} , field.validate( {'a': 1} ))
self.assertRaisesErrorNames(ErrorNames.RequiredMissing,
field.validate, None)
def testValidateMinValues(self):
field = Dictionary(id="field", title='Dictionary field',
description='', readonly=0, required=0,
min_values=1)
self.assertEqual(None, field.validate(None))
self.assertEqual( {1: 'a'}, field.validate( {1: 'a'} ))
self.assertEqual( {1: 'a', 2: 'b'}, field.validate( {1: 'a', 2: 'b'} ))
self.assertRaisesErrorNames(ErrorNames.NotEnoughElements,
field.validate, {})
def testValidateMaxValues(self):
field = Dictionary(id="field", title='Dictionary field',
description='', readonly=0, required=0,
max_values=1)
self.assertEqual(None, field.validate(None))
self.assertEqual( {}, field.validate( {} ))
self.assertEqual( {1: 'a'}, field.validate( {1: 'a'} ))
self.assertRaisesErrorNames(ErrorNames.TooManyElements,
field.validate, {1: 'a', 2: 'b'})
self.assertRaisesErrorNames(ErrorNames.TooManyElements,
field.validate, {1: 'a', 2: 'b', 3: 'c'})
def testValidateMinValuesAndMaxValues(self):
field = Dictionary(id="field", title='Dictionary field',
description='', readonly=0, required=0,
min_values=1, max_values=2)
self.assertEqual(None, field.validate(None))
self.assertEqual( {1: 'a'}, field.validate( {1: 'a'} ))
self.assertEqual( {1: 'a', 2: 'b'}, field.validate( {1: 'a', 2: 'b'} ))
self.assertRaisesErrorNames(ErrorNames.NotEnoughElements,
field.validate, {})
self.assertRaisesErrorNames(ErrorNames.TooManyElements,
field.validate, {1: 'a', 2: 'b', 3: 'c'})
def testValidateValueTypes(self):
field = Dictionary(id="field", title='Dictionary field',
description='', readonly=0, required=0,
value_types=(Integer, Float))
self.assertEqual(None, field.validate(None))
self.assertEqual({'a': 5.3} , field.validate({'a': 5.3}))
self.assertEqual({'a': 2, 'b': 2.3},
field.validate( {'a': 2, 'b': 2.3} ))
self.assertRaisesErrorNames(ErrorNames.WrongContainedType,
field.validate, {1: ''} )
self.assertRaisesErrorNames(ErrorNames.WrongContainedType,
field.validate, {'a': ()} )
def testValidateKeyTypes(self):
field = Dictionary(id="field", title='Dictionary field',
description='', readonly=0, required=0,
key_types=(Integer, Float))
self.assertEqual(None, field.validate(None))
self.assertEqual({5.3: 'a'} , field.validate({5.3: 'a'}))
self.assertEqual({2: 'a', 2.3: 'b'},
field.validate( {2: 'a', 2.3: 'b'} ))
self.assertRaisesErrorNames(ErrorNames.WrongContainedType,
field.validate, {'': 1} )
self.assertRaisesErrorNames(ErrorNames.WrongContainedType,
field.validate, {(): 'a'} )
def test_suite():
return TestSuite((
makeSuite(DictionaryTest),
))
if __name__ == '__main__':
main(defaultTest='test_suite')
=== Added File Zope3/lib/python/Schema/tests/testFloatField.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.
#
##############################################################################
"""
$Id: testFloatField.py,v 1.1 2002/07/14 13:32:53 srichter Exp $
"""
from unittest import TestSuite, main, makeSuite
from Schema import Float, ErrorNames
from testField import FieldTest
class FloatTest(FieldTest):
"""Test the Float Field."""
def testValidate(self):
field = Float(id="field", title='Float field', description='',
readonly=0, required=0)
self.assertEqual(None, field.validate(None))
self.assertEqual(10.0, field.validate(10.0))
self.assertEqual(0.93, field.validate(0.93))
self.assertEqual(1000.0003, field.validate(1000.0003))
def testValidateRequired(self):
field = Float(id="field", title='Float field', description='',
readonly=0, required=1)
self.assertEqual(10.0, field.validate(10.0))
self.assertEqual(0.93, field.validate(0.93))
self.assertEqual(1000.0003, field.validate(1000.0003))
self.assertRaisesErrorNames(ErrorNames.RequiredMissing,
field.validate, None)
def testAllowedValues(self):
field = Float(id="field", title='Integer field', description='',
readonly=0, required=0, allowed_values=(0.1, 2.6))
self.assertEqual(None, field.validate(None))
self.assertEqual(2.6, field.validate(2.6))
self.assertRaisesErrorNames(ErrorNames.InvalidValue,
field.validate, -5.4)
def testValidateMin(self):
field = Float(id="field", title='Float field', description='',
readonly=0, required=0, min=10.5)
self.assertEqual(None, field.validate(None))
self.assertEqual(10.6, field.validate(10.6))
self.assertEqual(20.2, field.validate(20.2))
self.assertRaisesErrorNames(ErrorNames.TooSmall, field.validate, -9)
self.assertRaisesErrorNames(ErrorNames.TooSmall, field.validate, 10.4)
def testValidateMax(self):
field = Float(id="field", title='Float field', description='',
readonly=0, required=0, max=10.5)
self.assertEqual(None, field.validate(None))
self.assertEqual(5.3, field.validate(5.3))
self.assertEqual(-9.1, field.validate(-9.1))
self.assertRaisesErrorNames(ErrorNames.TooBig, field.validate, 10.51)
self.assertRaisesErrorNames(ErrorNames.TooBig, field.validate, 20.7)
def testValidateMinAndMax(self):
field = Float(id="field", title='Float field', description='',
readonly=0, required=0, min=-0.6, max=10.1)
self.assertEqual(None, field.validate(None))
self.assertEqual(0, field.validate(0))
self.assertEqual(-0.03, field.validate(-0.03))
self.assertEqual(10.0001, field.validate(10.0001))
self.assertRaisesErrorNames(ErrorNames.TooSmall, field.validate, -10)
self.assertRaisesErrorNames(ErrorNames.TooSmall, field.validate, -1.6)
self.assertRaisesErrorNames(ErrorNames.TooBig, field.validate, 11.45)
self.assertRaisesErrorNames(ErrorNames.TooBig, field.validate, 20.02)
def testValidateDecimals(self):
field = Float(id="field", title='Float field', description='',
readonly=0, required=0, decimals=2)
self.assertEqual(None, field.validate(None))
self.assertEqual(5.3, field.validate(5.3))
self.assertEqual(-9.11, field.validate(-9.11))
self.assertRaisesErrorNames(ErrorNames.TooManyDecimals,
field.validate, 10.511)
self.assertRaisesErrorNames(ErrorNames.TooManyDecimals,
field.validate, 20.7122)
def test_suite():
return TestSuite((
makeSuite(FloatTest),
))
if __name__ == '__main__':
main(defaultTest='test_suite')
=== Added File Zope3/lib/python/Schema/tests/testIntegerField.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.
#
##############################################################################
"""
$Id: testIntegerField.py,v 1.1 2002/07/14 13:32:53 srichter Exp $
"""
from unittest import TestSuite, main, makeSuite
from Schema import Integer, ErrorNames
from testField import FieldTest
class IntegerTest(FieldTest):
"""Test the Integer Field."""
def testValidate(self):
field = Integer(id="field", title='Integer field', description='',
readonly=0, required=0)
self.assertEqual(None, field.validate(None))
self.assertEqual(10, field.validate(10))
self.assertEqual(0, field.validate(0))
self.assertEqual(-1, field.validate(-1))
def testValidateRequired(self):
field = Integer(id="field", title='Integer field', description='',
readonly=0, required=1)
self.assertEqual(10, field.validate(10))
self.assertEqual(0, field.validate(0))
self.assertEqual(-1, field.validate(-1))
self.assertRaisesErrorNames(ErrorNames.RequiredMissing,
field.validate, None)
def testAllowedValues(self):
field = Integer(id="field", title='Integer field', description='',
readonly=0, required=0, allowed_values=(-1, 2))
self.assertEqual(None, field.validate(None))
self.assertEqual(2, field.validate(2))
self.assertRaisesErrorNames(ErrorNames.InvalidValue,
field.validate, 4)
def testValidateMin(self):
field = Integer(id="field", title='Integer field', description='',
readonly=0, required=0, min=10)
self.assertEqual(None, field.validate(None))
self.assertEqual(10, field.validate(10))
self.assertEqual(20, field.validate(20))
self.assertRaisesErrorNames(ErrorNames.TooSmall, field.validate, 9)
self.assertRaisesErrorNames(ErrorNames.TooSmall, field.validate, -10)
def testValidateMax(self):
field = Integer(id="field", title='Integer field', description='',
readonly=0, required=0, max=10)
self.assertEqual(None, field.validate(None))
self.assertEqual(5, field.validate(5))
self.assertEqual(9, field.validate(9))
self.assertRaisesErrorNames(ErrorNames.TooBig, field.validate, 11)
self.assertRaisesErrorNames(ErrorNames.TooBig, field.validate, 20)
def testValidateMinAndMax(self):
field = Integer(id="field", title='Integer field', description='',
readonly=0, required=0, min=0, max=10)
self.assertEqual(None, field.validate(None))
self.assertEqual(0, field.validate(0))
self.assertEqual(5, field.validate(5))
self.assertEqual(10, field.validate(10))
self.assertRaisesErrorNames(ErrorNames.TooSmall, field.validate, -10)
self.assertRaisesErrorNames(ErrorNames.TooSmall, field.validate, -1)
self.assertRaisesErrorNames(ErrorNames.TooBig, field.validate, 11)
self.assertRaisesErrorNames(ErrorNames.TooBig, field.validate, 20)
def test_suite():
return TestSuite((
makeSuite(IntegerTest),
))
if __name__ == '__main__':
main(defaultTest='test_suite')
=== Added File Zope3/lib/python/Schema/tests/testListField.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.
#
##############################################################################
"""
$Id: testListField.py,v 1.1 2002/07/14 13:32:53 srichter Exp $
"""
from unittest import TestSuite, main, makeSuite
from Schema import List, Integer, Float, ErrorNames
from testField import FieldTest
class ListTest(FieldTest):
"""Test the List Field."""
def testValidate(self):
field = List(id="field", title='List field', description='',
readonly=0, required=0)
self.assertEqual(None, field.validate(None))
self.assertEqual( [] , field.validate([]))
self.assertEqual( [1, 2] , field.validate( [1, 2] ))
self.assertEqual( [3,] , field.validate( [3,] ))
def testValidateRequired(self):
field = List(id="field", title='List field', description='',
readonly=0, required=1)
self.assertEqual( [] , field.validate([]))
self.assertEqual( [1, 2] , field.validate( [1, 2] ))
self.assertEqual( [3,] , field.validate( [3,] ))
self.assertRaisesErrorNames(ErrorNames.RequiredMissing,
field.validate, None)
def testValidateMinValues(self):
field = List(id="field", title='List field', description='',
readonly=0, required=0, min_values=2)
self.assertEqual(None, field.validate(None))
self.assertEqual( [1, 2], field.validate( [1, 2] ))
self.assertEqual( [1, 2, 3], field.validate( [1, 2, 3] ))
self.assertRaisesErrorNames(ErrorNames.NotEnoughElements,
field.validate, [])
self.assertRaisesErrorNames(ErrorNames.NotEnoughElements,
field.validate, [1,])
def testValidateMaxValues(self):
field = List(id="field", title='List field', description='',
readonly=0, required=0, max_values=2)
self.assertEqual(None, field.validate(None))
self.assertEqual( [], field.validate( [] ))
self.assertEqual( [1, 2], field.validate( [1, 2] ))
self.assertRaisesErrorNames(ErrorNames.TooManyElements,
field.validate, [1, 2, 3, 4])
self.assertRaisesErrorNames(ErrorNames.TooManyElements,
field.validate, [1, 2, 3])
def testValidateMinValuesAndMaxValues(self):
field = List(id="field", title='List field', description='',
readonly=0, required=0, min_values=1, max_values=2)
self.assertEqual(None, field.validate(None))
self.assertEqual( [1, ], field.validate( [1, ] ))
self.assertEqual( [1, 2], field.validate( [1, 2] ))
self.assertRaisesErrorNames(ErrorNames.NotEnoughElements,
field.validate, [])
self.assertRaisesErrorNames(ErrorNames.TooManyElements,
field.validate, [1, 2, 3])
def testValidateValueTypes(self):
field = List(id="field", title='List field', description='',
readonly=0, required=0, value_types=(Integer, Float))
self.assertEqual(None, field.validate(None))
self.assertEqual( [5.3,] , field.validate( [5.3,] ))
self.assertEqual( [2, 2.3], field.validate( [2, 2.3] ))
self.assertRaisesErrorNames(ErrorNames.WrongContainedType,
field.validate, ['',] )
self.assertRaisesErrorNames(ErrorNames.WrongContainedType,
field.validate, [2, ''] )
def test_suite():
return TestSuite((
makeSuite(ListTest),
))
if __name__ == '__main__':
main(defaultTest='test_suite')
=== Added File Zope3/lib/python/Schema/tests/testStringField.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.
#
##############################################################################
"""
$Id: testStringField.py,v 1.1 2002/07/14 13:32:53 srichter Exp $
"""
from unittest import TestSuite, main, makeSuite
from Schema import String, ErrorNames
from testField import FieldTest
class StringTest(FieldTest):
"""Test the String Field."""
def testValidate(self):
field = String(id='field', title='String field', description='',
readonly=0, required=0)
self.assertEqual(None, field.validate(None))
self.assertEqual('foo', field.validate('foo'))
self.assertEqual('', field.validate(''))
def testValidateRequired(self):
field = String(id='field', title='String field', description='',
readonly=0, required=1)
self.assertEqual('foo', field.validate('foo'))
self.assertRaisesErrorNames(ErrorNames.RequiredMissing,
field.validate, None)
self.assertRaisesErrorNames(ErrorNames.RequiredEmptyString,
field.validate, '')
def testAllowedValues(self):
field = String(id="field", title='String field', description='',
readonly=0, required=0, allowed_values=('foo', 'bar'))
self.assertEqual(None, field.validate(None))
self.assertEqual('foo', field.validate('foo'))
self.assertRaisesErrorNames(ErrorNames.InvalidValue,
field.validate, 'blah')
def testValidateMinLength(self):
field = String(id='field', title='String field', description='',
readonly=0, required=0, min_length=3)
self.assertEqual(None, field.validate(None))
self.assertEqual('333', field.validate('333'))
self.assertEqual('55555', field.validate('55555'))
self.assertRaisesErrorNames(ErrorNames.TooShort, field.validate, '')
self.assertRaisesErrorNames(ErrorNames.TooShort, field.validate, '22')
self.assertRaisesErrorNames(ErrorNames.TooShort, field.validate, '1')
def testValidateMaxLength(self):
field = String(id='field', title='String field', description='',
readonly=0, required=0, max_length=5)
self.assertEqual(None, field.validate(None))
self.assertEqual('', field.validate(''))
self.assertEqual('333', field.validate('333'))
self.assertEqual('55555', field.validate('55555'))
self.assertRaisesErrorNames(ErrorNames.TooLong, field.validate,
'666666')
self.assertRaisesErrorNames(ErrorNames.TooLong, field.validate,
'999999999')
def testValidateMinLengthAndMaxLength(self):
field = String(id='field', title='String field', description='',
readonly=0, required=0, min_length=3, max_length=5)
self.assertEqual(None, field.validate(None))
self.assertEqual('333', field.validate('333'))
self.assertEqual('4444', field.validate('4444'))
self.assertEqual('55555', field.validate('55555'))
self.assertRaisesErrorNames(ErrorNames.TooShort, field.validate, '22')
self.assertRaisesErrorNames(ErrorNames.TooShort, field.validate, '22')
self.assertRaisesErrorNames(ErrorNames.TooLong, field.validate,
'666666')
self.assertRaisesErrorNames(ErrorNames.TooLong, field.validate,
'999999999')
def testValidateWhiteSpace(self):
field = String(id='field', title='String field', description='',
readonly=0, required=0, whitespaces="preserve")
self.assertEqual(None, field.validate(None))
self.assertEqual('sample', field.validate('sample'))
self.assertEqual(' sample ', field.validate(' sample '))
self.assertEqual(' sam\tple ', field.validate(' sam\tple '))
field = String(id='field', title='String field', description='',
readonly=0, required=0, whitespaces="replace")
self.assertEqual(None, field.validate(None))
self.assertEqual('sample', field.validate('sample'))
self.assertEqual(' sample ', field.validate(' sample '))
self.assertEqual(' sam ple ', field.validate(' sam\tple '))
self.assertEqual(' sam ple ', field.validate(' sam\rple '))
self.assertEqual(' sam ple ', field.validate(' sam\nple '))
field = String(id='field', title='String field', description='',
readonly=0, required=0, whitespaces="collapse")
self.assertEqual(None, field.validate(None))
self.assertEqual('sample', field.validate('sample'))
self.assertEqual('sample', field.validate(' sample '))
self.assertEqual('sam ple', field.validate(' sam\tple '))
field = String(id='field', title='String field', description='',
readonly=0, required=0, whitespaces="strip")
self.assertEqual(None, field.validate(None))
self.assertEqual('sample', field.validate('sample'))
self.assertEqual('sample', field.validate(' sample '))
self.assertEqual('sam\tple', field.validate(' sam\tple '))
def test_suite():
return TestSuite((
makeSuite(StringTest),
))
if __name__ == '__main__':
main(defaultTest='test_suite')
=== Added File Zope3/lib/python/Schema/tests/testTupleField.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.
#
##############################################################################
"""
$Id: testTupleField.py,v 1.1 2002/07/14 13:32:53 srichter Exp $
"""
from unittest import TestSuite, main, makeSuite
from Schema import Tuple, Integer, Float, ErrorNames
from testField import FieldTest
class TupleTest(FieldTest):
"""Test the Tuple Field."""
def testValidate(self):
field = Tuple(id="field", title='Tuple field', description='',
readonly=0, required=0)
self.assertEqual(None, field.validate(None))
self.assertEqual( () , field.validate(()))
self.assertEqual( (1, 2) , field.validate( (1, 2) ))
self.assertEqual( (3,) , field.validate( (3,) ))
def testValidateRequired(self):
field = Tuple(id="field", title='Tuple field', description='',
readonly=0, required=1)
self.assertEqual( () , field.validate(()))
self.assertEqual( (1, 2) , field.validate( (1, 2) ))
self.assertEqual( (3,) , field.validate( (3,) ))
self.assertRaisesErrorNames(ErrorNames.RequiredMissing,
field.validate, None)
def testValidateMinValues(self):
field = Tuple(id="field", title='Tuple field', description='',
readonly=0, required=0, min_values=2)
self.assertEqual(None, field.validate(None))
self.assertEqual( (1, 2), field.validate( (1, 2) ))
self.assertEqual( (1, 2, 3), field.validate( (1, 2, 3) ))
self.assertRaisesErrorNames(ErrorNames.NotEnoughElements,
field.validate, ())
self.assertRaisesErrorNames(ErrorNames.NotEnoughElements,
field.validate, (1,))
def testValidateMaxValues(self):
field = Tuple(id="field", title='Tuple field', description='',
readonly=0, required=0, max_values=2)
self.assertEqual(None, field.validate(None))
self.assertEqual( (), field.validate( () ))
self.assertEqual( (1, 2), field.validate( (1, 2) ))
self.assertRaisesErrorNames(ErrorNames.TooManyElements,
field.validate, (1, 2, 3, 4))
self.assertRaisesErrorNames(ErrorNames.TooManyElements,
field.validate, (1, 2, 3))
def testValidateMinValuesAndMaxValues(self):
field = Tuple(id="field", title='Tuple field', description='',
readonly=0, required=0, min_values=1, max_values=2)
self.assertEqual(None, field.validate(None))
self.assertEqual( (1, ), field.validate( (1, ) ))
self.assertEqual( (1, 2), field.validate( (1, 2) ))
self.assertRaisesErrorNames(ErrorNames.NotEnoughElements,
field.validate, ())
self.assertRaisesErrorNames(ErrorNames.TooManyElements,
field.validate, (1, 2, 3))
def testValidateValueTypes(self):
field = Tuple(id="field", title='Tuple field', description='',
readonly=0, required=0, value_types=(Integer, Float))
self.assertEqual(None, field.validate(None))
self.assertEqual( (5.3,) , field.validate( (5.3,) ))
self.assertEqual( (2, 2.3), field.validate( (2, 2.3) ))
self.assertRaisesErrorNames(ErrorNames.WrongContainedType,
field.validate, ('',) )
self.assertRaisesErrorNames(ErrorNames.WrongContainedType,
field.validate, (2, '') )
def test_suite():
return TestSuite((
makeSuite(TupleTest),
))
if __name__ == '__main__':
main(defaultTest='test_suite')
=== Zope3/lib/python/Schema/tests/testField.py 1.3 => 1.4 ===
#
##############################################################################
"""
-
$Id$
"""
-
from unittest import TestCase, TestSuite, main, makeSuite
-
+from Schema import Field, IField, ErrorNames
from Schema.Exceptions import StopValidation, ValidationError
-from Schema import Field, Int, Str, Bool
-from Schema import IField
-from Schema import ErrorNames
-class FieldTestCase(TestCase):
+class FieldTest(TestCase):
+ """Test generic Field."""
+
def assertRaisesErrorNames(self, error_name, f, *args, **kw):
try:
f(*args, **kw)
@@ -33,236 +30,29 @@
return
self.fail('Expected ValidationError')
- def test_validate(self):
- field = Field(
- title='Not required field',
- description='',
- readonly=0,
- required=0)
- try:
- field.validate(None)
- field.validate('foo')
- field.validate(1)
- field.validate(0)
- field.validate('')
- except ValidationError, e:
- self.fail('Expected no ValidationError, but we got %s.' % e.error_name)
-
- def test_validate_required(self):
- field = Field(
- title='Required field',
- description='',
- readonly=0,
- required=1)
- self.assertRaisesErrorNames(ErrorNames.RequiredMissing, field.validate, None)
- try:
- field.validate('foo')
- field.validate(1)
- field.validate(0)
- field.validate('')
- except ValidationError, e:
- self.fail('Expected no ValidationError, but we got %s.' % e.error_name)
-
-class StrTestCase(FieldTestCase):
- def test_validate(self):
- field = Str(
- title='Str field',
- description='',
- readonly=0,
- required=0)
- try:
- field.validate(None)
- field.validate('foo')
- field.validate('')
- except ValidationError, e:
- self.fail('Expected no ValidationError, but we got %s.' % e.error_name)
-
- def test_validate_required(self):
- field = Str(
- title='Str field required',
- description='',
- readonly=0,
- required=1)
- self.assertRaisesErrorNames(ErrorNames.RequiredMissing, field.validate, None)
- try:
- field.validate('foo')
- except ValidationError, e:
- self.fail('Expected no ValidationError, but we got %s.' % e.error_name)
- self.assertRaises(ValidationError, field.validate, '')
-
- def test_validate_min(self):
- field = Str(
- title='Str field',
- description='',
- readonly=0,
- required=0,
- min_length=3)
- try:
- field.validate(None)
- field.validate('333')
- field.validate('55555')
- except ValidationError, e:
- self.fail('Expected no ValidationError, but we got %s.' % e.error_name)
-
- self.assertRaisesErrorNames(ErrorNames.TooShort, field.validate, '')
- self.assertRaisesErrorNames(ErrorNames.TooShort, field.validate, '22')
- self.assertRaisesErrorNames(ErrorNames.TooShort, field.validate, '1')
-
- def test_validate_max(self):
- field = Str(
- title='Str field',
- description='',
- readonly=0,
- required=0,
- max_length=5)
- try:
- field.validate(None)
- field.validate('')
- field.validate('333')
- field.validate('55555')
- except ValidationError, e:
- self.fail('Expected no ValidationError, but we got %s.' % e.error_name)
-
- self.assertRaisesErrorNames(ErrorNames.TooLong, field.validate, '666666')
- self.assertRaisesErrorNames(ErrorNames.TooLong, field.validate, '999999999')
-
- def test_validate_min_max(self):
- field = Str(
- title='Str field',
- description='',
- readonly=0,
- required=0,
- min_length=3,
- max_length=5)
- try:
- field.validate(None)
- field.validate('333')
- field.validate('4444')
- field.validate('55555')
- except ValidationError, e:
- self.fail('Expected no ValidationError, but we got %s.' % e.error_name)
-
- self.assertRaisesErrorNames(ErrorNames.TooShort, field.validate, '22')
- self.assertRaisesErrorNames(ErrorNames.TooShort, field.validate, '22')
- self.assertRaisesErrorNames(ErrorNames.TooLong, field.validate, '666666')
- self.assertRaisesErrorNames(ErrorNames.TooLong, field.validate, '999999999')
-
-
-class BoolTestCase(FieldTestCase):
- def test_validate(self):
- field = Bool(
- title='Bool field',
- description='',
- readonly=0,
- required=0)
- try:
- field.validate(None)
- field.validate(1)
- field.validate(0)
- field.validate(10)
- field.validate(-10)
- except ValidationError, e:
- self.fail('Expected no ValidationError, but we got %s.' % e.error_name)
-
- def test_validate(self):
- field = Bool(
- title='Bool field required',
- description='',
- readonly=0,
- required=1)
- self.assertRaisesErrorNames(ErrorNames.RequiredMissing, field.validate, None)
-
-class IntTestCase(FieldTestCase):
- def test_validate(self):
- field = Int(
- title='Int field',
- description='',
- readonly=0,
- required=0)
- try:
- field.validate(None)
- field.validate(10)
- field.validate(0)
- field.validate(-1)
- except ValidationError, e:
- self.fail('Expected no ValidationError, but we got %s.' % e.error_name)
-
- def test_validate_required(self):
- field = Int(
- title='Int field required',
- description='',
- readonly=0,
- required=1)
- self.assertRaisesErrorNames(ErrorNames.RequiredMissing, field.validate, None)
-
- try:
- field.validate(10)
- field.validate(0)
- field.validate(-1)
- except ValidationError, e:
- self.fail('Expected no ValidationError, but we got %s.' % e.error_name)
-
- def test_validate_min(self):
- field = Int(
- title='Int field',
- description='',
- readonly=0,
- required=0,
- min=10)
- try:
- field.validate(None)
- field.validate(10)
- field.validate(20)
- except ValidationError, e:
- self.fail('Expected no ValidationError, but we got %s.' % e.error_name)
-
- self.assertRaisesErrorNames(ErrorNames.TooSmall, field.validate, 9)
- self.assertRaisesErrorNames(ErrorNames.TooSmall, field.validate, -10)
-
- def test_validate_max(self):
- field = Int(
- title='Int field',
- description='',
- readonly=0,
- required=0,
- max=10)
- try:
- field.validate(None)
- field.validate(5)
- field.validate(9)
- except ValidationError, e:
- self.fail('Expected no ValidationError, but we got %s.' % e.error_name)
-
- self.assertRaisesErrorNames(ErrorNames.TooBig, field.validate, 11)
- self.assertRaisesErrorNames(ErrorNames.TooBig, field.validate, 20)
-
- def test_validate_min_max(self):
- field = Int(
- title='Int field',
- description='',
- readonly=0,
- required=0,
- min=0,
- max=10)
- try:
- field.validate(None)
- field.validate(0)
- field.validate(5)
- field.validate(10)
- except ValidationError, e:
- self.fail('Expected no ValidationError, but we got %s.' % e.error_name)
+ def testValidate(self):
+ field = Field(id="field", title='Not required field', description='',
+ readonly=0, required=0)
+ self.assertEqual(None, field.validate(None))
+ self.assertEqual('foo', field.validate('foo'))
+ self.assertEqual(1, field.validate(1))
+ self.assertEqual(0, field.validate(0))
+ self.assertEqual('', field.validate(''))
+
+ def testValidateRequired(self):
+ field = Field(id="field", title='Required field', description='',
+ readonly=0, required=1)
+ self.assertEqual('foo', field.validate('foo'))
+ self.assertEqual(1, field.validate(1))
+ self.assertEqual(0, field.validate(0))
+ self.assertEqual('', field.validate(''))
+ self.assertRaisesErrorNames(ErrorNames.RequiredMissing,
+ field.validate, None)
- self.assertRaisesErrorNames(ErrorNames.TooSmall, field.validate, -10)
- self.assertRaisesErrorNames(ErrorNames.TooSmall, field.validate, -1)
- self.assertRaisesErrorNames(ErrorNames.TooBig, field.validate, 11)
- self.assertRaisesErrorNames(ErrorNames.TooBig, field.validate, 20)
def test_suite():
return TestSuite((
- makeSuite(FieldTestCase),
- makeSuite(StrTestCase),
- makeSuite(BoolTestCase),
- makeSuite(IntTestCase),
+ makeSuite(FieldTest),
))
if __name__ == '__main__':
=== Zope3/lib/python/Schema/tests/testSchema.py 1.3 => 1.4 ===
#
##############################################################################
"""
-
$Id$
"""
-
from unittest import TestCase, TestSuite, main, makeSuite
+from Schema.Exceptions import StopValidation, ValidationError, \
+ ValidationErrorsAll
+from Schema import *
+
+
+class ISchemaTest(Schema):
+ title = String(id="title",
+ title="Title",
+ description="Title",
+ default="",
+ required=1)
+
+ description = String(id="description",
+ title="Description",
+ description="Description",
+ default="",
+ required=1)
+
+ spam = String(id="spam",
+ title="Spam",
+ description="Spam",
+ default="",
+ required=1)
+
+
+class SchemaTest(TestCase):
-from Schema.Exceptions import StopValidation, ValidationError, ValidationErrorsAll
-from Schema._Schema import validateMapping,validateMappingAll
-from Schema import ErrorNames
-from Interface import Interface
-import Schema
-
-
-class ISchemaTest(Interface):
- title = Schema.Str(
- title="Title",
- description="Title",
- default="",
- required=1,
- )
- description = Schema.Str(
- title="Description",
- description="Description",
- default="",
- required=1,
- )
- spam = Schema.Str(
- title="Spam",
- description="Spam",
- default="",
- required=1,
- )
-
-class SchemaTestCase(TestCase):
- def test_validateMapping(self):
- dict = {
- 'title': 'A title',
+ def testValidateMapping(self):
+ dict = {'title': 'A title',
'description': 'A particular description.',
- 'spam': 'Spam',
- }
- try:
- validateMapping(ISchemaTest, dict)
- except ValidationError:
- self.fail()
+ 'spam': 'Spam'}
+ self.assertEqual(1, validateMapping(ISchemaTest, dict))
- def test_validateBadMapping(self):
+ def testValidateBadMapping(self):
dict = {'title': 'A title'}
self.assertRaises(ValidationError, validateMapping, ISchemaTest, dict)
- def test_validateMappingAll(self):
+
+ def testValidateMappingAll(self):
dict = {'title': 'A title',
'description': 'A particular description.',
'spam': 'Spam',
}
- try:
- validateMappingAll(ISchemaTest, dict)
- except ValidationErrorsAll:
- self.fail()
+ self.assertEqual(1, validateMappingAll(ISchemaTest, dict))
def test_validateBadMappingAll(self):
dict = {'title': 'A title'}
-
try:
validateMappingAll(ISchemaTest, dict)
except ValidationErrorsAll, e:
error=ValidationError(ErrorNames.RequiredMissing)
self.assertEqual(e.errors ,
[('description',error), ('spam',error)])
- self.assertRaises(ValidationError, validateMapping, ISchemaTest, dict)
+ self.assertRaises(ValidationError, validateMapping, ISchemaTest,
+ dict)
return
self.fail()
+
def test_suite():
- return TestSuite((makeSuite(SchemaTestCase), ))
+ return TestSuite((makeSuite(SchemaTest), ))
if __name__ == '__main__':
main(defaultTest='test_suite')