[Zope3-checkins] CVS: Zope3/src/zope/schema/tests - states.py:1.2.2.1 tabcomplete.py:1.2.2.1 test_accessors.py:1.2.4.1 test_states.py:1.2.2.1 test_tabcomplete.py:1.2.2.1 test_vocabulary.py:1.2.2.1

Grégoire Weber zope@i-con.ch
Sun, 22 Jun 2003 10:24:27 -0400


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

Modified Files:
      Tag: cw-mail-branch
	states.py tabcomplete.py test_accessors.py test_states.py 
	test_tabcomplete.py test_vocabulary.py 
Log Message:
Synced up with HEAD

=== Zope3/src/zope/schema/tests/states.py 1.2 => 1.2.2.1 ===
--- Zope3/src/zope/schema/tests/states.py:1.2	Tue May 20 12:10:30 2003
+++ Zope3/src/zope/schema/tests/states.py	Sun Jun 22 10:23:52 2003
@@ -16,7 +16,7 @@
 
 from zope.schema import interfaces
 from zope.schema import vocabulary
-
+from zope.interface import implements
 
 # This table is based on information from the United States Postal Service:
 # http://www.usps.com/ncsc/lookups/abbreviations.html#states
@@ -85,7 +85,7 @@
 
 class State:
     __slots__ = 'value', 'title'
-    __implements__ = interfaces.ITerm
+    implements(interfaces.ITerm)
 
     def __init__(self, value, title):
         self.value = value
@@ -101,7 +101,7 @@
 
 class StateVocabulary(object):
     __slots__ = ()
-    __implements__ = IStateVocabulary
+    implements(IStateVocabulary)
 
     def __init__(self, object=None):
         pass
@@ -120,7 +120,7 @@
 
 
 class StateSelectionField(vocabulary.VocabularyField):
-    __implements__ = vocabulary.VocabularyField.__implements__
+
     vocabulary = StateVocabulary()
 
     def __init__(self, **kw):


=== Zope3/src/zope/schema/tests/tabcomplete.py 1.2 => 1.2.2.1 ===
--- Zope3/src/zope/schema/tests/tabcomplete.py:1.2	Tue May 20 12:10:30 2003
+++ Zope3/src/zope/schema/tests/tabcomplete.py	Sun Jun 22 10:23:52 2003
@@ -15,73 +15,62 @@
 """Example vocabulary for tab completion."""
 
 
-from zope.schema.interfaces import ITerm, ISubsetVocabulary, IVocabulary
+from zope.schema.interfaces import ITerm, IVocabulary, IVocabularyQuery
+from zope.interface import implements, Interface
+
+
+class IPrefixQuery(IVocabularyQuery):
+    """Interface for prefix queries."""
+    
+    def queryForPrefix(prefix):
+        """Return a vocabulary that contains terms beginning with
+        prefix."""
 
 
 class Term:
-    __implements__ = ITerm
+    implements(ITerm)
 
     def __init__(self, value):
         self.value = value
 
 
-class TermIterator:
-    def __init__(self, values):
-        self._next = iter(values).next
-
-    def __iter__(self):
-        return self
-
-    def next(self):
-        return Term(self._next())
-
-
 class CompletionVocabulary(object):
-    __implements__ = IVocabulary
+    implements(IVocabulary)
 
     def __init__(self, values):
         # In practice, something more dynamic could be used to
         # get the list possible completions.
-        self._values = tuple(values)
+        # We force a _values to be a list so we can use .index().
+        self._values = list(values)
+        self._terms = map(Term, self._values)
 
     def __contains__(self, value):
         return value in self._values
 
-    def getTerm(self, value):
-        if value in self._values:
-            return Term(value)
-        raise LookupError(value)
-
     def __iter__(self):
-        return TermIterator(self._values)
+        return iter(self._terms)
 
     def __len__(self):
         return len(self._values)
 
-    def queryForPrefix(self, prefix):
-        return SubsetCompletionVocabulary(self._match_prefix(prefix),
-                                          self)
-
-    def _match_prefix(self, prefix):
-        L = [v for v in self._values if v.startswith(prefix)]
-        if L:
-            return L
-        else:
-            raise LookupError("no entries matching prefix %r" % prefix)
+    def getQuery(self):
+        return PrefixQuery(self)
 
+    def getTerm(self, value):
+        if value in self._values:
+            return self._terms[self._values.index(value)]
+        raise LookupError(value)
 
-class SubsetCompletionVocabulary(CompletionVocabulary):
-    __implements__ = IVocabulary, ISubsetVocabulary
 
-    def __init__(self, values, master):
-        super(SubsetCompletionVocabulary, self).__init__(values)
-        self._master = master
+class PrefixQuery:
+    implements(IPrefixQuery)
 
-    def getMasterVocabulary(self):
-        return self._master
+    def __init__(self, vocabulary):
+        self.vocabulary = vocabulary
 
     def queryForPrefix(self, prefix):
