[Zope3-checkins] CVS: Zope3/lib/python/Zope/Schema/tests - __init__.py:1.1 testBoolField.py:1.1 testConverter.py:1.1 testDictField.py:1.1 testField.py:1.1 testFloatField.py:1.1 testIntField.py:1.1 testListField.py:1.1 testSchema.py:1.1 testStrField.py:1.1 testTupleField.py:1.1
Jim Fulton
jim@zope.com
Thu, 5 Sep 2002 14:55:05 -0400
Update of /cvs-repository/Zope3/lib/python/Zope/Schema/tests
In directory cvs.zope.org:/tmp/cvs-serv21741/lib/python/Zope/Schema/tests
Added Files:
__init__.py testBoolField.py testConverter.py testDictField.py
testField.py testFloatField.py testIntField.py
testListField.py testSchema.py testStrField.py
testTupleField.py
Log Message:
Moved the Schema package into the Zope package.
Also cleaned up a bunch of "from x import *".
=== Added File Zope3/lib/python/Zope/Schema/tests/__init__.py ===
=== Added File Zope3/lib/python/Zope/Schema/tests/testBoolField.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: testBoolField.py,v 1.1 2002/09/05 18:55:04 jim Exp $
"""
from unittest import TestSuite, main, makeSuite
from Zope.Schema import Bool, ErrorNames
from testField import FieldTest
class BoolTest(FieldTest):
"""Test the Bool Field."""
def testValidate(self):
field = Bool(id="field", title='Bool field', description='',
readonly=0, required=0)
field.validate(None)
field.validate(1)
field.validate(0)
field.validate(10)
field.validate(-10)
def testValidateRequired(self):
field = Bool(id="field", title='Bool field', description='',
readonly=0, required=1)
field.validate(1)
field.validate(0)
self.assertRaisesErrorNames(ErrorNames.RequiredMissing,
field.validate, None)
def testAllowedValues(self):
field = Bool(id="field", title='Bool field', description='',
readonly=0, required=0, allowed_values=(0,))
field.validate(None)
field.validate(0)
self.assertRaisesErrorNames(ErrorNames.InvalidValue,
field.validate, 1)
def test_suite():
return makeSuite(BoolTest)
if __name__ == '__main__':
main(defaultTest='test_suite')
=== Added File Zope3/lib/python/Zope/Schema/tests/testConverter.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.
#
##############################################################################
"""Test conversion functionality of schema.
$Id: testConverter.py,v 1.1 2002/09/05 18:55:04 jim Exp $
"""
from unittest import TestCase, TestSuite, main, makeSuite
from Zope.Schema.Exceptions import ConversionError
from Zope.Schema.Converter import NullConverter, StrToIntConverter,\
IntToStrConverter, CombinedConverter, StrToFloatConverter,\
FloatToStrConverter
class ConverterTest(TestCase):
def test_NullConverter(self):
null_converter = NullConverter()
self.assertEquals(1, null_converter.convert(1))
self.assertEquals('foo', null_converter.convert('foo'))
def test_StrToIntConverter(self):
str_to_int_converter = StrToIntConverter()
self.assertEquals(1, str_to_int_converter.convert('1'))
self.assertEquals(100, str_to_int_converter.convert('100'))
self.assertRaises(ConversionError, str_to_int_converter.convert, 'foo')
def test_IntToStrConverter(self):
int_to_str_converter = IntToStrConverter()
self.assertEquals('1', int_to_str_converter.convert(1))
def test_StrToFloatConverter(self):
str_to_float_converter = StrToFloatConverter()
self.assertEquals(1., str_to_float_converter.convert('1.0'))
self.assertEquals(1., str_to_float_converter.convert('1'))
self.assertRaises(ConversionError, str_to_float_converter.convert,
'foo')
def test_FloatToStrConverter(self):
float_to_str_converter = FloatToStrConverter()
self.assertEquals('1.0', float_to_str_converter.convert(1.0))
def test_CombinedConverter(self):
combined_converter = CombinedConverter([StrToIntConverter(),
IntToStrConverter()])
self.assertEquals('1', combined_converter.convert('1'))
self.assertRaises(ConversionError, combined_converter.convert, 'foo')
def test_suite():
return makeSuite(ConverterTest)
if __name__=='__main__':
main(defaultTest='test_suite')
=== Added File Zope3/lib/python/Zope/Schema/tests/testDictField.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: testDictField.py,v 1.1 2002/09/05 18:55:04 jim Exp $
"""
from unittest import TestSuite, main, makeSuite
from Zope.Schema import Dict, Int, Float, ErrorNames
from testField import FieldTest
class DictTest(FieldTest):
"""Test the Dict Field."""
def testValidate(self):
field = Dict(id="field", title='Dict field',
description='', readonly=0, required=0)
field.validate(None)
field.validate({})
field.validate({1: 'foo'})
field.validate({'a': 1})
def testValidateRequired(self):
field = Dict(id="field", title='Dict field',
description='', readonly=0, required=1)
field.validate({})
field.validate({1: 'foo'})
field.validate({'a': 1})
self.assertRaisesErrorNames(ErrorNames.RequiredMissing,
field.validate, None)
def testValidateMinValues(self):
field = Dict(id="field", title='Dict field',
description='', readonly=0, required=0,
min_values=1)
field.validate(None)
field.validate({1: 'a'})
field.validate({1: 'a', 2: 'b'})
self.assertRaisesErrorNames(ErrorNames.NotEnoughElements,
field.validate, {})
def testValidateMaxValues(self):
field = Dict(id="field", title='Dict field',
description='', readonly=0, required=0,
max_values=1)
field.validate(None)
field.validate({})
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 = Dict(id="field", title='Dict field',
description='', readonly=0, required=0,
min_values=1, max_values=2)
field.validate(None)
field.validate({1: 'a'})
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 = Dict(id="field", title='Dict field',
description='', readonly=0, required=0,
value_types=(Int, Float))
field.validate(None)
field.validate({'a': 5.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 = Dict(id="field", title='Dict field',
description='', readonly=0, required=0,
key_types=(Int, Float))
field.validate(None)
field.validate({5.3: 'a'})
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 makeSuite(DictTest)
if __name__ == '__main__':
main(defaultTest='test_suite')
=== Added File Zope3/lib/python/Zope/Schema/tests/testField.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: testField.py,v 1.1 2002/09/05 18:55:04 jim Exp $
"""
from unittest import TestCase, TestSuite, main, makeSuite
from Zope.Schema import Field, IField, ErrorNames
from Zope.Schema.Exceptions import StopValidation, ValidationError
class FieldTest(TestCase):
"""Test generic Field."""
def assertRaisesErrorNames(self, error_name, f, *args, **kw):
try:
f(*args, **kw)
except ValidationError, e:
self.assertEquals(error_name, e.error_name)
return
self.fail('Expected ValidationError')
def testValidate(self):
field = Field(id="field", title='Not required field', description='',
readonly=0, required=0)
field.validate(None)
field.validate('foo')
field.validate(1)
field.validate(0)
field.validate('')
def testValidateRequired(self):
field = Field(id="field", title='Required field', description='',
readonly=0, required=1)
field.validate('foo')
field.validate(1)
field.validate(0)
field.validate('')
self.assertRaisesErrorNames(ErrorNames.RequiredMissing,
field.validate, None)
def test_suite():
return makeSuite(FieldTest)
if __name__ == '__main__':
main(defaultTest='test_suite')
=== Added File Zope3/lib/python/Zope/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/09/05 18:55:04 jim Exp $
"""
from unittest import TestSuite, main, makeSuite
from Zope.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)
field.validate(None)
field.validate(10.0)
field.validate(0.93)
field.validate(1000.0003)
def testValidateRequired(self):
field = Float(id="field", title='Float field', description='',
readonly=0, required=1)
field.validate(10.0)
field.validate(0.93)
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))
field.validate(None)
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)
field.validate(None)
field.validate(10.6)
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)
field.validate(None)
field.validate(5.3)
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)
field.validate(None)
field.validate(0)
field.validate(-0.03)
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)
field.validate(None)
field.validate(5.3)
field.validate(-9.11)
self.assertRaisesErrorNames(ErrorNames.TooManyDecimals,
field.validate, 10.511)
self.assertRaisesErrorNames(ErrorNames.TooManyDecimals,
field.validate, 20.7122)
def test_suite():
return makeSuite(FloatTest)
if __name__ == '__main__':
main(defaultTest='test_suite')
=== Added File Zope3/lib/python/Zope/Schema/tests/testIntField.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: testIntField.py,v 1.1 2002/09/05 18:55:04 jim Exp $
"""
from unittest import TestSuite, main, makeSuite
from Zope.Schema import Int, ErrorNames
from testField import FieldTest
class IntTest(FieldTest):
"""Test the Int Field."""
def testValidate(self):
field = Int(id="field", title='Int field', description='',
readonly=0, required=0)
field.validate(None)
field.validate(10)
field.validate(0)
field.validate(-1)
def testValidateRequired(self):
field = Int(id="field", title='Int field', description='',
readonly=0, required=1)
field.validate(10)
field.validate(0)
field.validate(-1)
self.assertRaisesErrorNames(ErrorNames.RequiredMissing,
field.validate, None)
def testAllowedValues(self):
field = Int(id="field", title='Int field', description='',
readonly=0, required=0, allowed_values=(-1, 2))
field.validate(None)
field.validate(2)
self.assertRaisesErrorNames(ErrorNames.InvalidValue,
field.validate, 4)
def testValidateMin(self):
field = Int(id="field", title='Int field', description='',
readonly=0, required=0, min=10)
field.validate(None)
field.validate(10)
field.validate(20)
self.assertRaisesErrorNames(ErrorNames.TooSmall, field.validate, 9)
self.assertRaisesErrorNames(ErrorNames.TooSmall, field.validate, -10)
def testValidateMax(self):
field = Int(id="field", title='Int field', description='',
readonly=0, required=0, max=10)
field.validate(None)
field.validate(5)
field.validate(9)
self.assertRaisesErrorNames(ErrorNames.TooBig, field.validate, 11)
self.assertRaisesErrorNames(ErrorNames.TooBig, field.validate, 20)
def testValidateMinAndMax(self):
field = Int(id="field", title='Int field', description='',
readonly=0, required=0, min=0, max=10)
field.validate(None)
field.validate(0)
field.validate(5)
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 makeSuite(IntTest)
if __name__ == '__main__':
main(defaultTest='test_suite')
=== Added File Zope3/lib/python/Zope/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/09/05 18:55:04 jim Exp $
"""
from unittest import TestSuite, main, makeSuite
from Zope.Schema import List, Int, 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)
field.validate(None)
field.validate([])
field.validate([1, 2])
field.validate([3,])
def testValidateRequired(self):
field = List(id="field", title='List field', description='',
readonly=0, required=1)
field.validate([])
field.validate([1, 2])
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)
field.validate(None)
field.validate([1, 2])
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)
field.validate(None)
field.validate([])
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)
field.validate(None)
field.validate([1, ])
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=(Int, Float))
field.validate(None)
field.validate([5.3,])
field.validate([2, 2.3])
self.assertRaisesErrorNames(ErrorNames.WrongContainedType,
field.validate, ['',] )
self.assertRaisesErrorNames(ErrorNames.WrongContainedType,
field.validate, [2, ''] )
def test_suite():
return makeSuite(ListTest)
if __name__ == '__main__':
main(defaultTest='test_suite')
=== Added File Zope3/lib/python/Zope/Schema/tests/testSchema.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: testSchema.py,v 1.1 2002/09/05 18:55:04 jim Exp $
"""
from unittest import TestCase, TestSuite, main, makeSuite
from Zope.Schema.Exceptions import StopValidation, ValidationError, \
ValidationErrorsAll
from Interface import Interface
from Zope.Schema import Str, ErrorNames
from Zope.Schema import validateMapping, validateMappingAll, getFields
class ISchemaTest(Interface):
title = Str(
title="Title",
description="Title",
default="",
required=1)
description = Str(
title="Description",
description="Description",
default="",
required=1)
spam = Str(
title="Spam",
description="Spam",
default="",
required=1)
class SchemaTest(TestCase):
def testValidateMapping(self):
dict = {'title': 'A title',
'description': 'A particular description.',
'spam': 'Spam'}
try:
validateMapping(ISchemaTest, dict)
except ValidationError, e:
self.fail("Unexpected ValidationError: %s" % e.error_name)
def testValidateBadMapping(self):
dict = {'title': 'A title'}
self.assertRaises(ValidationError, validateMapping, ISchemaTest, dict)
def testValidateMappingAll(self):
dict = {'title': 'A title',
'description': 'A particular description.',
'spam': 'Spam',
}
try:
validateMappingAll(ISchemaTest, dict)
except ValidationErrorsAll:
self.fail("Unexpected ValidationErrors")
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)
return
self.fail('Expected ValidationErrors, but none detected')
def test_getFields(self):
fields = getFields(ISchemaTest)
self.assert_(fields.has_key('title'))
self.assert_(fields.has_key('description'))
self.assert_(fields.has_key('spam'))
# test whether getName() has the right value
for key, value in fields.iteritems():
self.assertEquals(key, value.getName())
def test_suite():
return makeSuite(SchemaTest)
if __name__ == '__main__':
main(defaultTest='test_suite')
=== Added File Zope3/lib/python/Zope/Schema/tests/testStrField.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: testStrField.py,v 1.1 2002/09/05 18:55:04 jim Exp $
"""
from unittest import TestSuite, main, makeSuite
from Zope.Schema import Str, ErrorNames
from testField import FieldTest
class StrTest(FieldTest):
"""Test the Str Field."""
def testValidate(self):
field = Str(id='field', title='Str field', description='',
readonly=0, required=0)
field.validate(None)
field.validate('foo')
field.validate('')
def testValidateRequired(self):
field = Str(id='field', title='Str field', description='',
readonly=0, required=1)
field.validate('foo')
self.assertRaisesErrorNames(ErrorNames.RequiredMissing,
field.validate, None)
self.assertRaisesErrorNames(ErrorNames.RequiredEmptyStr,
field.validate, '')
def testAllowedValues(self):
field = Str(id="field", title='Str field', description='',
readonly=0, required=0, allowed_values=('foo', 'bar'))
field.validate(None)
field.validate('foo')
self.assertRaisesErrorNames(ErrorNames.InvalidValue,
field.validate, 'blah')
def testValidateMinLength(self):
field = Str(id='field', title='Str field', description='',
readonly=0, required=0, min_length=3)
field.validate(None)
field.validate('333')
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 = Str(id='field', title='Str field', description='',
readonly=0, required=0, max_length=5)
field.validate(None)
field.validate('')
field.validate('333')
field.validate('55555')
self.assertRaisesErrorNames(ErrorNames.TooLong, field.validate,
'666666')
self.assertRaisesErrorNames(ErrorNames.TooLong, field.validate,
'999999999')
def testValidateMinLengthAndMaxLength(self):
field = Str(id='field', title='Str field', description='',
readonly=0, required=0, min_length=3, max_length=5)
field.validate(None)
field.validate('333')
field.validate('4444')
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 test_suite():
return makeSuite(StrTest)
if __name__ == '__main__':
main(defaultTest='test_suite')
=== Added File Zope3/lib/python/Zope/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/09/05 18:55:04 jim Exp $
"""
from unittest import TestSuite, main, makeSuite
from Zope.Schema import Tuple, Int, 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)
field.validate(None)
field.validate(())
field.validate((1, 2))
field.validate((3,))
def testValidateRequired(self):
field = Tuple(id="field", title='Tuple field', description='',
readonly=0, required=1)
field.validate(())
field.validate((1, 2))
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)
field.validate(None)
field.validate((1, 2))
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)
field.validate(None)
field.validate(())
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)
field.validate(None)
field.validate((1, ))
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=(Int, Float))
field.validate(None)
field.validate((5.3,))
field.validate((2, 2.3))
self.assertRaisesErrorNames(ErrorNames.WrongContainedType,
field.validate, ('',) )
self.assertRaisesErrorNames(ErrorNames.WrongContainedType,
field.validate, (2, '') )
def test_suite():
return makeSuite(TupleTest)
if __name__ == '__main__':
main(defaultTest='test_suite')