[Zope3-checkins] CVS: Zope3/src/zope/schema/tests -
test_choice.py:1.1 test_sequencefield.py:1.1 test_setfield.py:1.1
Stephan Richter
srichter at cosmos.phy.tufts.edu
Sat Apr 24 19:19:23 EDT 2004
Update of /cvs-repository/Zope3/src/zope/schema/tests
In directory cvs.zope.org:/tmp/cvs-serv29988/src/zope/schema/tests
Added Files:
test_choice.py test_sequencefield.py test_setfield.py
Log Message:
Test new fields.
=== Added File Zope3/src/zope/schema/tests/test_choice.py ===
##############################################################################
#
# Copyright (c) 2003 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 of the Choice field.
$Id: test_choice.py,v 1.1 2004/04/24 23:19:23 srichter Exp $
"""
import unittest
from zope.schema import vocabulary
from zope.schema import Choice, Sequence
from zope.schema.interfaces import IChoiceSequence
from zope.schema.interfaces import ConstraintNotSatisfied
from zope.schema.interfaces import ValidationError
from zope.schema.interfaces import InvalidValue, NotAContainer, NotUnique
from test_vocabulary import SampleVocabulary, DummyRegistry
class SequenceAndChoiceFieldTests(unittest.TestCase):
def test_provides_IChoiceSequence(self):
sequence = Sequence(value_type=Choice(values=[1, 2, 3]))
self.assert_(IChoiceSequence.providedBy(sequence))
def test_validate(self):
sequence = Sequence(value_type=Choice(values=[1, 2, 3, 4, 5]))
sequence.validate([])
sequence.validate([1])
sequence.validate([1, 4])
sequence.validate([1, 4, 4])
self.assertRaises(NotAContainer, sequence.validate, 1)
def test_validate_unique(self):
sequence = Sequence(value_type=Choice(values=[1, 2, 3, 4, 5]),
unique=True)
sequence.validate([1, 4])
self.assertRaises(NotUnique, sequence.validate, [1, 4, 4])
class Value_ChoiceFieldTests(unittest.TestCase):
"""Tests of the Choice Field using values."""
def test_create_vocabulary(self):
choice = Choice(values=[1, 3])
self.assertEqual([term.value for term in choice.vocabulary], [1, 3])
def test_validate_int(self):
choice = Choice(values=[1, 3])
choice.validate(1)
choice.validate(3)
self.assertRaises(ConstraintNotSatisfied, choice.validate, 4)
def test_validate_string(self):
choice = Choice(values=['a', 'c'])
choice.validate('a')
choice.validate('c')
choice.validate(u'c')
self.assertRaises(ConstraintNotSatisfied, choice.validate, 'd')
def test_validate_tuple(self):
choice = Choice(values=[(1, 2), (5, 6)])
choice.validate((1, 2))
choice.validate((5, 6))
self.assertRaises(ConstraintNotSatisfied, choice.validate, [5, 6])
self.assertRaises(ConstraintNotSatisfied, choice.validate, ())
def test_validate_mixed(self):
choice = Choice(values=[1, 'b', (0.2,)])
choice.validate(1)
choice.validate('b')
choice.validate((0.2,))
self.assertRaises(ConstraintNotSatisfied, choice.validate, '1')
self.assertRaises(ConstraintNotSatisfied, choice.validate, 0.2)
class Vocabulary_ChoiceFieldTests(unittest.TestCase):
"""Tests of the Choice Field using vocabularies."""
def setUp(self):
vocabulary._clear()
def tearDown(self):
vocabulary._clear()
def check_preconstructed(self, cls, okval, badval):
v = SampleVocabulary()
field = cls(vocabulary=v)
self.assert_(field.vocabulary is v)
self.assert_(field.vocabularyName is None)
bound = field.bind(None)
self.assert_(bound.vocabulary is v)
self.assert_(bound.vocabularyName is None)
bound.default = okval
self.assertEqual(bound.default, okval)
self.assertRaises(ValidationError, setattr, bound, "default", badval)
def test_preconstructed_vocabulary(self):
self.check_preconstructed(Choice, 1, 42)
def check_constructed(self, cls, okval, badval):
vocabulary.setVocabularyRegistry(DummyRegistry())
field = cls(vocabulary="vocab")
self.assert_(field.vocabulary is None)
self.assertEqual(field.vocabularyName, "vocab")
o = object()
bound = field.bind(o)
self.assert_(isinstance(bound.vocabulary, SampleVocabulary))
bound.default = okval
self.assertEqual(bound.default, okval)
self.assertRaises(ValidationError, setattr, bound, "default", badval)
def test_constructed_vocabulary(self):
self.check_constructed(Choice, 1, 42)
def test_create_vocabulary(self):
vocabulary.setVocabularyRegistry(DummyRegistry())
field = Choice(vocabulary="vocab")
o = object()
bound = field.bind(o)
self.assertEqual([term.value for term in bound.vocabulary],
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(Vocabulary_ChoiceFieldTests))
suite.addTest(unittest.makeSuite(Value_ChoiceFieldTests))
suite.addTest(unittest.makeSuite(SequenceAndChoiceFieldTests))
return suite
if __name__ == "__main__":
unittest.main(defaultTest="test_suite")
=== Added File Zope3/src/zope/schema/tests/test_sequencefield.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.
#
##############################################################################
"""Sequence and Tuple field tests.
This set of tests exercises both Tuple and Sequence. The only
behavior Tuple adds to sequence is the restriction of the type
to 'tuple'.
$Id: test_sequencefield.py,v 1.1 2004/04/24 23:19:23 srichter Exp $
"""
from unittest import TestSuite, main, makeSuite
from zope.interface import implements
from zope.schema import Sequence
from zope.schema.interfaces import NotAContainer, RequiredMissing
from zope.schema.tests.test_field import FieldTestBase
class SequenceTest(FieldTestBase):
"""Test the Sequence Field."""
_Field_Factory = Sequence
def testValidate(self):
field = self._Field_Factory(title=u'test field', description=u'',
readonly=False, required=False)
field.validate(None)
field.validate(())
field.validate([])
field.validate('')
field.validate({})
field.validate([1, 2])
self.assertRaises(NotAContainer, field.validate, 1)
def testValidateRequired(self):
field = self._Field_Factory(title=u'test field', description=u'',
readonly=False, required=True)
field.validate([1, 2])
self.assertRaises(RequiredMissing, field.validate, None)
def test_suite():
suite = TestSuite()
suite.addTest(makeSuite(SequenceTest))
return suite
if __name__ == '__main__':
main(defaultTest='test_suite')
=== Added File Zope3/src/zope/schema/tests/test_setfield.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.
#
##############################################################################
"""Sequence and Tuple field tests.
This set of tests exercises both Tuple and Sequence. The only
behavior Tuple adds to sequence is the restriction of the type
to 'tuple'.
$Id: test_setfield.py,v 1.1 2004/04/24 23:19:23 srichter Exp $
"""
from unittest import TestSuite, main, makeSuite
from zope.schema import Set
from zope.schema.tests.test_tuplefield import TupleTest
# We could also choose the ListField as base test. It should not matter.
class SetTest(TupleTest):
"""Test the Set Field."""
_Field_Factory = Set
def test_suite():
suite = TestSuite()
suite.addTest(makeSuite(SetTest))
return suite
if __name__ == '__main__':
main(defaultTest='test_suite')
More information about the Zope3-Checkins
mailing list