-        # Never nest more than one level; cause the real
-        # master to always be returned by getMasterVocabulary()
-        return SubsetCompletionVocabulary(self._match_prefix(prefix),
-                                          self._master)
+        L = [v for v in self.vocabulary._values if v.startswith(prefix)]
+        if L:
+            return CompletionVocabulary(L)
+        else:
+            raise LookupError("no entries matching prefix %r" % prefix)


=== Zope3/src/zope/schema/tests/test_accessors.py 1.2 => 1.2.4.1 ===
--- Zope3/src/zope/schema/tests/test_accessors.py:1.2	Mon May 12 06:02:41 2003
+++ Zope3/src/zope/schema/tests/test_accessors.py	Sun Jun 22 10:23:52 2003
@@ -19,7 +19,7 @@
 """
 
 import unittest
-from zope.interface import Interface
+from zope.interface import Interface, implements
 from zope.schema import Text, accessors
 from zope.schema.interfaces import IText
 from zope.schema.accessors import FieldReadAccessor, FieldWriteAccessor
@@ -37,10 +37,10 @@
             getFoo, setFoo = accessors(field)
 
         class Bad:
-            __implements__ = I
+            implements(I)
 
         class Good:
-            __implements__ = I
+            implements(I)
 
             def getFoo(self):
                 return u"foo"


=== Zope3/src/zope/schema/tests/test_states.py 1.2 => 1.2.2.1 ===
--- Zope3/src/zope/schema/tests/test_states.py:1.2	Tue May 20 12:10:30 2003
+++ Zope3/src/zope/schema/tests/test_states.py	Sun Jun 22 10:23:52 2003
@@ -16,7 +16,7 @@
 
 import unittest
 
-from zope.interface import Interface
+from zope.interface import Interface, implements
 
 from zope.schema import vocabulary
 from zope.schema.tests import states
@@ -47,7 +47,7 @@
         )
 
 class BirthInfo:
-    __implements__ = IBirthInfo
+    implements(IBirthInfo)
 
     def __init__(self):
         self.state = state


=== Zope3/src/zope/schema/tests/test_tabcomplete.py 1.2 => 1.2.2.1 ===
--- Zope3/src/zope/schema/tests/test_tabcomplete.py:1.2	Tue May 20 12:10:30 2003
+++ Zope3/src/zope/schema/tests/test_tabcomplete.py	Sun Jun 22 10:23:53 2003
@@ -16,8 +16,7 @@
 
 import unittest
 
-from zope.schema import vocabulary
-from zope.schema.interfaces import ITerm
+from zope.schema.interfaces import ITerm, IVocabularyQuery
 from zope.schema.tests import tabcomplete
 
 
@@ -27,17 +26,21 @@
         self.vocab = tabcomplete.CompletionVocabulary(['abc', 'def'])
 
     def test_successful_query(self):
-        subset = self.vocab.queryForPrefix("a")
+        query = self.vocab.getQuery()
+        subset = query.queryForPrefix("a")
         L = [term.value for term in subset]
         self.assertEqual(L, ["abc"])
-        self.assert_(subset.getMasterVocabulary() is self.vocab)
-        subset = self.vocab.queryForPrefix("def")
+        subset = query.queryForPrefix("def")
         L = [term.value for term in subset]
         self.assertEqual(L, ["def"])
-        self.assert_(subset.getMasterVocabulary() is self.vocab)
 
     def test_failed_query(self):
-        self.assertRaises(LookupError, self.vocab.queryForPrefix, "g")
+        query = self.vocab.getQuery()
+        self.assertRaises(LookupError, query.queryForPrefix, "g")
+
+    def test_query_interface(self):
+        query = self.vocab.getQuery()
+        self.assert_(IVocabularyQuery.isImplementedBy(query))
 
     def test_getTerm(self):
         term = self.vocab.getTerm("abc")


=== Zope3/src/zope/schema/tests/test_vocabulary.py 1.2 => 1.2.2.1 ===
--- Zope3/src/zope/schema/tests/test_vocabulary.py:1.2	Tue May 20 12:10:30 2003
+++ Zope3/src/zope/schema/tests/test_vocabulary.py	Sun Jun 22 10:23:53 2003
@@ -14,9 +14,11 @@
 
 """Test of the VocabularyField and related support APIs."""
 
-import sys
 import unittest
 
+from zope.interface.verify import verifyObject
+from zope.interface import Interface, implements
+
 from zope.schema import interfaces
 from zope.schema import vocabulary
 
@@ -58,7 +60,7 @@
     pass
 
 class SampleVocabulary:
-    __implements__ = interfaces.IVocabulary
+    implements(interfaces.IVocabulary)
 
     def __contains__(self, value):
         return 0 <= value < 10
@@ -98,7 +100,7 @@
         self.check_preconstructed(vocabulary.VocabularyField, 1, 42)
 
     def test_preconstructed_vocabulary_multi(self):
-        self.check_preconstructed(vocabulary.VocabularyMultiField,
+        self.check_preconstructed(vocabulary.VocabularyListField,
                                   [1], [1, 42])
 
     def check_constructed(self, cls, okval, badval):
@@ -118,13 +120,179 @@
         self.check_constructed(vocabulary.VocabularyField, 1, 42)
 
     def test_constructed_vocabulary_multi(self):
-        self.check_constructed(vocabulary.VocabularyMultiField,
+        self.check_constructed(vocabulary.VocabularyListField,
                                [1], [1, 42])
 
+    def test_abstract_base_class_is_abstract(self):
+        self.assertRaises(NotImplementedError,
+                          vocabulary.VocabularyMultiField, vocabulary="foo")
+
+    def check_constructed_vocabulary_multi_default(self, cls):
+        # make sure these don't die during construction:
+        cls(vocabulary="testvocab", default=None)
+        L = []
+        unbound = cls(vocabulary="testvocab", default=L)
+        self.assertEqual(L, unbound.default)
+        self.assert_(unbound.default is not L)
+        # XXX this does, but not clear that it should:
+        self.assertRaises(ValueError,
+                          cls, vocabulary="testvocab", default=['xy'])
+
+    def test_constructed_vocabulary_bag_default(self):
+        self.check_constructed_vocabulary_multi_default(
+            vocabulary.VocabularyBagField)
+
+    def test_constructed_vocabulary_list_default(self):
+        self.check_constructed_vocabulary_multi_default(
+            vocabulary.VocabularyListField)
+
+    def test_constructed_vocabulary_set_default(self):
+        self.check_constructed_vocabulary_multi_default(
+            vocabulary.VocabularySetField)
+
+    def test_constructed_vocabulary_unique_list_default(self):
+        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):
+
+    list_vocab = vocabulary.SimpleVocabulary.fromValues([1, 2, 3])
+    items_vocab = vocabulary.SimpleVocabulary.fromItems(
+        [('one', 1), ('two', 2), ('three', 3), ('fore!', 4)])
+
+    def test_simple_term(self):
+        t = vocabulary.SimpleTerm(1)
+        verifyObject(interfaces.ITokenizedTerm, t)
+        self.assertEqual(t.value, 1)
+        self.assertEqual(t.token, "1")
+        t = vocabulary.SimpleTerm(1, "One")
+        verifyObject(interfaces.ITokenizedTerm, t)
+        self.assertEqual(t.value, 1)
+        self.assertEqual(t.token, "One")
+
+    def test_order(self):
+        value = 1
+        for t in self.list_vocab:
+            self.assertEqual(t.value, value)
+            value += 1
+
+        value = 1
+        for t in self.items_vocab:
+            self.assertEqual(t.value, value)
+            value += 1
+
+    def test_implementation(self):
+        self.failUnless(verifyObject(interfaces.IVocabulary, self.list_vocab))
+        self.failUnless(
+            verifyObject(interfaces.IVocabularyTokenized, self.list_vocab))
+        self.failUnless(verifyObject(interfaces.IVocabulary, self.items_vocab))
+        self.failUnless(
+            verifyObject(interfaces.IVocabularyTokenized, self.items_vocab))
+
+    def test_addt_interfaces(self):
+        class IStupid(Interface):
+            pass
+        v = vocabulary.SimpleVocabulary.fromValues([1, 2, 3], IStupid)
+        self.failUnless(IStupid.isImplementedBy(v))
+
+    def test_len(self):
+        self.assertEqual(len(self.list_vocab), 3)
+        self.assertEqual(len(self.items_vocab), 4)
+
+    def test_contains(self):
+        for v in (self.list_vocab, self.items_vocab):
+            self.assert_(1 in v and 2 in v and 3 in v)
+            self.assert_(5 not in v)
+
+    def test_get_query(self):
+        self.assert_(self.list_vocab.getQuery() is None)
+
+    def test_iter_and_get_term(self):
+        for v in (self.list_vocab, self.items_vocab):
+            for term in v:
+                self.assert_(v.getTerm(term.value) is term)
+                self.assert_(v.getTermByToken(term.token) is term)
+
+    def test_nonunique_tokens(self):
+        self.assertRaises(
+            AssertionError, vocabulary.SimpleVocabulary.fromValues,
+            [2, '2'])
+        self.assertRaises(
+            AssertionError, vocabulary.SimpleVocabulary.fromItems, 
+            [(1, 'one'), ('1', 'another one')])
+
+    def test_overriding_createTerm(self):
+        class MyTerm:
+            def __init__(self, value):
+                self.value = value
+                self.token = repr(value)
+                self.nextvalue = value + 1
+
+        class MyVocabulary(vocabulary.SimpleVocabulary):
+            def createTerm(cls, value):
+                return MyTerm(value)
+            createTerm = classmethod(createTerm)
+
+        vocab = MyVocabulary.fromValues([1, 2, 3])
+        for term in vocab:
+            self.assertEqual(term.value + 1, term.nextvalue)
+
 
 def test_suite():
     suite = unittest.makeSuite(RegistryTests)
     suite.addTest(unittest.makeSuite(VocabularyFieldTests))
+    suite.addTest(unittest.makeSuite(SimpleVocabularyTests))
     return suite
 
 if __name__ == "__main__":