[Zope3-checkins] CVS: Zope3/src/zope/schema/tests - test_choice.py:1.2 test_listfield.py:1.7 test_setfield.py:1.2 test_tuplefield.py:1.9 test_sequencefield.py:NONE

Gary Poster gary at zope.com
Thu May 6 12:13:51 EDT 2004


Update of /cvs-repository/Zope3/src/zope/schema/tests
In directory cvs.zope.org:/tmp/cvs-serv9758/src/zope/schema/tests

Modified Files:
	test_choice.py test_listfield.py test_setfield.py 
	test_tuplefield.py 
Removed Files:
	test_sequencefield.py 
Log Message:
Convert the field collection behavior as described in 
http://mail.zope.org/pipermail/zope3-dev/2004-May/010797.html

The Sequence field is removed.  As I spoke with Stephan, it may be acceptable to add the Sequence field back in if it actually means something.  Sequence did mean it was iterable but container access API was not described.  It should be described.  IPythonSequence might describe this API.  A Sequence widget should require specification of the factory.

Set now specifies a sets.Set.

IChoiceSequence was removed.




=== Zope3/src/zope/schema/tests/test_choice.py 1.1 => 1.2 ===
--- Zope3/src/zope/schema/tests/test_choice.py:1.1	Sat Apr 24 19:19:23 2004
+++ Zope3/src/zope/schema/tests/test_choice.py	Thu May  6 12:13:50 2004
@@ -18,34 +18,13 @@
 import unittest
 
 from zope.schema import vocabulary
-from zope.schema import Choice, Sequence
-from zope.schema.interfaces import IChoiceSequence
+from zope.schema import Choice
 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."""
@@ -135,7 +114,6 @@
     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__":


=== Zope3/src/zope/schema/tests/test_listfield.py 1.6 => 1.7 ===
--- Zope3/src/zope/schema/tests/test_listfield.py:1.6	Sun Apr 11 06:35:17 2004
+++ Zope3/src/zope/schema/tests/test_listfield.py	Thu May  6 12:13:50 2004
@@ -15,8 +15,13 @@
 $Id$
 """
 from unittest import main, makeSuite
-from zope.schema import List, Int
-from zope.schema.interfaces import RequiredMissing, WrongContainedType
+
+from zope.interface import implements
+from zope.schema import Field, List, Int
+from zope.schema.interfaces import IField
+from zope.schema.interfaces import ICollection, ISequence, IList
+from zope.schema.interfaces import NotAContainer, RequiredMissing
+from zope.schema.interfaces import WrongContainedType, WrongType, NotUnique
 from zope.schema.interfaces import TooShort, TooLong
 from zope.schema.tests.test_field import FieldTestBase
 
@@ -83,6 +88,32 @@
 
         self.assertRaises(WrongContainedType, field.validate, ['',] )
         self.assertRaises(WrongContainedType, field.validate, [3.14159,] )
+
+    def testCorrectValueType(self):
+        # allow value_type of None (XXX)
+        List(value_type=None)
+
+        # do not allow arbitrary value types
+        self.assertRaises(ValueError, List, value_type=object())
+        self.assertRaises(ValueError, List, value_type=Field)
+
+        # however, allow anything that implements IField
+        List(value_type=Field())
+        class FakeField:
+            implements(IField)
+        List(value_type=FakeField())
+
+    def testUnique(self):
+        field = self._Field_Factory(title=u'test field', description=u'',
+                                    readonly=False, required=True, unique=True)
+        field.validate([1, 2])
+        self.assertRaises(NotUnique, field.validate, [1, 2, 1])
+    
+    def testImplements(self):
+        field = List()
+        self.failUnless(IList.providedBy(field))
+        self.failUnless(ISequence.providedBy(field))
+        self.failUnless(ICollection.providedBy(field))
 
 def test_suite():
     return makeSuite(ListTest)


=== Zope3/src/zope/schema/tests/test_setfield.py 1.1 => 1.2 ===
--- Zope3/src/zope/schema/tests/test_setfield.py:1.1	Sat Apr 24 19:19:23 2004
+++ Zope3/src/zope/schema/tests/test_setfield.py	Thu May  6 12:13:50 2004
@@ -11,23 +11,117 @@
 # 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'.
+"""Set field tests.
 
 $Id$
 """
 from unittest import TestSuite, main, makeSuite
-from zope.schema import Set
-from zope.schema.tests.test_tuplefield import TupleTest
+import sets
+
+from zope.interface import implements, providedBy
+from zope.schema import Field, Set, Int
+from zope.schema.interfaces import IField
+from zope.schema.interfaces import ICollection, IUnorderedCollection, ISet
+from zope.schema.interfaces import NotAContainer, RequiredMissing
+from zope.schema.interfaces import WrongContainedType, WrongType, NotUnique
+from zope.schema.interfaces import TooShort, TooLong
+from zope.schema.tests.test_field import FieldTestBase
 
-# We could also choose the ListField as base test. It should not matter.
-class SetTest(TupleTest):
-    """Test the Set Field."""
+class SetTest(FieldTestBase):
+    """Test the Tuple Field."""
 
     _Field_Factory = Set
