[Zope3-checkins] CVS: Zope3/src/zope/schema/tests - test_vocabulary.py:1.11

Fred L. Drake, Jr. fred@zope.com
Mon, 16 Jun 2003 14:54:31 -0400


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

Modified Files:
	test_vocabulary.py 
Log Message:
Make sure min_length and max_length are properly honored for multi-selection
vocabulary fields.


=== Zope3/src/zope/schema/tests/test_vocabulary.py 1.10 => 1.11 ===
--- Zope3/src/zope/schema/tests/test_vocabulary.py:1.10	Fri Jun 13 11:33:35 2003
+++ Zope3/src/zope/schema/tests/test_vocabulary.py	Mon Jun 16 14:54:31 2003
@@ -154,6 +154,56 @@
         self.check_constructed_vocabulary_multi_default(
             vocabulary.VocabularyUniqueListField)
 
+    def check_min_length_ok(self, v, cls):
+        field = cls(vocabulary=v, min_length=1)
+        self.assertEqual(field.min_length, 1)
+        field.validate([0])
+
+    def test_min_length_ok(self):
+        v = SampleVocabulary()
+        self.check_min_length_ok(v, vocabulary.VocabularyBagField)
+        self.check_min_length_ok(v, vocabulary.VocabularyListField)
+        self.check_min_length_ok(v, vocabulary.VocabularySetField)
+        self.check_min_length_ok(v, vocabulary.VocabularyUniqueListField)
+
+    def check_min_length_short(self, v, cls):
+        field = cls(vocabulary=v, min_length=1)
+        self.assertEqual(field.min_length, 1)
+        self.assertRaises(interfaces.ValidationError,
+                          field.validate, [])
+
+    def test_min_length_short(self):
+        v = SampleVocabulary()
+        self.check_min_length_short(v, vocabulary.VocabularyBagField)
+        self.check_min_length_short(v, vocabulary.VocabularyListField)
+        self.check_min_length_short(v, vocabulary.VocabularySetField)
+        self.check_min_length_short(v, vocabulary.VocabularyUniqueListField)
+
+    def check_max_length_ok(self, v, cls):
+        field = cls(vocabulary=v, min_length=2)
+        self.assertEqual(field.min_length, 2)
+        field.validate([0, 1])
+
+    def test_max_length_ok(self):
+        v = SampleVocabulary()
+        self.check_max_length_ok(v, vocabulary.VocabularyBagField)
+        self.check_max_length_ok(v, vocabulary.VocabularyListField)
+        self.check_max_length_ok(v, vocabulary.VocabularySetField)
+        self.check_max_length_ok(v, vocabulary.VocabularyUniqueListField)
+
+    def check_max_length_long(self, v, cls):
+        field = cls(vocabulary=v, max_length=2)
+        self.assertEqual(field.max_length, 2)
+        self.assertRaises(interfaces.ValidationError,
+                          field.validate, [0, 1, 2])
+
+    def test_max_length_long(self):
+        v = SampleVocabulary()
+        self.check_max_length_long(v, vocabulary.VocabularyBagField)
+        self.check_max_length_long(v, vocabulary.VocabularyListField)
+        self.check_max_length_long(v, vocabulary.VocabularySetField)
+        self.check_max_length_long(v, vocabulary.VocabularyUniqueListField)
+
 
 class SimpleVocabularyTests(unittest.TestCase):