+
+    def testValidate(self):
+        field = Set(title=u'Set field', description=u'',
+                    readonly=False, required=False)
+        field.validate(None)
+        field.validate(sets.Set())
+        field.validate(sets.Set((1, 2)))
+        field.validate(sets.Set((3,)))
+
+        self.assertRaises(WrongType, field.validate, [1, 2, 3])
+        self.assertRaises(WrongType, field.validate, 'abc')
+        self.assertRaises(WrongType, field.validate, 1)
+        self.assertRaises(WrongType, field.validate, {})
+        self.assertRaises(WrongType, field.validate, (1, 2, 3))
+
+    def testValidateRequired(self):
+        field = Set(title=u'Set field', description=u'',
+                    readonly=False, required=True)
+        field.validate(sets.Set())
+        field.validate(sets.Set((1, 2)))
+        field.validate(sets.Set((3,)))
+
+        self.assertRaises(RequiredMissing, field.validate, None)
+
+    def testValidateMinValues(self):
+        field = Set(title=u'Set field', description=u'',
+                    readonly=False, required=False, min_length=2)
+        field.validate(None)
+        field.validate(sets.Set((1, 2)))
+        field.validate(sets.Set((1, 2, 3)))
+
+        self.assertRaises(TooShort, field.validate, sets.Set(()))
+        self.assertRaises(TooShort, field.validate, sets.Set((3,)))
+
+    def testValidateMaxValues(self):
+        field = Set(title=u'Set field', description=u'',
+                    readonly=False, required=False, max_length=2)
+        field.validate(None)
+        field.validate(sets.Set())
+        field.validate(sets.Set((1, 2)))
+
+        self.assertRaises(TooLong, field.validate, sets.Set((1, 2, 3, 4)))
+        self.assertRaises(TooLong, field.validate, sets.Set((1, 2, 3)))
+
+    def testValidateMinValuesAndMaxValues(self):
+        field = Set(title=u'Set field', description=u'',
+                    readonly=False, required=False,
+                    min_length=1, max_length=2)
+        field.validate(None)
+        field.validate(sets.Set((3,)))
+        field.validate(sets.Set((1, 2)))
+
+        self.assertRaises(TooShort, field.validate, sets.Set())
+        self.assertRaises(TooLong, field.validate, sets.Set((1, 2, 3)))
+
+    def testValidateValueTypes(self):
+        field = Set(title=u'Set field', description=u'',
+                    readonly=False, required=False,
+                    value_type=Int())
+        field.validate(None)
+        field.validate(sets.Set((5,)))
+        field.validate(sets.Set((2, 3)))
+
+        self.assertRaises(WrongContainedType, field.validate, sets.Set(('',)))
+        self.assertRaises(WrongContainedType, 
+                          field.validate, sets.Set((3.14159,)))
+
+    def testCorrectValueType(self):
+        # allow value_type of None (XXX)
+        Set(value_type=None)
+
+        # do not allow arbitrary value types
+        self.assertRaises(ValueError, Set, value_type=object())
+        self.assertRaises(ValueError, Set, value_type=Field)
+
+        # however, allow anything that implements IField
+        Set(value_type=Field())
+        class FakeField:
+            implements(IField)
+        Set(value_type=FakeField())
+    
+    def testNoUniqueArgument(self):
+        self.assertRaises(TypeError, Set, unique=False)
+        self.assertRaises(TypeError, Set, unique=True)
+        self.failUnless(Set().unique)
+    
+    def testImplements(self):
+        field = Set()
+        self.failUnless(ISet.providedBy(field))
+        self.failUnless(IUnorderedCollection.providedBy(field))
+        self.failUnless(ICollection.providedBy(field))
 
 def test_suite():
     suite = TestSuite()


=== Zope3/src/zope/schema/tests/test_tuplefield.py 1.8 => 1.9 ===
--- Zope3/src/zope/schema/tests/test_tuplefield.py:1.8	Sat Apr 24 19:21:06 2004
+++ Zope3/src/zope/schema/tests/test_tuplefield.py	Thu May  6 12:13:50 2004
@@ -11,19 +11,16 @@
 # 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'.
+"""Tuple field tests.
 
 $Id$
 """
 from unittest import TestSuite, main, makeSuite
 
 from zope.interface import implements
-from zope.schema import Field, Sequence, Tuple, Int
+from zope.schema import Field, Tuple, Int
 from zope.schema.interfaces import IField
+from zope.schema.interfaces import ICollection, ISequence, ITuple
 from zope.schema.interfaces import NotAContainer, RequiredMissing
 from zope.schema.interfaces import WrongContainedType, WrongType, NotUnique
 from zope.schema.interfaces import TooShort, TooLong
@@ -117,6 +114,12 @@
                                     readonly=False, required=True, unique=True)
         field.validate((1, 2))
         self.assertRaises(NotUnique, field.validate, (1, 2, 1))
+    
+    def testImplements(self):
+        field = Tuple()
+        self.failUnless(ITuple.providedBy(field))
+        self.failUnless(ISequence.providedBy(field))
+        self.failUnless(ICollection.providedBy(field))
 
 def test_suite():
     suite = TestSuite()

=== Removed File Zope3/src/zope/schema/tests/test_sequencefield.py ===




More information about the Zope3-Checkins mailing list